Technology  /  Python

🐍 Python 78 guides · updated 2026

From first variable to OOP, generators, and real projects — the language that runs everything from data pipelines to AI agents, taught the practical way.

Python Operators Explained: Arithmetic, Comparison, Logical, and the Tricky Ones

Python operators do exactly what the name suggests — they operate on values. You already know the arithmetic ones from school. The interesting parts are the Python-specific operators, the precedence rules that determine which operations happen first, and the few operators that behave differently than you might expect.


Arithmetic Operators

The standard math operators:

a, b = 17, 5
print(a + b) # 22 — addition
print(a - b) # 12 — subtraction
print(a * b) # 85 — multiplication
print(a / b) # 3.4 — true division (always returns float)
print(a // b) # 3 — floor division (rounds down, returns int)
print(a % b) # 2 — modulo (remainder)
print(a ** b) # 1419857 — exponentiation

Two things worth noting:

Division always returns a float. 10 / 2 gives 5.0, not 5. Use // when you need an integer result.

Floor division rounds toward negative infinity, not toward zero. This matters with negative numbers:

print(-17 // 5) # -4, not -3
print(-17 % 5) # 3, not -2

If you need truncation toward zero, use int(-17 / 5) instead.


Comparison Operators

These return True or False:

x = 10
print(x == 10) # True — equal to
print(x != 5) # True — not equal to
print(x > 8) # True — greater than
print(x < 8) # False — less than
print(x >= 10) # True — greater than or equal to
print(x <= 9) # False — less than or equal to

Python allows chaining comparisons, which reads more naturally:

score = 75
# Instead of: score >= 60 and score < 90
if 60 <= score < 90:
print("Passing grade")

Important: use == for equality, = for assignment. Using = in a condition causes a SyntaxError in Python (unlike some languages where it silently assigns).


Identity and Membership Operators

Two categories that beginners sometimes overlook:

Identity operators: is and is not

These test whether two variables point to the same object in memory — not whether they have equal values:

a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True — same values
print(a is b) # False — different objects
print(a is c) # True — same object
# Use 'is' for None comparisons
value = None
if value is None:
print("No value set")

Always use is None, not == None. The is check is more accurate and is the Python convention.

Membership operators: in and not in

fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # True
print("grape" not in fruits) # True
# Works on strings, lists, tuples, sets, and dicts
text = "Hello, world"
print("world" in text) # True
config = {"debug": True, "port": 8080}
print("debug" in config) # True — checks keys

Logical Operators

and, or, and not combine boolean expressions:

age = 25
has_ticket = True
# and: both must be True
if age >= 18 and has_ticket:
print("Entry allowed")
# or: at least one must be True
if age < 13 or age > 65:
print("Discount available")
# not: reverses the result
if not has_ticket:
print("No ticket found")

Short-circuit evaluation

Python stops evaluating as soon as the result is determined. With and, if the first expression is False, the second is never evaluated. With or, if the first is True, the second is skipped:

def expensive_check():
print("Running expensive check...")
return True
# The second expression is never evaluated
result = False and expensive_check() # prints nothing
# Practical use: safe attribute access
user = None
name = user and user.name # name = None without AttributeError

Truthy and falsy values

Logical operators work on any value, not just booleans. Empty sequences, zero, None, and False are all falsy:

name = ""
display_name = name or "Anonymous" # "Anonymous" — because "" is falsy
print(display_name)
items = []
if not items:
print("Cart is empty")

Assignment Operators

Beyond the basic =, Python has compound assignment operators:

count = 10
count += 3 # count = count + 3 = 13
count -= 2 # 11
count *= 2 # 22
count //= 4 # 5
count **= 2 # 25

The walrus operator := (Python 3.8+)

The walrus operator assigns a value and returns it in the same expression. It’s most useful in while loops and comprehensions where you’d otherwise need to compute something twice:

# Without walrus — compute len(line) twice
while len(line := input("Enter line: ")) > 0:
print(f"You entered {len(line)} characters")
# More useful example: filtering with a computed value
data = [1, 5, 2, 8, 3, 9]
filtered = [y for x in data if (y := x * 2) > 10]
print(filtered) # [16, 18]

Operator Precedence

When multiple operators appear in one expression, precedence determines the order of evaluation (highest to lowest):

  1. ** (exponentiation)
  2. +x, -x, ~x (unary)
  3. *, /, //, %
  4. +, -
  5. <<, >> (bitwise shifts)
  6. & (bitwise AND)
  7. ^ (bitwise XOR)
  8. | (bitwise OR)
  9. Comparison operators: ==, !=, <, >, <=, >=, is, in
  10. not
  11. and
  12. or

When in doubt, use parentheses. (a + b) * c is clearer than relying on the reader to recall precedence. The only rule worth memorising: multiplication before addition, and not before and before or.

# Surprising without knowledge of precedence
print(not True or True) # True — (not True) or True
print(not (True or True)) # False — not (True)

Parentheses resolve any ambiguity and make intent explicit.