Remember
An if condition chooses which block runs. Only the selected branch executes; statements after the branch run normally.
An if condition chooses which block runs. Only the selected branch executes; statements after the branch run normally.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
score = 7
if score >= 5:
result = "pass"
else:
result = "try again"
print(result)Top-level scope
7Object 2 · str'pass'An if condition chooses which block runs. Only the selected branch executes; statements after the branch run normally.
(no output)
What do you expect this program to print?
CHECK THE CONDITION
/ \
True False
| |
if branch else branch
\ /
CONTINUE THE PROGRAMChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
score = 7
if score >= 5:
result = "pass"
else:
result = "try again"
print(result)Top-level scope
7Object 2 · str'pass'The example has finished. Compare the final output with the values and references above.
pass
What do you expect this program to print?
score = 7
if score >= 5:
result = "pass"
else:
result = "try again"
print(result)In real life, we make decisions all the time:
Python also lets us make decisions in our code using conditional statements.
In Python, for conditional statements, we use:
The if statement is used to run a block of code only when a given condition is True.
if condition:
# code to run if condition is True
if: The keyword that starts the conditional statement.condition: An expression that evaluates to either True or False.: marks the start of the indented code block.
x = 10
if x > 5:
print("x is greater than 5")
x is greater than 5
The variable x holds the value 10. When Python checks the condition x > 5, it evaluates to True. As a result, the print() statement inside the if block is executed, and the message is shown in the output.

Follow initialization, the condition, the selected branch, and the end of the program.
The if-else statement allows you to define an alternate block of code to run when the condition is False. It provides a clear choice between two paths.
if condition:
# code to run if condition is True
else:
# code to run if condition is False
if: Starts the conditional check.condition: An expression that returns True or False.:: A colon follows the condition and begins the indented block.if): Executes if the condition is True.else:: Specifies the alternate block to run if the condition is False.else): Executes if the condition is False.
age = 16
if age >= 18:
print("You can vote.")
else:
print("You cannot vote.")
You cannot vote.
Explanation: The variable age is assigned the value 16. Python checks the condition age >= 18, which evaluates to False. Therefore, the code inside the else block runs and prints You cannot vote..
Use the if-elif-else structure when you want to test multiple conditions in sequence. Python evaluates each condition in order and executes the block under the first True condition.
if condition1:
# code block 1
elif condition2:
# code block 2
elif condition3:
# code block 3
else:
# default code block if none above are true
if: Checks the first condition. If it’s True, its block is executed and the rest are skipped.elif: Stands for “else if.” It lets you check more conditions if the previous ones are False.else: A fallback that runs if none of the if or elif conditions are true.True condition runs; the rest are ignored.
marks = 75
if marks >= 90:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: F")
Grade: B
Explanation: The value of marks is 75. The first condition (marks >= 90) is false, so Python checks the next one: marks >= 70. This is true, so it prints Grade: B. The rest of the conditions are skipped.
You can also put one if inside another. This is called nesting.
num = 10
if num > 0:
if num % 2 == 0:
print("Positive even number")
Positive even number
Why? First, num > 0 is true. Then, 10 % 2 == 0 (means even), so the inner print runs.
:) after if, elif, and else.==, !=, >, <, >=, <=.Connect what you noticed in the example with the idea below.
An if condition chooses which block runs. Only the selected branch executes; statements after the branch run normally.
Both the if and else blocks execute every time.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.