1. Introduction
Operators are special symbols in Python that perform operations on values and variables, called operands. Python groups operators into several categories — arithmetic, comparison, logical, assignment, bitwise, identity, and membership — each serving a different purpose, from doing math to checking whether an item exists in a collection.
Cricket analogy: Just as cricket has different kinds of deliveries -- yorkers, bouncers, googlies -- each serving a purpose, Python groups operators into arithmetic, comparison, logical, assignment, bitwise, identity, and membership categories, each doing a distinct job on the operands (runs, overs, players).
Knowing which operator to use, and how Python evaluates expressions containing several operators (operator precedence), is fundamental to writing correct conditions and calculations.
Cricket analogy: Knowing whether to check the required run rate before or after factoring in overs remaining is like operator precedence -- picking the right operator and evaluation order is essential to correctly judge a chase.
2. Syntax
a + b, a - b, a * b, a / b, a // b, a % b, a ** b # arithmetic
a == b, a != b, a > b, a < b, a >= b, a <= b # comparison
a and b, a or b, not a # logical
a += 1, a -= 1, a *= 2 # assignment
a & b, a | b, a ^ b, ~a, a << 1, a >> 1 # bitwise
a is b, a is not b # identity
a in b, a not in b # membership3. Explanation
Arithmetic and Comparison Operators
Arithmetic operators perform math: / always returns a float (true division), while // performs floor division, discarding the remainder. Comparison operators return a boolean and are commonly used in conditions like if statements.
Cricket analogy: A required-run-rate calculation like runs / overs always returns a float true division for precision, while balls // 6 (floor division) tells you the completed over count, discarding the remainder; comparison operators like score > target return booleans used in match-result conditions.
Logical and Assignment Operators
and, or, and not combine boolean expressions using short-circuit evaluation — a or b stops and returns a if a is truthy, without evaluating b. Augmented assignment operators like += combine an operation with assignment in one step.
Cricket analogy: 'chasing' or 'defending' short-circuits and returns 'chasing' without even checking 'defending' if the team is already batting second (truthy); a runs += 4 augmented assignment adds a boundary to the score in one step.
Bitwise, Identity, and Membership Operators
Bitwise operators (&, |, ^, ~, <<, >>) act on the binary representation of integers. is/is not check whether two names refer to the exact same object in memory (identity), which is different from == (equality of value). in/not in test membership within a sequence or collection.
Cricket analogy: Checking DRS review flags with & and | acts on the binary bits of a decision code; player_a is player_b checks if two variables point to the exact same person on the field (identity), unlike == which just checks matching stats; 'Kohli' in playing_xi tests membership in the lineup.
Operator precedence follows PEMDAS-like rules: ** binds tighter than unary -, which binds tighter than *, /, //, %, which bind tighter than +, -, which bind tighter than comparisons, which bind tighter than not, and, or. Use parentheses to make intent explicit.
Common gotcha: use == to compare values and is to compare identity. [1, 2] == [1, 2] is True (same value) but [1, 2] is [1, 2] is False (two different list objects in memory).
4. Example
a, b = 13, 4
print("Addition:", a + b)
print("Floor Division:", a // b)
print("True Division:", a / b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
print(a > b, a == b, a != b)
x = True
y = False
print(x and y, x or y, not x)
print(5 & 3, 5 | 3, 5 ^ 3, ~5, 5 << 1, 5 >> 1)
lst1 = [1, 2, 3]
lst2 = [1, 2, 3]
print(lst1 == lst2, lst1 is lst2)
nums = [1, 2, 3]
print(2 in nums, 5 not in nums)5. Output
Addition: 17
Floor Division: 3
True Division: 3.25
Modulus: 1
Exponent: 28561
True False True
False True False
1 7 6 -6 10 2
True False
True True6. Key Takeaways
- Arithmetic
/always returns a float;//returns a floored result of the input types. - and/or/not use short-circuit evaluation and can return operand values, not just booleans.
- Augmented assignment operators (+=, -=, *=) combine an operation and assignment.
- Bitwise operators act on the binary digits of integers.
- == compares values; is compares object identity.
- in / not in test membership within sequences, strings, and other collections.
Practice what you learned
1. What does the expression `13 // 4` evaluate to?
2. What is the key difference between `==` and `is` in Python?
3. What does `x or y` return if `x` is a non-empty string and `y` is 0?
4. Evaluate `[1, 2] == [1, 2]` and `[1, 2] is [1, 2]`.
5. What does the modulus operator `%` return for `13 % 4`?
6. Which operator has the highest precedence in Python among the following?
Was this page helpful?
You May Also Like
Variables in Python
Learn how Python variables work as dynamically-typed name bindings, how to create and reassign them, and the rules for naming them correctly.
Data Types in Python
A tour of Python's built-in data types — numeric, sequence, mapping, set, boolean, and NoneType — and how mutability affects each.
if-elif-else in Python
Learn how Python's if, elif, and else statements let your program make decisions based on conditions.
Keywords and Identifiers in Python
The difference between Python's reserved keywords and programmer-defined identifiers, plus the naming rules that make an identifier valid.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics