Python Decision Control
Decision control is an important aspect of programming, and Python provides several constructs for making decisions in a program.
These constructs are also known as control flow statements, which allow a program to execute different paths of code based on certain conditions.
Here we will explore the different decision control statements in Python which are as below:
- If-else statements
- Elif statements
- Nested if statements
- Ternary operator
Let's see them in detail.
If-else statements :
The if-else statement in Python is a fundamental decision control construct that allows us to execute different blocks of code based on a condition.
It provides a way to perform alternative actions depending on whether a condition is true or false.
The syntax of the if-else statement is as follows:
if condition:
# code block to execute if condition is true
else:
# code block to execute if condition is false
Let's break down the components of the if-else statement:
- condition: It is an expression or a Boolean condition that evaluates to either True or False. If the condition is True, the code block inside the if statement is executed. If the condition is False, the code block inside the else statement is executed.
- code block: It is a set of indented statements that are executed if the corresponding condition is true or false. The code block can contain one or more statements. It is important to maintain proper indentation to define the code block correctly.
Here's an example to illustrate the usage of the if-else statement:
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
In this example, the condition x > 5 is evaluated. If it is true, the statement print("x is greater than 5") is executed. Otherwise, if the condition is false, the statement print("x is less than or equal to 5") is executed.
Elif statements :
The elif statement in Python stands for "else if". It is used when we have multiple conditions to check and execute different blocks of code based on each
condition. The elif statement allows us to specify additional conditions to be evaluated if the previous conditions in the if-else chain are false.
The syntax of the elif statement is as follows:
if condition1:
# code block to execute if condition1 is true
elif condition2:
# code block to execute if condition2 is true
elif condition3:
# code block to execute if condition3 is true
...
else:
# code block to execute if all conditions are false
Here's how the elif statement works:
- The first condition,"condition1", is checked. If it evaluates to True, the corresponding code block is executed, and the rest of the elif and else blocks are skipped.
- If "condition1" is False, the next condition, "condition2", is evaluated. If it is True, the code block associated with condition2 is executed, and the rest of the elif and else blocks are skipped.
- This process continues for each elif statement until a condition evaluates to True.
- If none of the conditions in the if and elif statements are true, the code block inside the else statement, if present, is executed.
Let's see an example to understand the usage of elif statements:
x = 10
if x > 10:
print("x is greater than 10")
elif x < 10:
print("x is less than 10")
else:
print("x is equal to 10")
In this example, the value of x is evaluated against multiple conditions. If x is greater than 10, the first condition is true, and the corresponding code
block print("x is greater than 10") is executed. If x is not greater than 10, the next condition x < 10 is checked. If it is true, the corresponding code
block print("x is less than 10") is executed. Finally, if both conditions are false, the code block inside the else statement is executed, printing "x is equal to 10".
It's important to note that the else block at the end is optional. If we only need to check multiple conditions and don't require a default action for when all conditions
are false, we can omit the else block.
The elif statement allows us to handle multiple conditions and execute different blocks of code based on those conditions. It provides a way to create complex decision
structures in our Python programs, giving us more flexibility and control over program execution.
Nested if statements :
Nested if statements in Python allow us to have an inner if statement inside an outer if statement. This construct is useful when we need to evaluate multiple
conditions in a hierarchical manner. The inner if statement is only evaluated if the condition of the outer if statement is true. Nested if statements can be
nested to multiple levels based on the complexity of the conditions which we need to check.
The syntax of nested if statements in Python is as follows:
if condition1:
# Outer if block
if condition2:
# Inner if block
# Code to execute if condition1 and condition2 are true
else:
# Inner else block
# Code to execute if condition1 is true but condition2 is false
else:
# Outer else block
# Code to execute if condition1 is false
Let's take an example to understand the concept:
x = 10
y = 5
if x > 5:
print("x is greater than 5")
if y > 10:
print("y is greater than 10")
else:
print("y is less than or equal to 10")
else:
print("x is less than or equal to 5")
In this example, there are two conditions being evaluated. The outer 'if' statement checks if 'x' is greater than 5. If it is true, the code block inside
the outer 'if' statement is executed. Within that code block, there is an inner 'if' statement that checks if 'y' is greater than 10. If it is true, the
corresponding code block inside the inner 'if' statement is executed. If the condition of the outer 'if' statement is true but the condition of the inner
'if' statement is false, the code block inside the inner 'else' statement is executed.
The nesting of 'if' statements can continue further, allowing us to create more complex decision structures based on the conditions which we need to check.
It's important to maintain proper indentation to define the code blocks correctly. The indentation helps Python identify which statements belong to
the 'outer if', 'inner if', 'inner else', or 'outer else' blocks.
Nested if statements provide a way to handle intricate decision scenarios where conditions need to be evaluated in a hierarchical manner. By
utilizing nested if statements effectively, we can create programs that make detailed decisions based on multiple conditions.
Ternary operator :
The ternary operator in Python provides a concise way to write if-else statements in a single line of code. It allows us to assign a value to a variable based on a condition.
The ternary operator is also known as a conditional expression.
The syntax of the ternary operator is as follows:
value_if_true if condition else value_if_false
Let's break down the components of the ternary operator:
- condition: It is a Boolean expression or a condition that evaluates to either True or False.
- value_if_true: It is the value that will be assigned to the variable if the condition is true.
- value_if_false: It is the value that will be assigned to the variable if the condition is false.
Here's an example to illustrate the usage of the ternary operator:
x = 10
result = "Even" if x % 2 == 0 else "Odd"
print(result)
In this example, we check if 'x' is divisible by 2 using the condition 'x % 2 == 0'. If the condition is
true, the value "Even" is assigned to the variable result. If the condition is false, the value "Odd" is assigned.
Finally, the value of the result is printed, which will be either "Even" or "Odd" based on the value of 'x'.
The ternary operator can be nested as well, allowing for more complex expressions. Here's an example:
x = 7
result = "Positive" if x > 0 else ("Zero" if x == 0 else "Negative")
print(result)
In this example, we check if 'x' is positive, zero, or negative. If 'x' is greater than '0', the
value "Positive" is assigned. If 'x' is equal to '0', the value "Zero" is assigned. If both
conditions are false, the value "Negative" is assigned.
The ternary operator is a concise and powerful tool that allows us to assign values based on conditions without writing lengthy if-else statements.
However, it's important to use it judiciously and ensure that the code remains readable and maintainable.