Operators
Operators combine values: arithmetic to calculate, comparisons to ask questions, logical operators to combine answers, and a few Python specialities like chained comparisons, membership tests and the walrus operator.
Arithmetic
a, b = 17, 5 print(a + b, a - b, a * b) print(a / b) print(a // b) print(a % b) print(a ** 2) print(-a // b, -a % b)
22 12 85 3.4 3 2 289 -4 3
| Operator | Meaning |
|---|---|
/ | True division — always a float |
// | Floor division — rounds down towards negative infinity |
% | Remainder (modulo); the result has the sign of the divisor |
** | Power |
Note the last line: floor division rounds down, so -17 // 5 is -4, and % follows suit so that (a // b) * b + a % b == a always holds.
Operators work on other types too
print("ab" + "cd", "ab" * 3) print([1, 2] + [3], [0] * 4) print({1, 2} | {2, 3}, {1, 2} & {2, 3}) print({"a": 1} | {"b": 2})
abcd ababab
[1, 2, 3] [0, 0, 0, 0]
{1, 2, 3} {2}
{'a': 1, 'b': 2}Each type decides what an operator means for it. Your own classes can do the same (lesson 38).
Comparison
x = 7 print(x == 7, x != 7) print(x > 5, x <= 6) print(1 < x < 10) print(1 < x > 10) print([1, 2] < [1, 3], "abc" < "abd")
True False True False True False True True
Python lets you chain comparisons: 1 < x < 10 means 1 < x and x < 10, just like in maths. Sequences compare element by element.
Logical operators
age = 25 member = False print(age >= 18 and member) print(age >= 18 or member) print(not member) print(age >= 18 and not member)
False True True True
Python spells them as words: and, or, not. Precedence is not, then and, then or; use parentheses when mixing them.
Identity and membership
a = [1, 2] b = [1, 2] c = a print(a == b, a is b, a is c) print(2 in a, 5 not in a) print("py" in "python", "key" in {"key": 1})
True False True True True True True
is asks "the same object?"; == asks "equal values?". in checks membership in strings, lists, tuples, sets and dictionary keys.
Assignment operators
n = 10 n += 3 n *= 2 n //= 4 n **= 2 n %= 7 print(n) words = ["a"] words += ["b", "c"] print(words)
1 ['a', 'b', 'c']
The walrus operator :=
The assignment expression := assigns a value and returns it, so you can use it inside a condition:
data = [4, 8, 15, 16, 23, 42] if (n := len(data)) > 5: print(f"List is long ({n} items)") values = [y for x in data if (y := x * 2) > 30] print(values)
List is long (6 items) [32, 46, 84]
Use it sparingly — it's handy for avoiding a repeated calculation, but can make code harder to read.
Conditional expression
stock = 0 status = "in stock" if stock > 0 else "sold out" print(status)
sold out
Python's "ternary" reads like English: value_if_true if condition else value_if_false.
Bitwise operators
a, b = 0b1100, 0b1010
print(a & b, a | b, a ^ b, ~a, a << 1, a >> 2)
8 14 6 -13 24 3
AND, OR, XOR, NOT and shifts work on the binary digits of integers — used for flags, permissions and low-level work.
Precedence
From highest to lowest: ** → unary -x → * / // % → + - → shifts → & → ^ → | → comparisons, in, is → not → and → or → conditional expression → :=. One trap: -2 ** 2 is -4, because ** binds tighter than the minus sign.