Operations by Category
In this section, trailing parentheses are omitted from X method names for brevity. In general, all built-in types support the comparisons and Boolean operations listed in Table 2 (although Python 3.X does not support magnitude comparisons for dictionaries or mixed nonnumeric types).
Boolean true means any nonzero number or any nonempty collection object (list, dictionary, etc.), and all objects have a Boolean value. The built-in names True and False are preassigned to true and false values and behave like integers 1 and 0 with custom display formats. The special object None is false and appears in various Python contexts.
Comparisons return True or False and are automatically applied recursively in compound objects as needed to determine a result.
Boolean and and or operators stop (short-circuit) as soon as a result is known and return one of the two operand objects — the value on the left or the right of the operator — whose Boolean value gives the result.
Table 2. Comparisons and Boolean operations
Operator
Description
X < Y
Strictly less than[a]
X <= Y
Less than or equal to
X > Y
Strictly greater than
X >= Y
Greater than or equal to
X == Y
Equal to (same value)
X != Y
Not equal to (same as X<>Y in Python 2.X only)[b]
X is Y
Same object
X is not Y
Negated object identity
X < Y < Z
Chained comparisons
not X
If X is false then True; else, False
X or Y
If X is false then Y; else, X
X and Y
If X is false then X; else, Y
[a] To implement comparison expressions, see both the rich comparison (e.g., lt for <) class methods in 3.X and 2.X, and general cmp method in 2.X, described in Operator Overloading Methods.
[b] != and <> both mean not equal by value in 2.X, but != is the preferred syntax in 2.X and the only supported option in 3.X. is performs an identity test; == performs value comparison, and so is much more generally useful.
Tables 3 through 6 define operations common to types in the three major type categories — sequence (positionally ordered), mapping (access-by-key), and number (all numeric types) — as well as operations available for mutable (changeable) types in Python. Most types also export additional type-specific operations (e.g., methods), as described in Specific Built-in Types.
Table 3. Sequence operations (strings, lists, tuples, bytes, bytearray)
Operation
Description
Class method
X in S
X not in S
Membership tests
contains,
iter,
getitem[a]
S1 + S2
Concatenation
add
S * N, N * S
Repetition
mul
S[i]
Index by offset
getitem
S[i:j], S[i:j:k]
Slicing: items in S from offset i through j−1 by optional stride k
getitem[b]
len(S)
Length
len
min(S), max(S)
Minimum, maximum item
iter,
getitem
iter(S)
Iteration protocol
iter
for X in S:,
[expr for X in S],
map(func, S), etc.
Iteration (all contexts)
iter,
getitem
[a] See also The iteration protocol for more on these methods and their interplay. If defined, contains is preferred over iter, and iter is preferred over getitem.
[b] In Python 2.X, you may also define getslice, setslice, and delslice to handle slicing operations. In 3.X, these are removed in favor of passing slice objects to their item-based indexing counterparts. Slice objects may be used explicitly in indexing expressions in place of i:j:k bounds.
Table 4. Mutable sequence operations (lists, bytearray)
Operation
Description
Class method
S[i] = X
Index assignment: change item at existing offset i to reference X
setitem
S[i:j] = I,
S[i:j:k] = I
Slice assignment: S from i through j−1 with optional stride k (possibly empty) is replaced by all items in iterable I
setitem
del S[i]
Index deletion
delitem
del S[i:j],
del S[i:j:k]
Slice deletion
delitem
Table 5. Mapping operations (dictionaries)
Operation
Description
Class method
D[k]
Index by key
getitem
D[k] = X
Key assignment: change or create entry for key k to reference X
setitem
del D[k]
Delete item by key
delitem
len(D)
Length (number of keys)
len
k in D
Key membership test[a]
Same as in Table 3
k not in D
Converse of k in D
Same as in Table 3
iter(D)
Iterator object for D’s keys
Same as in Table 3
for k in D:, etc.
Iterate through keys in D (all iteration contexts)
Same as in Table 3
[a] In Python 2.X, key membership may also be coded as D.has_key(k). This method is removed in Python 3.X in favor of the in expression, which is also generally preferred in 2.X. See Dictionaries.
Table 6. Numeric operations (all number types)
Operation
Description
Class method
X + Y, X − Y
Add, subtract
add, sub
X * Y, X / Y,
X // Y, X % Y
Multiply, divide, floor divide, remainder
mul, truediv[a],
floordiv, mod
−X, +X
Negative, identity
neg, pos
X | Y, X & Y,
X ^ Y
Bitwise OR, AND, exclusive OR (integers)
or, and, xor
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[b]
int
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
[a] The / operator invokes truediv in Python 3.X, but div in Python 2.X unless true division is enabled. See Operator Usage Notes for division semantics.
[b] In Python 2.X, the long() built-in function invokes the long class method. In Python 3.X, the int type subsumes long, which is removed.
Fair Use Source: B00HZ41PGC