Difference between revisions of "Python quick reference"
From thelinuxwiki
(→integar variable) |
(→assigning a variable to another variable) |
||
Line 49: | Line 49: | ||
>>> print var1 | >>> print var1 | ||
bar | bar | ||
+ | </source> | ||
+ | |||
+ | muli-variable example: | ||
+ | |||
+ | <source lang="python"> | ||
+ | >>> var1 = 'foo' | ||
+ | >>> var2 = 'bar' | ||
+ | >>> new_var = "%s %s" % (var1,var2) | ||
+ | >>> print new_var | ||
+ | <nowiki>foo bar</nowiki> | ||
</source> | </source> | ||
Revision as of 16:04, 14 August 2017
Contents |
Introduction
Lexical analysis
Data model
Execution model
Expressions
Simple statements
variable operations
assigenment examples
simple string assignment
>>> var1 = 'foo'
this automatically creates a variable of type string
>>> type(var1) <type 'str'>
printing your variable...
>>> print var1 foo
string assignment like above must be incapsulated by quotes or the right side is interpretted as a variable name.
example:
>>> var1 = foo Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'foo' is not defined
integer variable
>>> my_int_var1 = 1 >>> print my_int_var1 1
assigning a variable to another variable
>>> var2 = 'bar' >>> var1 = var2 >>> print var1 bar
muli-variable example:
>>> var1 = 'foo' >>> var2 = 'bar' >>> new_var = "%s %s" % (var1,var2) >>> print new_var <nowiki>foo bar</nowiki>
print sing variable named foo
>>> print foo
print multiple variables with text
print 'my variable are %s %s' % (FOO, BAR)
Common string operations
print nth word of string
print s.split()[n]