Python Basics - Control Flow Statements
Introduction to control flow statements like if, elif, and else
Control flow statements allow you to control the execution of code based on certain conditions. In Python, the primary control flow statements are if
, elif
, and else
. These statements enable you to execute different blocks of code depending on the conditions you specify.
Key Points:
- Control flow statements manage the flow of execution based on conditions.
- The
if
statement executes a block of code if a condition is true. - The
elif
statement allows you to check multiple conditions. - The
else
statement executes a block of code if all conditions are false.
if Statement
The if
statement is used to execute a block of code if a specified condition is true:
# Example of if statement
x = 10
if x > 5:
print("x is greater than 5")
In this example, the message "x is greater than 5" will be printed because the condition x > 5
is true.
elif Statement
The elif
statement stands for "else if" and allows you to check multiple conditions. If the first if
statement is false, the program checks the elif
condition:
# Example of elif statement
x = 10
if x > 15:
print("x is greater than 15")
elif x > 5:
print("x is greater than 5 but less than or equal to 15")
In this example, the message "x is greater than 5 but less than or equal to 15" will be printed because the condition x > 5
is true, and the if
condition x > 15
is false.
else Statement
The else
statement is used to execute a block of code if all preceding if
and elif
conditions are false:
# Example of else statement
x = 10
if x > 15:
print("x is greater than 15")
elif x > 5:
print("x is greater than 5 but less than or equal to 15")
else:
print("x is 5 or less")
In this example, the else
block will not be executed because the condition x > 5
is true.
Combining if, elif, and else Statements
You can combine if
, elif
, and else
statements to create complex conditional logic:
# Example of combining if, elif, and else statements
x = 10
if x > 15:
print("x is greater than 15")
elif x > 10:
print("x is greater than 10 but less than or equal to 15")
elif x > 5:
print("x is greater than 5 but less than or equal to 10")
else:
print("x is 5 or less")
In this example, the message "x is greater than 5 but less than or equal to 10" will be printed because the condition x > 5
is true, and the preceding conditions are false.
Nesting Control Flow Statements
You can nest control flow statements to create more complex logic:
# Example of nesting control flow statements
x = 10
y = 20
if x > 5:
if y > 15:
print("x is greater than 5 and y is greater than 15")
else:
print("x is greater than 5 but y is 15 or less")
else:
print("x is 5 or less")
In this example, the message "x is greater than 5 and y is greater than 15" will be printed because both conditions x > 5
and y > 15
are true.
Summary
In this tutorial, you learned about control flow statements in Python, including if
, elif
, and else
. These statements allow you to execute different blocks of code based on specified conditions, enabling you to implement complex logic in your programs.