TExcelTAD
Home › Python › Python Operators

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
python · try it yourself
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?" test
2 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
python · try it yourself
balance = 100
balance += 50
balance -= 30
balance *= 2
print(balance)

label = "item"
label += "s"
print(label)
240
items

Note: Python has no ++ or -- operators. Write count += 1.

The walrus operator :=

Python 3.8 added :=, which assigns and returns a value inside an expression:

python · try it yourself
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:

python · try it yourself
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:

python · try it yourself
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 is only for None (and True/False singletons). For everything else, compare with ==.

Membership operators: in, not in

python · try it yourself
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).

python · try it yourself
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
python · try it yourself
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?

  1. 3
  2. 2
  3. 3.4
  4. 12

What is the difference between == and is?

  1. None, they are synonyms
  2. == compares values, is checks for the same object
  3. is compares values, == checks for the same object
  4. is only works with strings

What does -2 ** 2 evaluate to?

  1. 4
  2. -4
  3. 0
  4. SyntaxError