Control Flow: Conditional Statements
In this chapter, we will learn about conditional statements in Python. Conditional statements allow you to execute specific blocks of code based on certain conditions. In Python, we use the if
, elif
, and else
statements to create conditional logic.
The if
Statement
The if
statement is used to execute a block of code only if a certain condition is true.
Here is the basic syntax of an if
statement:
if condition:
# code block
In this syntax, condition
is an expression that evaluates to either True
or False
. If the condition is True
, the code block will be executed. If the condition is False
, the code block will be skipped.
Let’s look at an example:
x = 10
if x > 5:
print("x is greater than 5")
In this example, the condition x > 5
evaluates to True
because x
is 10
, which is greater than 5
. Therefore, the code block print("x is greater than 5")
will be executed.
The elif
Statement
The elif
statement is short for “else if”. It allows you to check multiple conditions in sequence. If the condition of the if
statement is False
, the elif
statement will be evaluated. If the condition of the elif
statement is True
, the corresponding code block will be executed.
Here is the basic syntax of an elif
statement:
if condition1:
# code block 1
elif condition2:
# code block 2
In this syntax, condition1
and condition2
are expressions that evaluate to either True
or False
. If condition1
is True
, code block 1
will be executed. If condition1
is False
and condition2
is True
, code block 2
will be executed.
Let’s look at an example:
x = 10
if x < 5:
print("x is less than 5")
elif x > 5:
print("x is greater than 5")
In this example, the condition x < 5
is False
, so the code block print("x is less than 5"). However, the condition
x > 5is
True, so the code block
print(“x is greater than 5”)` will be executed.
The else
Statement
The else
statement is used to execute a block of code if none of the conditions in the if
or elif
statements are True
.
Here is the basic syntax of an else
statement:
if condition:
# code block 1
else:
# code block 2
In this syntax, condition
is an expression that evaluates to either True
or False
. If condition
is True
, code block 1
will be executed. If condition
is False
, code block 2
will be executed by the else
statement.
Let’s look at an example:
x = 10
if x < 5:
print("x is less than 5")
else:
print("x is greater than or equal to 5")
In this example, the condition x < 5
is False
, so the code block print("x is less than 5")
will be skipped. The else
statement will execute the code block print("x is greater than or equal to 5")
because none of the previous conditions were True
.
Nested Conditional Statements
You can also nest conditional statements within each other to create more complex logic. This allows you to check multiple conditions in sequence.
Here is an example of nested conditional statements:
x = 10
y = 5
if x > 5:
if y > 5:
print("Both x and y are greater than 5")
else:
print("x is greater than 5, but y is not greater than 5")
else:
print("x is not greater than 5")
In this example, we have nested an if
statement inside another if
statement. The inner if
statement checks if y
is greater than 5
. If this condition is True
, it prints “Both x and y are greater than 5”. If the condition is False
, it prints “x is greater than 5, but y is not greater than 5”.
Nested conditional statements can be used to create more complex logic and handle multiple scenarios in your code. However, it is essential to maintain code readability and avoid excessive nesting to prevent confusion.
switch-case Statement in Python
Python does not have a built-in switch-case
statement like some other programming languages. However, you can achieve similar functionality using a dictionary mapping or a series of if-elif-else
statements.
Here is an example of using a dictionary mapping to implement a switch-case
statement in Python:
def switch_case(argument):
switcher = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December"
}
return switcher.get(argument, "Invalid month")
month_number = 5
month_name = switch_case(month_number)
print(f"The month corresponding to {month_number} is {month_name}")
In this example, the switch_case
function takes an argument month_number
and returns the corresponding month name using a dictionary mapping. If the month_number
is not found in the dictionary, it returns “Invalid month”. This approach allows you to achieve similar functionality to a switch-case
statement in Python.
In summary, conditional statements in Python (if
, elif
, else
) allow you to execute specific blocks of code based on certain conditions. You can use nested conditional statements to create more complex logic, and you can implement a switch-case
statement using a dictionary mapping or a series of if-elif-else
statements. Understanding conditional statements is essential for writing effective and structured code in Python.
Conclusion
In this chapter, we learned about conditional statements in Python. We explored the if
, elif
, and else
statements, which allow you to execute specific blocks of code based on certain conditions. We also discussed nested conditional statements and how to implement a switch-case
statement in Python using a dictionary mapping. Conditional statements are essential for creating structured and logical code that can handle different scenarios based on specific conditions.
Now that you have learned about conditional statements, let’s move on to the next chapter to explore loops in Python.