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

PY arithmetic.py
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)
Output
22 12 85
3.4
3
2
289
-4 3
OperatorMeaning
/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

PY overloaded.py
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})
Output
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

PY compare.py
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")
Output
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

PY logic.py
age = 25
member = False

print(age >= 18 and member)
print(age >= 18 or member)
print(not member)
print(age >= 18 and not member)
Output
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

PY identity.py
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})
Output
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

PY assignment.py
n = 10
n += 3
n *= 2
n //= 4
n **= 2
n %= 7
print(n)

words = ["a"]
words += ["b", "c"]
print(words)
Output
1
['a', 'b', 'c']

The walrus operator :=

The assignment expression := assigns a value and returns it, so you can use it inside a condition:

PY walrus.py
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)
Output
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

PY ternary.py
stock = 0
status = "in stock" if stock > 0 else "sold out"
print(status)
Output
sold out

Python's "ternary" reads like English: value_if_true if condition else value_if_false.

Bitwise operators

PY bitwise.py
a, b = 0b1100, 0b1010
print(a & b, a | b, a ^ b, ~a, a << 1, a >> 2)
Output
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.

Try it yourself

Try it yourself
Console output