๐งช Practice: Operators
Ten quick-fire exercises covering all five operator families from Module 4. Solve in practice4.py, solutions at the bottom.
Exercise 1 โ The digit extractor
Ask for a 4-digit number. Print each digit on its own line using only // and %.
Exercise 2 โ Even machine
Print all even numbers from 1 to 20 using % (one loop, no hardcoding).
Exercise 3 โ Fix the conditions
Each condition has a bug. Fix them:
1. if age = 18:
2. if name == "Galvan" or "Admin":
3. if 0 < score and score < 100 == True:Exercise 4 โ The login gate
Ask for username and password. Allow entry only if username is "admin" AND password is "open123" (case-insensitive for username).
Exercise 5 โ Truth table by hand
Predict, then verify:
print(True or False and False)
print((True or False) and False)
print(not True or True)
print(5 > 3 and 2 > 4)
print(10 % 2 == 0 or 10 % 2 == 1)Exercise 6 โ Accumulate
Using only += and *=, turn x = 1 into 36 in exactly three statements (x should become 6, then 12, then 36 โ wait, that's wrong: plan your own three steps to land on 36).
Exercise 7 โ The swap trick
Using only += and -= (no third variable, no tuple swap), swap a and b:
a = 5
b = 10
# your two lines here
print(a, b) # 10 5Exercise 8 โ in or not in?
Write one line each to check:
text contain the word "python" (case-insensitive)?winners?user?Exercise 9 โ FizzBuzz preview
Print numbers 1 to 15. For multiples of 3 print "Fizz" instead, for multiples of 5 print "Buzz", for multiples of both print "FizzBuzz". (Uses % โ the most famous interview warmup ever.)
Exercise 10 โ Predict, then run
a = [1, 2, 3]
b = a
b += [4]
print(a)
print(a is b)Explain both outputs.
Solutions
Exercise 1:
num = int(input("4-digit number: "))
print(num // 1000)
print(num // 100 % 10)
print(num // 10 % 10)
print(num % 10)Exercise 2:
for n in range(1, 21):
if n % 2 == 0:
print(n)Exercise 3:
if age == 18: โ comparison, not assignmentif name == "Galvan" or name == "Admin": โ each side needs the variable (or name in ("Galvan", "Admin"))if 0 < score < 100: โ the == True is noise, and the chain was brokenExercise 4:
user = input("Username: ").lower()
pwd = input("Password: ")
if user == "admin" and pwd == "open123":
print("Welcome, admin!")
else:
print("Access denied")Exercise 5:
True # and binds tighter: True or (False and False)
False # parentheses change it: True and False
True # not True = False, False or True = True
False # True and False
True # 10 % 2 is 0, so the left side is True โ short-circuitsExercise 6 (one path):
x = 1
x += 5 # 6
x *= 6 # 36Exercise 7:
a = a + b # a=15, b=10
b = a - b # b=5
a = a - b # a=10(That's three lines โ the honest answer is you need three operations without a temp variable; the tuple swap a, b = b, a is the real Pythonic one-liner.)
Exercise 8:
1. "python" in text.lower()
2. 7 not in winners
3. "email" in userExercise 9:
for n in range(1, 16):
if n % 15 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)(Check multiples of 15 FIRST โ otherwise Fizz catches them early.)
Exercise 10:
[1, 2, 3, 4]
Trueb = a makes b point at the SAME list as a (no copy!). b += [4] extends that shared list, so printing a shows the change too โ and a is b confirms they are one object. This is your first taste of references โ a big idea arriving early, courtesy of is.
โ Module 4 Checkpoint
All ten attempted? Module 4 complete โ every operator family in Python is now yours.
Next module: Data Structures โ lists, tuples, sets, and dictionaries. The containers that hold everything.