
10
|
Python Pocket Reference
Sequence Operation Notes
Indexing: S[i]
• Fetches components at offsets (first item is at offset 0).
• Negative indexes mean to count backward from the end.
•
S[0] fetches the first item.
•
S[-2] fetches the second-to-last item (S[len(S) - 2]).
Slicing: S[i:j]
• Extracts contiguous sections of a sequence.
• Slice boundaries default to 0 and sequence length.
•
S[1:3] fetches from offsets 1 up to, but not including, 3.
•
S[1:] fetches from offsets 1 through the end (length-1).
•
S[:-1] fetches from offsets 0 up to, but not including, the
last item.
•
S[:]
makes a top-level (shallow) copy of sequence object
S
.
• Slice assignment is similar to deleting and then inserting.
X << N, X >> N Bitwise left-shift, right-
shift (integers).
__lshift__,
__rshift__
~X
Bitwise invert (integers). __invert__
X ** Y X
to the power Y. __pow__
abs(X)
Absolute value. __abs__
int(X)
Convert to integer. __int__
long(X)
Convert to long. __long__
float(X)
Convert to float. __float__
complex(X),
complex(re,im)
Make a complex value. __complex__
divmod(X, Y)
Tuple: (X/Y, X%Y). __divmod__
pow(X, Y [,Z])
Raise to a power. __pow__
Table 6. Numeric operations (all number types) (continued)
Operation Description Class method