Write if, if...else and if...elif...else statements.
π Decision Making
Teach your Python programs to inspect conditions, choose the correct path and respond clearly to different situations.
score = 72
if score >= 75:
grade = "Distinction"
elif score >= 60:
grade = "Good"
else:
grade = "Keep Practising"
print(grade)By the End of This Level
You will be able to design readable and complete decision paths.
Combine comparisons into meaningful conditions.
Use nesting and conditional expressions carefully.
Choose between conditional chains and match...case.
Conditions and Control Flow
A condition is an expression that Python treats as true or false. Control flow decides which statement runs next.
Python evaluates the condition after if. When it is True, the indented block runs. When it is False, Python skips that block and continues with the next available path.
A point where program execution follows one path and skips another according to a condition.
True or FalseThe if Statement
Use a simple if when an action is needed only for the true case.
Free Delivery Check
order_total = 850
if order_total >= 500:
print("Free delivery unlocked")
print("Order checked")Free delivery unlocked Order checked
The colon begins the decision block. Every statement controlled by that if must use consistent indentationβnormally four spaces.
if...else
Use else when exactly one of two mutually exclusive actions must run.
Even or Odd
number = 27
if number % 2 == 0:
print("Even")
else:
print("Odd")Odd
if balance >= price:
print("Purchase approved")Runs only when the condition is true.
else:
print("Insufficient balance")Runs only when the preceding condition is false.
if...elif...else
An elif chain checks conditions from top to bottom and runs only the first matching block.
Performance Band
score = 86
if score >= 90:
band = "Excellent"
elif score >= 75:
band = "Very Good"
elif score >= 60:
band = "Good"
else:
band = "Needs Improvement"
print(band)Very Good
Place the most restrictive or highest range first. If score >= 60 appeared before score >= 90, a score of 95 would stop at the wrong branch.
Nested and Combined Conditions
Use logical operators
Best when all requirements form one clear rule.
if age >= 18 and score >= 60:
print("Eligible")Use an inner decision
Useful when the second test matters only after the first succeeds.
if account_active:
if balance >= price:
print("Approved")Name complex conditions
A descriptive Boolean variable explains the rule.
can_apply = age >= 18 and score >= 60
if can_apply:
print("Eligible")Conditional Expression
A conditional expression chooses between two values. Keep it for short, simple assignments.
Access Label
age = 20
label = "Adult" if age >= 18 else "Minor"
print(label)Adult
true_value if condition else false_value. Use a normal statement when the branches perform several actions or the one-line form becomes difficult to scan.
match...case
Python 3.10+ can match one value or pattern against several cases. The underscore _ acts as the default case.
Menu Command
command = "save"
match command:
case "open":
print("Opening file")
case "save":
print("Saving file")
case "quit":
print("Closing program")
case _:
print("Unknown command")Saving file
| Situation | Prefer | Reason |
|---|---|---|
| Ranges such as marks or age | if...elif | Comparisons express ranges clearly. |
| Exact commands or structured patterns | match...case | Cases keep discrete choices readable. |
| Only two returned values | Conditional expression | Concise when the rule is simple. |
Common Decision-Making Mistakes
if score = 50:Use == to compare; = is assignment.
print("Approved")Indent every controlled statement consistently under its decision header.
if score >= 40Check higher thresholds before lower ones in an ordered elif chain.
if number > 0Include zero and negative paths when the problem requires all possibilities.
π Decision Making β Quick Revision
Review these rules before attempting the quiz and programming problems.
A condition is interpreted as True or False.
if runs its block only for a true condition.
else handles the remaining false path.
elif adds ordered alternatives.
Only the first matching block in an elif chain runs.
Four consistent spaces are the standard indentation.
Use and, or and not to combine rules.
Boundary values reveal missing or overlapping ranges.
A conditional expression chooses between two values.
match...case suits clear discrete patterns.
Level 4 Quick Quiz
Select one answer for every question, then check your score.
π― 5 Programming Problems β Decision Making
Solve every problem in the CodeBhavya pattern: understand the rule, inspect the sample, try the editor, run, check, then compare with the official program.
A problem counts as Solved when its output passes before the official solution is opened.
Read an integer and display whether it is positive, negative or zero.
0Expected output: Zeroelse handle zero.Python Code Editor
Program Output
Run your program to see the output.
Test Case
number = int(input())
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")Read an integer and display whether it is even or odd.
27Expected output: OddPython Code Editor
Program Output
Run your program to see the output.
Test Case
number = int(input())
if number % 2 == 0:
print("Even")
else:
print("Odd")Assign A for 90+, B for 75β89, C for 60β74, D for 40β59 and F otherwise.
86Expected output: Grade = BPython Code Editor
Program Output
Run your program to see the output.
Test Case
mark = int(input())
if mark >= 90:
grade = "A"
elif mark >= 75:
grade = "B"
elif mark >= 60:
grade = "C"
elif mark >= 40:
grade = "D"
else:
grade = "F"
print("Grade =", grade)Read three integers and display the largest value.
18, 42, 31Expected output: Largest = 42Python Code Editor
Program Output
Run your program to see the output.
Test Case
a = int(input())
b = int(input())
c = int(input())
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print("Largest =", largest)A learner is eligible when age is at least 18, percentage is at least 60 and backlogs are zero.
20, 72, 0Expected output: Eligibleand.Python Code Editor
Program Output
Run your program to see the output.
Test Case
age = int(input())
percentage = int(input())
backlogs = int(input())
if age >= 18 and percentage >= 60 and backlogs == 0:
print("Eligible")
else:
print("Not Eligible")π― Make Every Path Complete
Identify every possible outcome.
Place conditions from specific to general.
Run exactly the intended branch.
Verify boundaries and exceptions.
π¬ Decision Making β Visual Flow
Watch Python test score = 72, skip a false branch and select the first true branch.
π How Python Chooses a Branch
1. Read the Current Value
Python stores score = 72 before reaching the decision chain.
π Program Tracing β Eligibility Decision
Move through one statement at a time and observe the condition, chosen branch, variables and output.
Click Next to begin tracing.
β
π€ Decision Making β Interview Questions
Answer aloud before opening each explanation.
What is the difference between independent if statements and an elif chain?
Independent if statements are all tested and several blocks may run. An elif chain stops after its first matching block.
Why does indentation matter in Python decisions?
Indentation defines which statements belong to each branch. Incorrect indentation can change control flow or raise an IndentationError.
Why should higher thresholds appear first in a grade chain?
The chain stops at the first true condition. A lower threshold placed first would capture values that should belong to higher grades.
When is nesting preferable to one combined condition?
Nesting is helpful when the inner question should be evaluated only after an outer requirement succeeds or when each stage needs a separate action.
What are truthy and falsy values?
Truthy values behave like True in a condition. Values such as 0, None and empty collections are falsy.
When should match...case be considered?
Consider it for clear exact-value or structural pattern selection. Range comparisons are usually more readable with if...elif.
π‘ Decision Making β Extra Tips
- 01
Test the values immediately below, at and above every boundary.
- 02
Use descriptive Boolean names such as
is_eligibleandhas_access. - 03
Keep decision blocks short; move repeated work after the chain.
- 04
Prefer a flat
elifchain when deep nesting reduces readability. - 05
Use
infor a small group of accepted exact values. - 06
Add an
elseonly when a genuine default path exists.
βοΈ Decision Making β Extra Practice Questions
- Read a year and decide whether it is a leap year.
- Read three side lengths and decide whether they can form a valid triangle.
- Classify a character as a vowel, consonant, digit or other symbol.
- Calculate an electricity-bill category from a given unit range.
- Display the smallest of four integers without using
min(). - Check whether a date's month number belongs to a 30-day, 31-day or February group.
- Create a login decision using username, password and active-account status.
- Rewrite a two-branch assignment as a conditional expression.
- Build a menu with
match...casefor add, view, edit and quit commands. - Trace a deliberately misordered grade chain, explain the bug and correct it.
Decision Making Complete
When you can design complete branches, handle boundary values and solve all five programs without help, mark this level complete and continue.
