Intermediate Conditional Statements
Python allows the programmer to control the program's execution flow depending on the validity of the condition(s). Lets me explain using a real-world example.
Let's suppose you want to display all the shades of a user-selected color. What would your decision-making be?
If the color is red, then print Red, Burgundy, Sangria.
Else if the color is blue, then print blue, navy blue, Azul.
Else if the color is green, then print Green, Marine Green, Lush Green.
Else print, Sorry! We don't have shades for that color.
To implement such statements, Python provides If, elif, else statements.
if condition(s):
# Block 1
# This is executed when the condition evaluates to True
elif condition(s):
# Block 2
# This is executed when the condition evaluates to True
elif conditions(s):
# Block 3
# This is executed when the condition evaluates to True
else:
# Block 4
# This is executed when all the above conditions evaluates to False
Now let's convert this into Python code.
As you see from the above example, the Python code is very similar to the English language, and hence, very clear and easy to develop software.
Enter your favourite color from red, blue, green:
green
Dark Green, Marine Green, Lush Green
When used together, they are collectively also known as the If-Elif-else structure or If-Elif-Else ladder.
Python allows for the nesting of an if-elif-else structure.
Suppose you have a situation where statement(s) can only execute where two or more conditions must evaluate to True. In such scenarios, you can use an if statement inside another if statement.
Here is how you would write it.
8
The number is 8 and it's even!.
In the above example, we achieved the result using a nested if statement. However, utilizing many nested if statements could quickly lead to un-structured, hard-to-read, unintelligible code. To improve readability and productivity, Python provides the and operator.
Let's rewrite the previous example using the logical and operator.
3
This number is not required.
There could be situations when at least one condition needs to be True to execute statements. In such scenarios, Python provides the logical or operator.
16
The number is even!.
The above example will print, even if the number is not eight but is "even" and vice-versa. But it will not execute when the number is neither eight nor even.
Let's suppose you are looking for an all-wheel-drive, automatic, five-seater, in good condition with a panoramic sunroof, within the budget of $10,000 to a maximum of $20,000. What would your decision-making be while profiling the car?. Try, and then click to reveal.
Now, try to write the above in Python. When you're done click to reveal and check.
You can group multiple conditions to treat the resulting expression as a single condition by enclosing them in braces '()'. For example, in the above example, the person could refer to all-wheel-drive cars as "all-wheel-drive" or "AWD"; although they are different literally, they refer to the same drivetrain. In such scenarios, you can group these conditions to make them act together as one.
Car found!
In this chapter, we learned about the importance of decision-making in Python, taking decisions in Python using the if-else statements, creating nested decision structures, the importance of logical and and or operators, and grouping conditions.