Python Conditional Statements

Let’s look at conditional statements

  1. Notice

    An if condition chooses which block runs. Only the selected branch executes; statements after the branch run normally.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
score = 7
if score >= 5:
    result = "pass"
else:
    result = "try again"
print(result)
Live state

Top-level scope

score→Object 1
result→Object 2
Object 1 · int7Object 2 · str'pass'
This step

An if condition chooses which block runs. Only the selected branch executes; statements after the branch run normally.

A condition chooses the path

Only the selected branch runs. After that branch, the program continues.

Image brief

Alt text
A condition splits execution into true and false branches, then the paths meet before the following instructions.
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Conditional Statements". Teaching focus: Only the selected branch runs. After that branch, the program continues. Depict exactly the relationships and labels in this reference diagram: CHECK THE CONDITION / \ True False | | if branch else branch \ / CONTINUE THE PROGRAM Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A condition splits execution into true and false branches, then the paths meet before the following instructions.

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
score = 7
if score >= 5:
    result = "pass"
else:
    result = "try again"
print(result)
Live state

Top-level scope

score→Object 1
result→Object 2
Object 1 · int7Object 2 · str'pass'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
pass

What do you expect this program to print?

What does this example display?

score = 7
if score >= 5:
    result = "pass"
else:
    result = "try again"
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

pass

Follow the printed values in order. The output is: pass

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

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.

What are Conditional Statements?

In real life, we make decisions all the time:

  • If it’s raining, take an umbrella.
  • If you're hungry, eat something.
  • Otherwise, continue your day.

Python also lets us make decisions in our code using conditional statements.

In Python, for conditional statements, we use:

  • if – is used check a condition
  • elif – is used to check another condition if the first one was false
  • else – is used to do specific operations if nothing above is true

1. if Statement

The if statement is used to run a block of code only when a given condition is True.

Syntax

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.
  • The colon : marks the start of the indented code block.
  • Indented block: Code that will execute only if the condition is true.

Example

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.

Flowchart

If statement execution flow, completed diagram

Follow initialization, the condition, the selected branch, and the end of the program.

2. if-else Statement

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.

Syntax

if condition:
    # code to run if condition is True
else:
    # code to run if condition is False

Syntax Explanation

  • if: Starts the conditional check.
  • condition: An expression that returns True or False.
  • :: A colon follows the condition and begins the indented block.
  • Indented block (under if): Executes if the condition is True.
  • else:: Specifies the alternate block to run if the condition is False.
  • Indented block (under else): Executes if the condition is False.

Example

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..

3. if-elif-else Ladder

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.

Syntax

if condition1:
    # code block 1
elif condition2:
    # code block 2
elif condition3:
    # code block 3
else:
    # default code block if none above are true

Syntax Explanation

  • 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.
  • Only the first block with a True condition runs; the rest are ignored.

Example

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.

4. Nested if Statements

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.

Important Notes When Using Conditional Statements

  • Use colons (:) after if, elif, and else.
  • Indent the code block under each condition using spaces (usually 4).
  • Conditions are usually comparisons: ==, !=, >, <, >=, <=.

What to remember

Connect what you noticed in the example with the idea below.

Remember

An if condition chooses which block runs. Only the selected branch executes; statements after the branch run normally.

Watch out for this idea

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.

    Next lesson: Python Loops →