VM-LEARNING /class.xi ·track.cs ·ch-2-4 session: 2026_27
$cd ..

~/Python Operators

root@vm-learning ~ $ open ch-2-4
UNIT 2 ▪ CHAPTER 4
10
Python Operators
Arithmetic · Relational · Logical · Assignment · Identity · Membership
An operator is a symbol (or keyword) that tells Python to perform a specific action on one or more values. The values on which the operator acts are called its operands. In the expression a + b, the + is the operator and a, b are the operands. Python provides seven families of operators — arithmetic, relational, logical, assignment, augmented assignment, identity, and membership — which together give you everything needed to compute, compare, decide and transform data.
Unary vs Binary operators:
  • Unary — one operand, e.g., -x (negation), not flag.
  • Binary — two operands, e.g., a + b, x < y, name in users.
Every operator in this chapter is binary except not and the unary minus.

10.1 The Seven Families of Operators

Classification of Python operators: Python Operators (7 families) Arithmetic math + − * / // % ** Relational compare == != < > <= >= Logical combine bools and or  ·  not Assignment bind value = one symbol only Augmented compound assign +=  −=  *= /= //= %= **= Identity same object? is is not Membership in a collection? in not in Each family answers a different question: "compute", "compare", "combine", "store", "shortcut", "same?", "present?".
Learning Outcome 1: Apply arithmetic and relational operators.

10.2 Arithmetic Operators

Seven symbols — the usual four from school plus three extras that Python borrows from mathematics.

