Python Lesson 7: Operators
Operators are symbols that perform operations on values. You will use them constantly in every Python program.
Arithmetic Operators
a = 10
b = 3
print(a + b) # 13 — addition
print(a - b) # 7 — subtraction
print(a * b) # 30 — multiplication
print(a / b) # 3.333... — division (always float)
print(a // b) # 3 — floor division (whole number only)
print(a % b) # 1 — modulo (remainder)
print(a ** b) # 1000 — exponentiation (10 to the power 3)
Comparison Operators (return True or False)
x = 5
y = 10
print(x == y) # False — equal
print(x != y) # True — not equal
print(x < y) # True — less than
print(x > y) # False — greater than
print(x <= 5) # True — less than or equal
print(x >= 10) # False — greater than or equal
Logical Operators
age = 20
has_id = True
# and — both conditions must be True
print(age >= 18 and has_id) # True
# or — at least one must be True
print(age < 18 or has_id) # True
# not — reverses True/False
print(not has_id) # False
Assignment Shortcuts
score = 100
score += 10 # score = score + 10 -> 110
score -= 5 # score = score - 5 -> 105
score *= 2 # score = score * 2 -> 210
score //= 3 # score = score // 3 -> 70
print(score) # 70
🏋️ Practice Task
Build a tip calculator. Ask user for: bill amount and tip percentage. Print: tip amount, total bill, and split amount for 2 people. Format all money with 2 decimal places.
💡 Hint: tip = bill * (percent / 100). Use :.2f in f-strings. split = (bill + tip) / 2