Home
About Us Privacy Policy
 

CONDITIONAL STATEMENTS

ADVERTISEMENT

Intermediate Conditional Statements



Decision making in Python

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.

# Input color
color = input("Enter your favourite color from red, blue, green: \n")
if color == "red":
    print("Blood Red, Burgundy, Sangria")
elif color == "blue":
    print("Dark Blue, Navy Blue, Azul Blue")
elif color == "green":
    print("Dark Green, Marine Green, Lush Green ")
else:
    print("Sorry! We don't have shades for that color.")

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.


ADVERTISEMENT

Nested Statements

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.

# Accept number
number = int(input("Enter a number :\n"))

# Check for even number
# Even numbers will be completely divisible by 2
if number % 2 == 0:
    if number == 8:
        print("The number is 8 and it's even!. ")
else:
    print("This number is not required.")

8
The number is 8 and it's even!.


and (Logical And)

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.

number = int(input("Enter a number :\n"))

# Check for even number
# Even numbers will be completely divisible by 2
if number % 2 == 0 and number == 8:
    print("The number is 8 and it's even!. ")
else:
    print("This number is not required.")

3
This number is not required.


or (Logical Or)

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.

number = int(input("Enter a number :\n"))
if number % 2 == 0 or number == 8:
    print("The number is even!. ")
else:
    print("This number does not fit the Memo.")

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.


ADVERTISEMENT

A little exercise...

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.


Grouping Conditions

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.

# Example

drivetrain = "AWD" 
transmission = "automatic"
no_of_seats = 5
condition = "good"
has_panoramic_sunroof = True
cost = 17599

if (drivetrain ==  "all-wheel-drive" or drivetrain == "AWD" or drivetrain == "awd") and transmission == "automatic" and no_of_seats == 5 and condition == "good" and has_panoramic_sunroof and cost >= 10000 and cost <= 20000:
    print("Car found!")
else:
    print("No car with the given configuration is available at this time. Try again!.")

Car found!


ADVERTISEMENT

Conclusion

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.


ADVERTISEMENT



All product names, logos, and brands are property of their respective owners.