Practice: Introduction to Conditional Statements
Q1. Is the number even?
Given `n = 14`, print True if n is even and False otherwise, using a condition (no if statement).
Goal: Build a condition.
Q2. Between two limits
Given `x = 7`, print whether x is greater than 1 and less than 10, using a chained comparison.
Goal: Use chained comparisons.
Q3. Can vote
Given `age = 19` and `citizen = True`, print whether the person can vote (age at least 18 and a citizen).
Goal: Use and.
Q4. Special age
Given `age = 70`, print True if age is below 13 or above 60.
Goal: Use or.
Q5. Truthiness table
Print the Boolean value of the empty string, the string `"0"` and the number 0, each on its own line.
Goal: Know truthy and falsy values.
Q6. Safe division test
Given `d = 0`, print `10 / d > 1` only safely: use a condition that gives False without an error when d is zero.
Goal: Use short-circuit evaluation.
Q7. What does this print? (comparisons)
Predict the output.
print(5 > 3, 5 == 3, 5 != 3)Goal: Read comparisons.
Q8. What does this print? (not)
Predict the output.
print(not (4 > 9))Goal: Use not.
Q9. What does this print? (mixed logic)
Predict the output.
print(True or False and False)Goal: Know operator priority.
Q10. What does this print? (and returns)
Predict the output.
print(0 and 5)
print(3 and 5)Goal: Understand what and returns.
Q11. What does this print? (or returns)
Predict the output.
print("" or "default")
print("hi" or "default")Goal: Understand what or returns.
Q12. What does this print? (bool list)
Predict the output.
print([bool(x) for x in [0, 1, "", "a", [], None]])Goal: Check truthy values.
Q13. What does this print? (strings)
Predict the output.
print("5" == 5, int("5") == 5)Goal: Mind types in comparison.
Q14. Fix the assignment bug
The code should print True if x equals 3 but it causes a syntax error. Fix it.
x = 3
print(x = 3)Goal: Use == for comparison.
Q15. Fix the lowercase boolean
The code crashes with a NameError. Fix it.
is_ready = true
print(is_ready)Goal: Spell Boolean values correctly.
Q16. Fix the or test
The code should print True when x is 1 or 2, but it prints True for every x. For x = 5 it must print False. Fix it.
x = 5
print(x == 1 or 2)Goal: Write complete conditions.
Q17. Why decisions matter
In your own words, say why every useful program needs conditional statements.
Goal: Explain control flow.
Q18. = versus ==
Explain the difference between `=` and `==`.
Goal: Tell assignment from comparison.
Q19. Ticket price check
A cinema gives a cheap ticket if the age is below 12 or the person is a student. Given `age = 25` and `student = True`, print whether the ticket is cheap.
Goal: Combine conditions in a real rule.
Q20. Valid password length
Given `pw = "secret99"`, print True if the length is between 8 and 12 (inclusive) and the password is not all digits.
Goal: Validate data with conditions.