Python if Statement Practice Questions for Beginners
Chapter 8 Recap and Practice
Your programs can now make decisions. Here is the chapter in one table:
| You write | It does |
|---|---|
if age >= 18: | runs the indented block only when the condition is True |
| 4 spaces | the indentation that marks a block; it must line up exactly |
else: | the other path, when the condition is False |
elif marks >= 75: | another condition; only the first True one runs |
a and b | True only when both are True |
a or b | True when at least one is True; write both comparisons in full |
not a | flips True and False |
(a and b) or c | brackets show which parts go together |
an if inside an if | a nested if: a second question after a yes |
if b != 0: | check input before you use it |
And the checklist when an if misbehaves: == not =, the colon, the indentation, numbers converted with int(), and the order of your elifs.
Practice questions
Use input() with no prompt text. The tests try several inputs, including the edges (like exactly 18, or exactly 100), so test your program with those too.
The if statement
- 59.Python Program to Check If a Number Is PositiveEasySolve
- 60.Python Program to Apply a Discount on Orders Over 1000EasySolve
else and elif
- 61.Python Program to Check If a Number Is Even or OddEasySolve
- 62.Python Program to Find the Larger of Two NumbersEasySolve
- 63.Python Program to Calculate a Grade from MarksMediumSolve
and, or, not
- 64.Python Program to Check If a Number Is Between 1 and 100EasySolve
- 65.Python Program to Check If a Day Is a WeekendEasySolve
- 66.Python Program to Check If a Year Is a Leap YearMediumSolve
Nested if and checking input
- 67.Python Program to Check a Username and PasswordMediumSolve
- 68.Python Program to Divide Two Numbers SafelyEasySolve
- 69.Python Program to Make a Simple ATMHardSolve
Now your programs can choose. Next, they'll learn to remember many values at once: lists.