Skip to main content

Learning outcomes

By the end of this chapter, you should be able to:
  • use assignment and augmented assignment operators
  • evaluate multiple/chained assignment correctly
  • distinguish identity and equality
  • use membership operators with strings/lists
  • apply bitwise operators at basic level

30-minute recording plan

  • 0-8 min: assignment and augmented assignment
  • 8-14 min: multiple assignment and chained assignment
  • 14-19 min: identity vs equality
  • 19-24 min: membership operators
  • 24-30 min: bitwise basics and exam traps

Assignment operators

x = 10
x += 5   # x = x + 5
x *= 2
print(x)
Common forms: +=, -=, *=, /=, //=, %=

Multiple assignment and unpacking

Python can assign multiple variables in one line:
a, b, c = 10, 20, 30
This means:
a = 10
b = 20
c = 30
If counts do not match, Python raises ValueError.
# a, b = 1, 2, 3  # ValueError: too many values to unpack

Chained assignment (MCQ-critical)

x = y = z = 5
Meaning:
  • right-hand side expression is evaluated once (5)
  • that value is assigned to all targets (x, y, z)
It is assignment, not comparison. Do not confuse with x == y == z.

Quiz-style solved snippet

x, y, z = a, b, c
x = y = z
Step-by-step:
  1. After line 1: x = a, y = b, z = c
  2. In line 2, RHS is current value of z (which is c)
  3. That RHS value is assigned to x and y as well
Final state:
  • x = c
  • y = c
  • z = c
Concrete example:
a, b, c = 2, 7, 9
x, y, z = a, b, c
x = y = z
print(x, y, z)  # 9 9 9

Mutable object trap

x = y = []
x.append(10)
print(x, y)  # [10] [10]
Both names refer to the same list object in this case. For independent lists, assign separately.

Identity operators

is, is not
a = [1, 2]
b = [1, 2]
print(a == b)  # True (same value)
print(a is b)  # False (different objects)
Use is mainly for identity checks (for example value is None).

Beginner alert: == vs is rule

Use this rule in beginner code and exams:
  • Use == for value comparison (almost always)
  • Use is mainly with None
a = [1, 2]
b = [1, 2]
print(a == b)  # True (same content)
print(a is b)  # False (different objects)
Avoid writing checks like x is 5 or name is "Aarav".

Membership operators

in, not in
text = "python"
print("py" in text)
print("z" not in text)
Works for strings, lists, tuples, sets, and dictionary keys.

Bitwise operators (intro)

&, |, ^, ~, <<, >>
a = 5   # 0101
b = 3   # 0011
print(a & b)  # 1
print(a | b)  # 7
Bitwise operations are based on binary representation of integers. Treat this as an advanced-but-important exam topic: first master arithmetic/logical operators, then bitwise.

Conditional expression (ternary)

age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)

Mini showcase: simple permission flags

READ = 4    # 100
WRITE = 2   # 010
EXEC = 1    # 001

user_perm = READ | WRITE        # 110

print("Can read?", bool(user_perm & READ))
print("Can write?", bool(user_perm & WRITE))
print("Can execute?", bool(user_perm & EXEC))
This is a practical use of | and & with compact storage.

Exam-focused points

  • == checks value equality
  • is checks object identity
  • membership is very common in string/list questions
  • know at least one solved bitwise example
  • in chained assignment, RHS is evaluated once and assigned to all targets
  • x = y = [] creates one shared list object (aliasing trap)

Practice questions

  1. Show output:
x = 8
x >>= 1
print(x)
  1. Predict final values:
a, b, c = 1, 3, 5
x, y, z = a, b, c
x = y = z
  1. Write program to check if a character is a vowel using in.
  2. Demonstrate difference between == and is with list example.