Operators & Expressions
📖 Concept
Python provides a rich set of operators that work intuitively with its data types. Understanding operators deeply — especially truthiness, short-circuit evaluation, and operator overloading — is essential for writing Pythonic code.
Arithmetic operators:
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 3 |
10 |
- |
Subtraction | 7 - 3 |
4 |
* |
Multiplication | 7 * 3 |
21 |
/ |
True division | 7 / 3 |
2.333... |
// |
Floor division | 7 // 3 |
2 |
% |
Modulus | 7 % 3 |
1 |
** |
Exponentiation | 2 ** 10 |
1024 |
Comparison operators return bool and support chaining:
# Chained comparisons — unique to Python!
0 < x < 100 # Same as: 0 < x and x < 100
a == b == c # All three are equal
1 <= grade <= 5 # Range check in one expression
Logical operators use English words, not symbols:
and— returns first falsy value, or last value if all truthyor— returns first truthy value, or last value if all falsynot— boolean negation
The walrus operator := (Python 3.8+) assigns and returns a value in one expression — useful in while loops and comprehensions.
Identity vs equality:
is/is not— checks if two references point to the same object==/!=— checks if two objects have the same value
Membership operators:
in/not in— checks if a value exists in a sequence or collection
💻 Code Example
1# ============================================================2# Arithmetic operators3# ============================================================4print(7 / 3) # 2.3333... (true division — always returns float)5print(7 // 3) # 2 (floor division — rounds toward negative infinity)6print(-7 // 3) # -3 (NOT -2! Rounds toward negative infinity)7print(7 % 3) # 1 (modulus)8print(-7 % 3) # 2 (Python's modulus always returns non-negative for positive divisor)910# Exponentiation11print(2 ** 10) # 102412print(16 ** 0.5) # 4.0 (square root)13print(pow(2, 10)) # 1024 (built-in function)14print(pow(2, 10, 1000)) # 24 (modular exponentiation: 2^10 % 1000)1516# ============================================================17# Comparison chaining (Pythonic!)18# ============================================================19age = 252021# BAD (other languages style):22if age >= 18 and age <= 65:23 print("Working age")2425# GOOD (Pythonic):26if 18 <= age <= 65:27 print("Working age")2829# Multiple chaining30x = 531print(1 < x < 10) # True32print(1 < x < 10 < 100) # True (all comparisons must be true)3334# ============================================================35# Logical operators: and, or, not36# ============================================================37# 'and' returns first falsy value, or last value if all truthy38print(1 and 2 and 3) # 3 (all truthy, returns last)39print(1 and 0 and 3) # 0 (first falsy value)40print(1 and "" and 3) # "" (first falsy value)4142# 'or' returns first truthy value, or last value if all falsy43print(0 or "" or 3) # 3 (first truthy value)44print(0 or "" or []) # [] (all falsy, returns last)4546# Practical use: default values47name = ""48display_name = name or "Anonymous" # "Anonymous"49print(display_name)5051# Short-circuit evaluation52# 'and' stops at first falsy — right side not evaluated53x = 054result = x != 0 and 10 / x # 10/x never executes (no ZeroDivisionError)55print(result) # False5657# ============================================================58# Walrus operator := (Python 3.8+)59# ============================================================60# Without walrus — compute twice or use temp variable61data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]6263# BAD: compute len() twice64# if len(data) > 5:65# print(f"List has {len(data)} items")6667# GOOD: compute once with walrus68if (n := len(data)) > 5:69 print(f"List has {n} items")7071# In while loops — read and check in one line72import io73buffer = io.StringIO("line1\nline2\nline3\n")74while (line := buffer.readline()):75 print(f"Read: {line.strip()}")7677# In list comprehensions — filter and transform78numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]79results = [y for x in numbers if (y := x ** 2) > 20]80print(results) # [25, 36, 49, 64, 81, 100]8182# ============================================================83# Membership and identity operators84# ============================================================85fruits = ["apple", "banana", "cherry"]86print("banana" in fruits) # True87print("grape" not in fruits) # True8889# 'in' works with strings too90print("hell" in "hello world") # True9192# 'in' with dictionaries checks keys (not values)93user = {"name": "Alice", "age": 30}94print("name" in user) # True (checks keys)95print("Alice" in user) # False (not a key)9697# Identity checks98a = [1, 2, 3]99b = [1, 2, 3]100print(a == b) # True (same value)101print(a is b) # False (different objects)102103c = a104print(a is c) # True (same object)105106# ============================================================107# Bitwise operators108# ============================================================109a, b = 0b1010, 0b1100 # 10, 12110111print(f"a & b = {a & b:04b}") # 1000 (AND)112print(f"a | b = {a | b:04b}") # 1110 (OR)113print(f"a ^ b = {a ^ b:04b}") # 0110 (XOR)114print(f"~a = {~a}") # -11 (NOT — two's complement)115print(f"a << 2 = {a << 2}") # 40 (left shift)116print(f"a >> 1 = {a >> 1}") # 5 (right shift)117118# Practical: using sets with bitwise operators119set_a = {1, 2, 3, 4}120set_b = {3, 4, 5, 6}121print(set_a & set_b) # {3, 4} — intersection122print(set_a | set_b) # {1, 2, 3, 4, 5, 6} — union123print(set_a ^ set_b) # {1, 2, 5, 6} — symmetric difference124print(set_a - set_b) # {1, 2} — difference
🏋️ Practice Exercise
Exercises:
Predict the output without running:
print(3 or 5),print(0 and 5),print("" or "hello" or "world"),print(1 and 2 and 3 and 0 and 5). Then verify.Rewrite these conditions using comparison chaining:
if x >= 0 and x <= 100if a > b and b > c and c > dif grade >= 90 and grade <= 100
Use the walrus operator to simplify: read lines from a string using
io.StringIO, process only lines longer than 5 characters, and collect the processed results.Write a function that takes a dictionary and a key, and returns the value if it exists and is not empty, otherwise returns a default. Use short-circuit evaluation with
or.Demonstrate all set operations using bitwise operators (
&,|,^,-). Create two sets and show intersection, union, symmetric difference, and difference.Explain the difference between
/and//with negative numbers. What is-7 // 2and why?
⚠️ Common Mistakes
Confusing
/(true division, returns float) with//(floor division). In Python 3,7 / 2is3.5, not3. Floor division//rounds toward negative infinity, so-7 // 2is-4, not-3.Using
orfor default values without understanding falsy values:count = user_count or 10will replace0with10, which may not be intended. Usecount = user_count if user_count is not None else 10for None-only checks.Not understanding short-circuit evaluation —
x and expensive_function()won't call the function ifxis falsy. This is a feature, not a bug, and is commonly used for guard clauses.Using
isinstead of==for value comparison.a is bchecks identity (same object), not equality. Only useisforNone,True,False, and sentinel objects.Overusing the walrus operator — it can reduce readability when used in complex expressions. Use it for simple cases like
while (line := f.readline())andif (m := re.match(...)):. Don't nest walrus operators.
💼 Interview Questions
🎤 Mock Interview
Practice a live interview for Operators & Expressions