Operators are symbols that perform operations on one or more values: arithmetic (e.g. +, *), comparison (==, <), logical (and, or), and others. This guide explains how each group works and how they combine in expressions. You will use them in conditionals, loops, and every script you write. For the difference between == and is see == vs is.
1. Arithmetic operators
Arithmetic operators work on numbers (and some on sequences). Division / always returns a float in Python 3; use // for floor division and % for remainder. Mixing types can cause TypeError when adding int and str—convert with int() or str() when needed. See Data types.
a, b = 10, 3
print(a + b) # 13
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
2. Comparison operators
Comparisons return True or False. Use == for value equality and is only for identity (e.g. x is None). See == vs is.
x = 5
y = 10
print(x == y) # False (equal to)
print(x != y) # True (not equal to)
print(x < y) # True (less than)
print(x > y) # False (greater than)
print(x <= y) # True (less than or equal)
print(x >= y) # False
3. Logical operators
and, or, and not combine or invert boolean values. They short‑circuit. Non‑boolean values are truthy or falsy (e.g. empty list is falsy).
a = True
b = False
print(a and b) # False (both must be True)
print(a or b) # True (at least one True)
print(not a) # False
4. Assignment operators
Compound assignment: x += 5 is equivalent to x = x + 5. Same for -=, *=, /=, etc.
x = 10
x += 5 # x = x + 5, result: 15
x -= 3 # x = x - 3, result: 12
x *= 2 # x = x * 2, result: 24
x /= 4 # x = x / 4, result: 6.0
x //= 2 # x = x // 2, result: 3.0
x %= 2 # 1.0
5. Identity: is and is not
is tests whether two names refer to the same object; == tests value equality. Use is mainly for None. See == vs is.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a is b) # False (different objects)
print(a is c) # True (same object)
print(a == b) # True (same values)
print(a is not b) # True
6. Membership: in and not in
in checks whether a value is in a sequence (list, tuple, string, set, dict keys). See The in operator.
fruits = ["apple", "banana", "orange"]
print("apple" in fruits) # True
print("grape" in fruits) # False
print("banana" not in fruits) # False
7. Operator precedence
Parentheses () override precedence. Rough order: (), then **, then * / // %, then + -, then comparison, then not, and, or. When in doubt, add parentheses.
result = 2 + 3 * 4 # 14
result = (2 + 3) * 4 # 20
8. Next steps
Use operators in conditionals and loops. When you see TypeError or ZeroDivisionError, check types and the Errors section.