Python Operators
Operators are the symbols and keywords that combine values: +, ==, and, in and so on. Python groups them into a handful of families.
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 9 + 4 |
13 |
- |
Subtraction | 9 - 4 |
5 |
* |
Multiplication | 9 * 4 |
36 |
/ |
Division | 9 / 4 |
2.25 |
// |
Floor division | 9 // 4 |
2 |
% |
Modulus (remainder) | 9 % 4 |
1 |
** |
Exponent | 9 ** 2 |
81 |
minutes = 135
print(minutes // 60, "hours and", minutes % 60, "minutes")
print(-7 // 2) # floor division rounds DOWN, toward minus infinity
print(2 ** 0.5) # square root
print(10 % 2 == 0) # a classic "is it even?" test2 hours and 15 minutes
-4
1.4142135623730951
True
Assignment operators
Combined operators update a variable in place: x += 3 is short for x = x + 3.
| Operator | Same as |
|---|---|
x += 3 |
x = x + 3 |
x -= 3 |
x = x - 3 |
x *= 3 |
x = x * 3 |
x /= 3 |
x = x / 3 |
x //= 3 |
x = x // 3 |
x %= 3 |
x = x % 3 |
x **= 3 |
x = x ** 3 |
balance = 100
balance += 50
balance -= 30
balance *= 2
print(balance)
label = "item"
label += "s"
print(label)240
items
Note: Python has no
++or--operators. Writecount += 1.
The walrus operator :=
Python 3.8 added :=, which assigns and returns a value inside an expression:
data = [4, 8, 15, 16, 23, 42]
if (n := len(data)) > 5:
print(f"List is long ({n} items)")List is long (6 items)
Comparison operators
==, !=, >, <, >=, <= all return a boolean. Python lets you chain comparisons, which reads just like maths:
x = 7
print(x == 7, x != 7)
print(1 < x < 10) # same as 1 < x and x < 10
print(0 <= x <= 5)True False
True
False
Logical operators
and, or, not combine boolean expressions (see the Booleans lesson for short-circuiting).
Identity operators: is, is not
is checks whether two names refer to the same object, while == checks whether two objects have equal values:
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b, a is b)
print(a is c)
value = None
print(value is None)True False
True
True
Tip: Use
isonly forNone(andTrue/Falsesingletons). For everything else, compare with==.
Membership operators: in, not in
vowels = "aeiou"
print("e" in vowels)
print(3 in [1, 2, 3])
print("key" in {"key": 1}) # dictionaries check their keys
print("z" not in vowels)True
True
True
True
Bitwise operators
These work on the binary digits of integers: & (AND), | (OR), ^ (XOR), ~ (NOT), << and >> (shifts).
a, b = 0b1100, 0b1010
print(bin(a & b), bin(a | b), bin(a ^ b))
print(1 << 4, 64 >> 2)0b1000 0b1110 0b110
16 16
Operator precedence
When several operators appear in one expression, higher-precedence ones are evaluated first (top of the table first):
| Precedence | Operators |
|---|---|
| 1 | () parentheses |
| 2 | ** |
| 3 | unary +x, -x, ~x |
| 4 | *, /, //, % |
| 5 | +, - |
| 6 | <<, >> |
| 7 | &, then ^, then | |
| 8 | comparisons, in, is |
| 9 | not, then and, then or |
print(2 + 3 * 4)
print((2 + 3) * 4)
print(-2 ** 2) # ** binds tighter than unary minus
print(True or False and False)14
20
-4
True
When in doubt, add parentheses — they cost nothing and make intent clear.
Test yourself
What is 17 % 5?
- 3
- 2
- 3.4
- 12
What is the difference between == and is?
- None, they are synonyms
==compares values,ischecks for the same objectiscompares values,==checks for the same objectisonly works with strings
What does -2 ** 2 evaluate to?
- 4
- -4
- 0
- SyntaxError