Python Basics — Operators Exercises
Practice Exercises — Operators
Test your understanding of arithmetic, comparison, logical, identity, and membership operators.
Exercise 1: Time Converter
Write a program that converts 7384 seconds into hours, minutes, and seconds. Use // and %.
Show Answer
Answer:
total_seconds = 7384
hours = total_seconds // 3600
remaining = total_seconds % 3600
minutes = remaining // 60
seconds = remaining % 60
print(f"{total_seconds} seconds = {hours}h {minutes}m {seconds}s")
Expected output:
7384 seconds = 2h 3m 4s
Alternative using divmod():
total_seconds = 7384
hours, remaining = divmod(total_seconds, 3600)
minutes, seconds = divmod(remaining, 60)
print(f"{total_seconds} seconds = {hours}h {minutes}m {seconds}s")
Exercise 2: Leap Year Checker
Write a condition that checks if a year is a leap year. A year is a leap year if:
- It’s divisible by 4 AND
- NOT divisible by 100, UNLESS also divisible by 400
Test with years: 2024, 1900, 2000.
Show Answer
Answer:
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print(is_leap_year(2024)) # True
print(is_leap_year(1900)) # False (divisible by 100 but not 400)
print(is_leap_year(2000)) # True (divisible by 400)
print(is_leap_year(2023)) # False
Expected output:
True
False
True
False
Key concepts: Combines % (modulo), == (comparison), and/or (logical operators).
Exercise 3: is vs ==
Predict the output of each expression, then verify:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a == b)
print(a is b)
print(a == c)
print(a is c)
print(a is not c)
Show Answer
Answer:
a = [1, 2, 3]
b = a # b references the same object as a
c = [1, 2, 3] # c is a new list with same content
print(a == b) # True — same content
print(a is b) # True — same object (b = a)
print(a == c) # True — same content
print(a is c) # False — different objects in memory
print(a is not c) # True — they are NOT the same object
Key takeaway: == compares values, is compares identity (memory address). Two lists can be equal (==) but not identical (is).
Exercise 4: Membership Check
Write a program that checks if a user’s input is a valid day of the week. Use the in operator.
Show Answer
Answer:
valid_days = {"monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday"}
user_input = "Wednesday"
if user_input.lower() in valid_days:
print(f"'{user_input}' is a valid day.")
else:
print(f"'{user_input}' is NOT a valid day.")
Expected output:
'Wednesday' is a valid day.
Key concepts:
- Used a set for O(1) fast lookup
- Used
.lower()for case-insensitive comparison - The
inoperator checks membership in the set
Exercise 5: Operator Precedence
Without running the code, predict the result of each expression:
a = 2 + 3 * 4
b = (2 + 3) * 4
c = 2 ** 3 ** 2
d = True or False and False
e = not True or True
Show Answer
Answer:
a = 2 + 3 * 4
print(f"a = {a}") # a = 14 (3*4=12, then 2+12=14)
b = (2 + 3) * 4
print(f"b = {b}") # b = 20 ((2+3)=5, then 5*4=20)
c = 2 ** 3 ** 2
print(f"c = {c}") # c = 512 (3**2=9, then 2**9=512; ** is right-associative)
d = True or False and False
print(f"d = {d}") # d = True (and before or: False and False = False, then True or False = True)
e = not True or True
print(f"e = {e}") # e = True (not True = False, then False or True = True)
Expected output:
a = 14
b = 20
c = 512
d = True
e = True
Key concepts:
*before+**is right-associative:2 ** 3 ** 2=2 ** (3 ** 2)- Logical precedence:
not>and>or
Scoring Guide
| Score | Level |
|---|---|
| 5/5 | Excellent — Operators fully mastered |
| 4/5 | Great — Solid understanding |
| 3/5 | Good — Review identity operators and precedence |
| 2/5 | Needs Work — Re-read the operators chapter |
| 1/5 | Keep Going — Practice arithmetic and comparison first |
Next chapter: Input & Output →