OperatorNameExample (a=13, b=4)Result
+Additiona + b17
Subtractiona − b9
*Multiplicationa * b52
/True division (always returns float)a / b3.25
//Floor / integer divisiona // b3
%Modulus (remainder)a % b1
**Exponent (power)a ** b28561
a, b = 13, 4
print(a + b, a - b, a * b)        # 17 9 52
print(a / b)                       # 3.25   — always a float
print(a // b)                      # 3      — quotient (floor)
print(a % b)                       # 1      — remainder
print(a ** b)                      # 28561  — 13⁴
print(-a)                          # -13    — unary minus
Odd/even testn % 2 == 0 is True for even n. Divisibilityn % k == 0 is True when n is divisible by k. You will use these all through the loops unit.
/ vs //. In Python 3 the plain / always gives a float — even for whole results. If you want integer quotient, use //.
print(10 / 2)      # 5.0     (float)
print(10 // 2)     # 5       (int)
print(-7 // 2)     # -4      (floor division rounds TOWARDS NEGATIVE INFINITY)

10.3 Relational (Comparison) Operators

Compare two values and produce a Boolean — either True or False.

OperatorReads asExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
<less than4 < 9True
>greater than4 > 9False
<=less than or equal to5 <= 5True
>=greater than or equal to5 >= 6False
marks = 72
print(marks >= 33)           # True  — pass
print(marks == 100)          # False
print("abc" < "abd")          # True  — strings compare dictionary-order
print([1, 2] == [1, 2])       # True  — lists compare element-wise
Chained comparisons are allowed in Python — unlike C/C++:
age = 16
print(13 <= age < 20)      # True  — means "13 ≤ age AND age < 20"
Don't confuse = with ==.
  • = is assignment — stores a value.
  • == is comparison — asks "is it equal?".
if x = 10: is a syntax error; you meant if x == 10:.
Learning Outcome 2: Apply logical, assignment and augmented-assignment operators.

10.4 Logical Operators

Combine Boolean values. Python uses the English keywords and, or, not — not the C-style &&, ||, !.

OperatorReturns True when…ExampleResult
andboth operands are TrueTrue and FalseFalse
orat least one operand is TrueTrue or FalseTrue
notthe operand is False (flips it)not TrueFalse
age    = 17
has_id = True

# Is this person allowed to vote AND does she carry ID?
print(age >= 18 and has_id)     # False  — age is 17
print(age >= 18 or  has_id)     # True   — one branch holds
print(not has_id)                # False
Short-circuit evaluation. Python stops as soon as the answer is certain — it does not evaluate the rest.
  • False and anythingFalse (second operand is skipped).
  • True or anythingTrue (second operand is skipped).
This is useful for guards: if x != 0 and 10/x > 1: — the division is never attempted when x == 0.

10.5 Assignment Operator — =

A single = binds a name (L-value) to a value (R-value). Python also allows two neat shortcuts:

FormExampleEffect
Simplex = 10Name x now refers to 10.
Multiple targetsa = b = c = 0All three names refer to the same value 0.
Tuple unpackingx, y = 10, 20Name x gets 10; y gets 20 — in one line.
Swapa, b = b, aSwaps the two values without a temp variable — a Python classic.
x, y = 10, 20
print(x, y)        # 10 20
x, y = y, x
print(x, y)        # 20 10   — swap in one line

10.6 Augmented Assignment Operators

A compact way of saying "take the current value, apply an arithmetic operation, store back". Every arithmetic operator has an augmented form.

AugmentedMeaningEquivalent
x += 5Add 5 to xx = x + 5
x -= 5Subtract 5x = x - 5
x *= 5Multiply by 5x = x * 5
x /= 5True dividex = x / 5
x //= 5Floor dividex = x // 5
x %= 5Modulusx = x % 5
x **= 5Powerx = x ** 5
total = 0
for marks in [85, 72, 91]:
    total += marks           # same as total = total + marks
print(total)                 # 248

count = 1
count *= 2                     # 2
count **= 3                    # 8   (2 ** 3)
Python has no ++ or −− operators (C-style increment/decrement). Use x += 1 or x -= 1.
Learning Outcome 3: Apply identity and membership operators.

10.7 Identity Operators — is & is not

Identity operators ask: "Are the two names pointing to the same object in memory?" — not whether the values happen to be equal.

OperatorTrue when…
isboth operands are the same object (id(a) == id(b))
is notoperands are different objects
a = [1, 2, 3]
b = [1, 2, 3]       # equal in value, but a fresh object
c = a                   # c is the same object as a (alias)

print(a == b)             # True   — values match
print(a is b)             # False  — different objects in memory
print(a is c)             # True   — same object
print(a is not b)         # True
When to use is. Almost always only with the sentinels None, True, False. For ordinary value comparison use ==.
if result is None:   # ✓ the idiomatic way
    ...
if x is 1000:         # ❌ unreliable — do not rely on int identity
    ...

10.8 Membership Operators — in & not in

Ask whether a value appears inside a sequence or collection — a string, list, tuple, dictionary (its keys), or any iterable. Returns a Boolean.

OperatorMeaning
inTrue if the value is found in the sequence
not inTrue if the value is not found
vowels = "aeiou"
print("e" in vowels)            # True
print("z" not in vowels)        # True

marks = [85, 72, 91, 68]
print(72 in marks)              # True
print(100 not in marks)          # True

student = {"name": "Aarav", "roll": 22}
print("name" in student)        # True   — checks KEYS of a dict
print("Aarav" in student)       # False  — not a key
Membership is extremely handy for input validation:
choice = input("y/n? ").lower()
if choice in ("y", "yes"):
    print("Confirmed")
elif choice in ("n", "no"):
    print("Cancelled")
else:
    print("Please type y or n")

10.9 The Whole Menu — One-page Cheat Sheet

FamilyOperatorsReturns
Arithmetic+   −   *   /   //   %   **Number
Relational==   !=   <   >   <=   >=Boolean
Logicaland   or   notBoolean
Assignment=
Augmented+=   −=   *=   /=   //=   %=   **=
Identityis   is notBoolean
Membershipin   not inBoolean

10.9.1 Operator Precedence (preview)

When several operators appear in one expression, Python evaluates them in a fixed order. A detailed table is covered in Chapter 11 — Expressions, but here is the rough picture, highest precedence first:

1.   **  (exponent)
2.   +x, -x  (unary)
3.   *   /   //   %
4.   +   −
5.   <   <=   >   >=   ==   !=  (relational)
6.   is   is not   in   not in
7.   not
8.   and
9.   or
10. =   +=   −=   …  (assignment, lowest)
When in doubt, use parentheses — they cost nothing and make intent explicit.
print(2 + 3 * 4)            # 14  (not 20)
print((2 + 3) * 4)          # 20

📌 Quick Revision — Chapter 10 at a Glance

  • Operator = symbol or keyword that acts on one or more operands. Unary = 1 operand; binary = 2 operands.
  • 7 families: Arithmetic · Relational · Logical · Assignment · Augmented · Identity · Membership.
  • Arithmetic: + − * / // % **. Remember / always gives a float; // gives integer quotient.
  • Relational: == != < > <= >= → returns Boolean. Chained comparisons work: 0 < x < 10.
  • Logical: and · or · not (English keywords, not &&/||). Short-circuit evaluation — second operand skipped when result is already known.
  • Assignment = — binds name to value. Multiple targets a = b = 0; tuple unpacking x, y = 10, 20; swap a, b = b, a.
  • Augmented += −= *= /= //= %= **= — compact form of x = x op value.
  • Python has no ++/−− — use x += 1.
  • Identity is / is not — asks if two names point to the same memory object (same id()). Use only for None, True, False.
  • Membership in / not in — asks if a value appears in a sequence / collection. For dicts it tests keys.
  • Precedence (rough): ** → unary -* / // %+ - → comparison → notandor → assignment. Use parentheses to be safe.
🧠Practice Quiz — test yourself on this chapter