This section lists common Python coding tricks and general usage hints. Consult the Python Library Reference and Python Language Reference (http://www.python.org/doc/) for further information on topics mentioned here.
S[:] makes a top-level (shallow) copy of any sequence;
copy.deepcopy(X) makes full
D.copy() copy lists and
items at front of list
[X,Y,Z] all insert multiple
items at the end of a list, in-place.
X=L.pop() can be used to implement
in-place stack operations, where the end of the list is the top of
for key in D.keys():
to iterate through dictionaries, or simply
for key in D: in version 2.2 and later.
In Python 3.0 these two forms are equivalent, since
keys is an iterable view.
for key in sorted(D):
to iterate over dictionary keys in sorted fashion in version 2.4
and later; the form
for key in K: also works in Python 2.X but not Python 3.0, since
keys results are view objects,
X=A or B or None assigns
X to the first true object
None if both are false (i.e., 0 or
X,Y = Y,X swaps the
red, green, blue =
range(3) assigns integer series.
statements to ensure that arbitrary termination code is run;
especially useful around locking calls (acquire before the
try, release in the
with/as statements ...