Conditional branches
Chapter 32 of 132 · Level 2 · Core Python · Python demonstrations with recorded outputs. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Why this concept matters
if/elif/else makes exactly one branch of this decision chain execute. Order matters: the first true condition wins.
Learning goal: Conditional branches. Start by reading what the program does, then inspect its exact output. Where an example illustrates only a prerequisite, study the named advanced feature separately before using it in production.
02 · The important subtopics
Study these connected ideas as you work through the demonstration:
- if elif else
- Nested and combined decisions
- Guard clauses and early exits
- match/case basics and appropriate uses
03 · Pinpoint: read the code line by line
- Line 1
temperature = 17Create, update, or use a value. Notice what values enter this statement and which names it affects. - Line 2
if temperature < 10:Control the next step. Notice what values enter this statement and which names it affects. - Line 3
print('Coat')Display a result. Notice what values enter this statement and which names it affects. - Line 4
elif temperature < 20:Control the next step. Notice what values enter this statement and which names it affects. - Line 5
print('Sweater')Display a result. Notice what values enter this statement and which names it affects. - Line 6
else:Control the next step. Notice what values enter this statement and which names it affects. - Line 7
print('Light clothes')Display a result. Notice what values enter this statement and which names it affects.
Find the literal input values and arguments, follow each operation in execution order, and verify the result below. This habit turns a code listing into a testable explanation.
04 · Runnable Python example and verified output
Conditional branches: working demonstration
Python · runnabletemperature = 17
if temperature < 10:
print('Coat')
elif temperature < 20:
print('Sweater')
else:
print('Light clothes')
Sweater
Why it works: if/elif/else makes exactly one branch of this decision chain execute. Order matters: the first true condition wins.
Worked example 2 · Conditional Branches in practice
Python · executed and checkedscore = 86
if score >= 90: grade = 'A'
elif score >= 80: grade = 'B'
else: grade = 'Keep practicing'
print(grade)
B
Pinpoint learning: This practical conditional branches demonstration uses a small input you can inspect. Run it exactly as shown, compare the displayed result, then change one value and predict how the result will change.
Understand the code line by line
1: score = 86— Store or update the value on the left using the expression on the right.2: if score >= 90: grade = 'A'— Choose which indented branch executes based on a condition.3: elif score >= 80: grade = 'B'— Choose which indented branch executes based on a condition.4: else: grade = 'Keep practicing'— Choose which indented branch executes based on a condition.5: print(grade)— Display the value so you can compare it with the verified output.
05 · Independent practice and answer guidance
Exercise A · Adapt the code
Make a three-way delivery-fee rule based on order total.
Exercise B · Predict
Before pressing Run, predict the first displayed line. Next, change one literal value and record the new observed output. Explain the difference in one clear sentence.
Exercise C · Debug
Make a temporary invalid change, read the exception or failed assertion if one occurs, restore the working code, and explain the repair.
Worked explanation for the original example
if/elif/else makes exactly one branch of this decision chain execute. Order matters: the first true condition wins.
Sweater
06 · Build, verify and advance
Real-world task: Develop a command-based menu for a small shop.
Run the original code; explain the important lines; change one input and verify the new outcome; use a test or recorded result to justify your conclusion.
