Practice: Introduction to Conditional Statements

« Read the chapter

Q1. Is the number even?

BeginnerCoding

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

BeginnerCoding

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

BeginnerCoding

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

IntermediateCoding

Given `age = 70`, print True if age is below 13 or above 60.

Goal: Use or.

Q5. Truthiness table

IntermediateCoding

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

AdvancedCoding

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)

BeginnerOutput

Predict the output.

Code
print(5 > 3, 5 == 3, 5 != 3)

Goal: Read comparisons.

Q8. What does this print? (not)

BeginnerOutput

Predict the output.

Code
print(not (4 > 9))

Goal: Use not.

Q9. What does this print? (mixed logic)

IntermediateOutput

Predict the output.

Code
print(True or False and False)

Goal: Know operator priority.

Q10. What does this print? (and returns)

AdvancedOutput

Predict the output.

Code
print(0 and 5)
print(3 and 5)

Goal: Understand what and returns.

Q11. What does this print? (or returns)

AdvancedOutput

Predict the output.

Code
print("" or "default")
print("hi" or "default")

Goal: Understand what or returns.

Q12. What does this print? (bool list)

IntermediateOutput

Predict the output.

Code
print([bool(x) for x in [0, 1, "", "a", [], None]])

Goal: Check truthy values.

Q13. What does this print? (strings)

IntermediateOutput

Predict the output.

Code
print("5" == 5, int("5") == 5)

Goal: Mind types in comparison.

Q14. Fix the assignment bug

BeginnerDebugging

The code should print True if x equals 3 but it causes a syntax error. Fix it.

Code
x = 3
print(x = 3)

Goal: Use == for comparison.

Q15. Fix the lowercase boolean

BeginnerDebugging

The code crashes with a NameError. Fix it.

Code
is_ready = true
print(is_ready)

Goal: Spell Boolean values correctly.

Q16. Fix the or test

IntermediateDebugging

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.

Code
x = 5
print(x == 1 or 2)

Goal: Write complete conditions.

Q17. Why decisions matter

BeginnerConceptual

In your own words, say why every useful program needs conditional statements.

Goal: Explain control flow.

Q18. = versus ==

BeginnerConceptual

Explain the difference between `=` and `==`.

Goal: Tell assignment from comparison.

Q19. Ticket price check

IntermediateApplication

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

AdvancedApplication

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.