Welcome back! Now that you’ve learned about arrays in Python, it’s time to dive into control flow statements, specifically the if
… else
… condition. Control flow statements allow you to execute specific blocks of code based on certain conditions. In this article, we’ll explore how to use if
, else
, and elif
statements in Python to control the flow of your programs.
The if
Statement
The if
statement is used to test a specific condition. If the condition evaluates to True
, the block of code inside the if
statement is executed. If it evaluates to False
, the code inside the if
block is skipped.
Syntax
if condition:
# Code to execute if condition is True
Example
x = 10
if x > 5:
print("x is greater than 5") # Output: x is greater than 5
In this example, the condition x > 5
is True
, so the code inside the if
block is executed.
The else
Statement
The else
statement is used to define a block of code that will be executed if the condition in the if
statement evaluates to False
.
Syntax
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
Example
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5") # Output: x is not greater than 5
In this example, the condition x > 5
is False
, so the code inside the else
block is executed.
The elif
Statement
The elif
(short for “else if”) statement is used to test multiple conditions. If the condition in the if
statement is False
, the program checks the condition in the elif
statement. You can have multiple elif
statements in your code.
Syntax
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
else:
# Code to execute if all conditions are False
Example
x = 7
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but less than or equal to 10") # Output: x is greater than 5 but less than or equal to 10
else:
print("x is 5 or less")
In this example, the condition x > 10
is False
, but x > 5
is True
, so the code inside the elif
block is executed.
Nested if
Statements
You can also nest if
statements inside other if
, elif
, or else
blocks. This allows you to check multiple conditions in a more complex manner.
Example
x = 15
if x > 10:
print("x is greater than 10") # Output: x is greater than 10
if x > 20:
print("x is also greater than 20")
else:
print("x is not greater than 20") # Output: x is not greater than 20
else:
print("x is 10 or less")
In this example, the first if
condition x > 10
is True
, so the code inside the first if
block is executed. Then, the nested if
condition x > 20
is checked. Since x > 20
is False
, the code inside the else
block is executed.
Example: Using if
… else
in a Program
Let’s create a simple program that determines if a number is positive, negative, or zero.
# Get user input
number = float(input("Enter a number: "))
# Check if the number is positive, negative, or zero
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
In this program, we:
- Get a number from the user.
- Use an
if
statement to check if the number is positive. - Use an
elif
statement to check if the number is negative. - Use an
else
statement to handle the case where the number is zero.
Conclusion
The if
… else
… condition is a fundamental control flow statement in Python that allows you to execute specific blocks of code based on certain conditions. By understanding how to use if
, else
, and elif
statements, you can write more complex and dynamic programs.
In our next tutorial, we’ll explore loops in Python, which allow you to execute a block of code multiple times. Stay tuned, and happy coding!