
|
3.10.1 Mapping Operators to Functions
This table shows how abstract operations correspond to operator
symbols in the Python syntax and the functions in the
operator module.
| Addition |
a + b |
add(a, b) |
| Concatenation |
seq1 + seq2 |
concat(seq1, seq2) |
| Containment Test |
o in seq |
contains(seq, o) |
| Division |
a / b |
div(a, b) # without __future__.division |
| Division |
a / b |
truediv(a, b) # with __future__.division |
| Division |
a // b |
floordiv(a, b) |
| Bitwise And |
a & b |
and_(a, b) |
| Bitwise Exclusive Or |
a ^ b |
xor(a, b) |
| Bitwise Inversion |
~ a |
invert(a) |
| Bitwise Or |
a | b |
or_(a, b) |
| Indexed Assignment |
o[k] = v |
setitem(o, k, v) |
| Indexed Deletion |
del o[k] |
delitem(o, k) |
| Indexing |
o[k] |
getitem(o, k) |
| Left Shift |
a << b |
lshift(a, b) |
| Modulo |
a % b |
mod(a, b) |
| Multiplication |
a * b |
mul(a, b) |
| Negation (Arithmetic) |
- a |
neg(a) |
| Negation (Logical) |
not a |
not_(a) |
| Right Shift |
a >> b |
rshift(a, b) |
| Sequence Repitition |
seq * i |
repeat(seq, i) |
| Slice Assignment |
seq[i:j] = values |
setslice(seq, i, j, values) |
| Slice Deletion |
del seq[i:j] |
delslice(seq, i, j) |
| Slicing |
seq[i:j] |
getslice(seq, i, j) |
| String Formatting |
s % o |
mod(s, o) |
| Subtraction |
a - b |
sub(a, b) |
| Truth Test |
o |
truth(o) |
| Ordering |
a < b |
lt(a, b) |
| Ordering |
a <= b |
le(a, b) |
| Equality |
a == b |
eq(a, b) |
| Difference |
a != b |
ne(a, b) |
| Ordering |
a >= b |
ge(a, b) |
| Ordering |
a > b |
gt(a, b) |
See About this document... for information on suggesting changes.
|