Python B02 · Lesson 2 of 5

Operators

Arithmetic · Assignment · Comparison · Logical · Bitwise · Membership · Identity

Operators are keywords or symbols used to perform operations on values or variables. Your file (operators.py) covers 7 types — let's go through each one.

1. Arithmetic Operators

Used for mathematical calculations on numbers.

SymbolNameExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication4 * 312
/Division7 / 23.5 (float)
//Floor Division7 // 23 (int, rounds down)
%Modulus7 % 21 (remainder)
**Exponent2 ** 38 (2³)
Key distinction
/ always returns a float. // returns an int (floor). So 7/2 = 3.5 but 7//2 = 3.
print(10 + 3)    # 13
print(10 - 3)    # 7
print(10 * 3)    # 30
print(10 / 3)    # 3.3333... (float)
print(10 // 3)   # 3 (floor division)
print(10 % 3)    # 1 (remainder)
print(2 ** 10)   # 1024

2. Assignment Operators

Used to assign values to variables. Shorthand operators combine assignment with an arithmetic operation.

OperatorNameEquivalent toExample
=Assignx = 10
+=Add & assignx = x + 5x += 5
-=Subtract & assignx = x - 3x -= 3
*=Multiply & assignx = x * 2x *= 2
/=Divide & assignx = x / 2x /= 2
//=Floor divide & assignx = x // 2x //= 2
%=Modulus & assignx = x % 3x %= 3
**=Exponent & assignx = x ** 2x **= 2
x = 10
x += 5   # x is now 15
x *= 2   # x is now 30
x -= 10  # x is now 20
print(x)  # 20

3. Comparison Operators

Compare two values and return a Boolean (True or False).

OperatorNameExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than7 > 5True
<Less than3 < 5True
>=Greater than or equal5 >= 5True
<=Less than or equal4 <= 5True
Common mistake
= is assignment (stores a value). == is comparison (checks equality). Writing if x = 5: is a syntax error in Python.

4. Logical Operators

Combine multiple Boolean conditions. Returns a Boolean value.

OperatorMeaningReturns True when…
andLogical ANDBoth conditions are True
orLogical ORAt least one condition is True
notLogical NOTCondition is False (reverses it)
age = 20
has_id = True

print(age >= 18 and has_id)   # True — both true
print(age < 18 or has_id)     # True — one is true
print(not has_id)               # False — reverses True

5. Bitwise Operators

Operate on individual bits of integer values. Used in low-level programming.

OperatorNameRuleExample (a=5, b=3)
&Bitwise AND1 if both bits are 15 & 3 = 1
|Bitwise OR0 if both bits are 05 | 3 = 7
^Bitwise XOR1 if bits differ5 ^ 3 = 6
~Bitwise NOT~a = -(a+1)~5 = -6
<<Left shiftShifts bits left (×2 per shift)5 << 1 = 10
>>Right shiftShifts bits right (÷2 per shift)5 >> 1 = 2
# 5 in binary: 101 | 3 in binary: 011
print(5 & 3)   # 001 → 1
print(5 | 3)   # 111 → 7
print(5 ^ 3)   # 110 → 6
print(~5)      # -(5+1) → -6
print(5 << 1)  # 1010 → 10
print(5 >> 1)  # 10  → 2

6. Membership Operators

Check if a value exists in a sequence (string, list, etc.). Returns a Boolean.

OperatorReturns True when…Example
inValue IS in the sequence"a" in "apple"True
not inValue is NOT in the sequence"z" not in "apple"True
word = "Python"
print("P" in word)       # True
print("z" in word)       # False
print("x" not in word)   # True

7. Identity Operators

Check if two variables refer to the same object in memory. Returns a Boolean.

OperatorReturns True when…
isBoth variables point to the same memory object
is notThey point to different memory objects
a = None
print(a is None)      # True — 'is' is the correct way to check None

x = [1, 2]
y = [1, 2]
print(x == y)        # True — same values
print(x is y)        # False — different objects in memory

Key Differences (from your notes)

ComparisonDifference
= vs === assigns a value; == compares two values
== vs is== compares values; is compares memory identity
== vs in== checks exact equality; in checks membership in a collection
in vs isin checks membership; is checks memory identity

🛠 Drills — Practice Time

Drill 1 Arithmetic Calculator
Ask the user for two numbers. Print the result of all 7 arithmetic operations on them (label each one).
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print("Addition:",        a + b)
print("Subtraction:",     a - b)
print("Multiplication:",  a * b)
print("Division:",        a / b)
print("Floor Division:",  a // b)
print("Modulus:",         a % b)
print("Exponent:",        a ** b)
Drill 2 Shorthand Counter
Start with score = 100. Then: add 50, multiply by 2, subtract 30, floor-divide by 5. Print the final value. Use only shorthand assignment operators.
score = 100
score += 50    # 150
score *= 2     # 300
score -= 30    # 270
score //= 5   # 54
print(score)   # 54
Drill 3 Even or Odd?
Ask the user for a number. Use the modulus operator to check if it's even or odd. Print True if even, False if odd.
Hint: A number is even if number % 2 == 0.
number = int(input("Enter a number: "))
print(number % 2 == 0)  # True if even, False if odd
Drill 4 Membership Check
Ask the user for a letter. Check if it is a vowel using the in operator with the string "aeiouAEIOU". Print the result.
letter = input("Enter a letter: ")
print(letter in "aeiouAEIOU")  # True if vowel
Drill 5 Predict the Output
Without running the code, predict the output of each line. Then reveal to check:
print(10 == 10)
print(10 is 10)
print("py" in "python")
print(5 != 5)
print(not True)
print(10 > 5 and 3 < 1)
print(10 == 10)            # True
print(10 is 10)            # True (small ints are cached)
print("py" in "python")   # True
print(5 != 5)              # False
print(not True)            # False
print(10 > 5 and 3 < 1)  # False (3<1 is False)

📝 Quiz — 7 Questions

Score: 0 / 7
1 What does 7 // 2 return?
2 What is the result of 10 % 3?
3 After x = 5; x += 3, what is x?
4 What does 10 > 5 and 3 < 1 evaluate to?
5 a = [1,2] and b = [1,2]. Which statement is correct?
6 What does "o" in "Python" return?
7 What is the value of ~5?
← Lesson 1: Basics Next: Lesson 3 — Conditionals →