python_operations_by_category

Python Operations by Category

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.

python_operations_by_category.txt · Last modified: 2024/04/28 03:21 (external edit)