Flow control

Conditional statements

1. The if statement:

The if statement is the foundation of conditional statements in Python. It allows you to execute a block of code only if a certain condition is met. Here's the basic syntax:

if condition:
    # code to execute if the condition is True

condition: This is any expression that evaluates to True or False. Common expressions involve comparisons using operators like == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).

Example: simple if statement

if age >= 18:
    print("You are eligible to vote.")

In Python, indentation is crucial. The code block indented after the if statement (usually 4 spaces or 1 tab) is only executed if the condition is True. This indentation style is essential for defining code blocks within control flow statements.


2. The if-else statement:

The else clause provides an alternative block of code to execute if the condition in the if statement is False. Here's the syntax:

if condition:
    # code to execute if the condition is True
else:
    # code to execute if the condition is False

Example:

if age >= 18:
    print("You are eligible to vote.")
else
    print("You are not eligible to vote.")

3. The if-elif-else chain:

When you have multiple conditions to check, you can use an if-elif-else chain. The elif (else if) clause allows you to specify additional conditions to check if the previous if conditions weren't met. Here's the general structure:

if condition1:
    # code to execute if condition1 is True
elif condition2:
    # code to execute if condition1 is False and condition2 is True
# You can have multiple elif blocks
else:
    # code to execute if all conditions are False

Example:

grade = "B"
if grade == "A":
    print("Outstanding!")
elif grade == "B":
    print("Very good!")
elif grade == "C":
    print("Good!")
else:
    print("Please work harder next time.")


4. Nested if statements:

For complex decision-making, you can nest if statements within each other. However, make sure the indentation is clear to represent the nesting structure. Be cautious of overly nested statements as they can make code harder to read.


By effectively using conditional statements, you can write Python programs that make decisions and respond accordingly based on various conditions.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

What is the basic syntax of an if statement in Python?

if: condition then code
if condition: code to execute
if condition code: execute
if (condition) { code }
Check Answer

What does the else clause do in an if statement?

Executes code if the if condition is False
Always executes code
Executes only when there are no conditions
Acts as a second if statement
Check Answer

What is the purpose of the elif clause?

To end the if statement
To provide an alternative if the first if condition is True
To chain multiple if statements
To check additional conditions if previous conditions were False
Check Answer

What is a potential drawback of nested if statements?

They make the code run faster
They are easier to read
They simplify decision-making
They can make code harder to read if overly nested
Check Answer

Why is indentation important in Python conditional statements?

It is required for all programming languages
It improves performance
It determines the order of execution
It defines code blocks
Check Answer