π§ͺ 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 5
Exercise 8 β in or not in?
Write one line each to check:
- Does the string
textcontain the word βpythonβ (case-insensitive)? - Is the number 7 NOT in the list
winners? - Is βemailβ a key in the dict
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 (orname in ("Galvan", "Admin"))if 0 < score < 100:β the== Trueis noise, and the chain was broken
Exercise 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-circuits
Exercise 6 (one path):
x = 1
x += 5 # 6
x *= 6 # 36
Exercise 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 user
Exercise 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]
True
b = 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.