The Python Programming Language
Lexemes
# This is a comment
123L
9999999999l
6e10
9.9E-20
123.0
4E90
0176
0x4af
1+2j
3J
5.5-1.1j
This is a string.
"This is also a string."
"""This is a
triple-quoted string."""
Notice the last line: Pythons triple-quoted string constant form. Python collects all the lines in the quoted block and concatenates them in a single multiline string, putting an end-of-line character ("\012") between each line. Note that "\012" is an octal integer.
[] #an empty list
[1, 2, 3]
[a, [b, c] ] #nesting
You can think of Python lists as arrays of pointers in C because lists only store references to other objects, not the copy of the objects. For example:
L1 = [hi, hello, yo]
L2 = L1
In this case, if you make any changes to L1, it will also effect L2 because L2 is a reference of L1 (in another word, L2 is pointing to L1).
{} #an empty dictionary
{a: 1, b: 2}
{rose: {red: 1, black: 2} } #nesting
Note that Pyhon dictionary is implemented as hash tables. Like lists, dictionaries only store object references (not copies).
where L(x) ::= empty | x "," | x "," x #("," x).
() #an empty tuple
(0,) #a one-item tuple (not an expression)
(a, (b, c) ) #nesting
Notice the second line here. If you really want a one-item tuple, you need to put a comma after the single item, before the closing parenthesis, to tell Python that it is not a simple expression.
In some cases, Python allows us to omit the parentheses for a tuple where it is not syntactically ambiguous. For example:
T1 = 1, 2, 3, 4 #an example of an assignment statement
Although, the parentheses are optional, it is easier to use them every time than to figure out when they are optional.
Note that tuples work exactly the same way as lists, however, they cannot grow or shrink like lists.
** computes an exponential number.
% computes a division remainder.
2**3 = 8 #same as 23
5%2 = 1
+ is used to concatenate strings.
* is used to repeat strings.
% is used to format strings.
'I am a ' + 'student' # 'I am a student'
'Hello' * 3 #same as HelloHelloHello
'%d %s %d you %' (99, 'beer', 4) # 99 beer 4 you
Variable names must start with an underscore or letter, and be followed by any number of underscores, letters, or digits. Python variable names are case-sensitive (names z and Z are two different variables). For example:
_flag # a legal name
flag # a legal name
4_flag # not a legal name
flag$ # not a legal name
@!# # not a legal name
Names that you define cannot be the same as reserved words.
Statement
Assignment statement stores references to objects in variable names or data structure slots.
hello = 'Hello World' #Basic form
today, tomorrow = 13, 14 #Tuple assignment
lunch = dinner = 'junk food' #Multiple-target
In the second line, name today is assigned to integer 13, and name tomorrow is bound to integer 14. In the last line, names lunch and dinner are both assigned a reference to the string junk food.
1 + 1
'Hello' + 'World'
a == b
play()
len(d3,d4)
x.run()
L.append(L)
tom < dan and dan == john
A < B < C #tests whether B is between A and C
>>> print "What sup world"
"What sup world"
>>> print "How", "are", "you?"
"How are you"
>>> print "a" + "b"
ab
Here is a general form of an if statement:
if <test1>:
<statements1>
elif <test2>: #any number of elif
<statements2>
else: #optional else
<statements3>
For example:
>>> x = 0
>>> if x == 1:
... print true
... elif x == 0:
... print false
... else:
... print x
...
false
A > B
C != D
A and B
C or D
Here is a general form of a while statement:
while <test>:
<statements1>
else: #optional
<statements2>
For example:
>>> x = 0; y =10
>>> while x < y:
... print x,
... x = x +1
...
0 1 2 3 4 5 6 7 8 9
Here is a general form of a for statement:
for <target> in <object>:
<statements1>
else: #optional
<statements2>
For example:
>>> total = 0
>>> for x in [1, 2, 3, 4, 5]:
... total = total + x
...
>>> total
15