100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Operators in Python

An overview of Python's arithmetic, comparison, logical, assignment, bitwise, identity, and membership operators with precedence rules.

Basics & Data TypesBeginner10 min readJul 7, 2026
Analogies

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

python
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                                  # membership

3. 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

python
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

text
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 True

6. 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

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#OperatorsInPython#Operators#Syntax#Explanation#Arithmetic#StudyNotes#SkillVeris