
38
|
Python Pocket Reference
• Class names commonly begin with an uppercase letter
(e.g.,
MyClass).
• The first (leftmost) argument in a class method function
is commonly named
self.
Specific Statements
The following sections describe all Python statements. Each
section lists the statement’s syntax formats, followed by
usage details. For compound statements, each appearance of
a suite in a statement format stands for one or more other
statements, possibly indented as a block under a header line.
A suite must be indented under a header if it contains
another compound statement (
if, while, etc.); otherwise, it
can appear on the same line as the statement header. The fol-
lowing are both valid constructs:
if x < 42:
print x
while x: x = x -1
if x < 42: print x
Assignment
target = expression
target1 = target2 = expression
target1, target2 = expression1, expression2
target1, target2,... = same-length-sequence
(target1, target2,...) = same-length-sequence
[target1, target2,...] = same-length-sequence
Assignments store references to objects in targets. Expres-
sions yield objects. Targets can be simple names (
X), quali-
fied attributes (
X.attr), or indexes and slices (X[i], X[i:j]).
The second format assigns an
expression object to each tar-
get. The third format pairs targets with expressions, left to
right. The last three formats assign components of any
sequence to corresponding targets, from left to right. The
sequence ...