Python: if…else [Control Flow]

In the code, we sometimes want certain pieces to be executed, and we ignore certain parts of the code. Keywords like “if“, “else“, and “elif” help us with that.

if Condition

Using the “if” condition we ask the interpreter to execute the code under the “if” block, only if the provided condition. Check the general syntax for “if“-

# Syntax for "if" condition
if some_condition:
    # code to execute
    # in case only some_condition is satisfied
Python

NOTES

There is an indentation after the “if” statement line. All the code that belongs to the if block should be indented properly.

In the following code, we have added “if” statement with some boolean checking.

The first if statement is executed successfully, as the condition is True.

But, the second one is not executed, as the condition is False.

if True:
    print("This is true") # This is executed
    
if False:
    # Execution never reaches this block
    print("This is false") # Never printed
Python

Output:

This is true
Plaintext

For example, check the following example. We have a condition “x >= 40” and we have the value of x is 80. So the condition is satisfied, and it prints “Success”.

x = 80

if x >= 40:
    print("Success")
Python

Output:

Success
Plaintext

if…else

x = 80

if x >= 40:
    print("Success")
else: 
    print("Failure")
Python

Output:

Success
Plaintext

if…elif…else

x = 80

if x > 100:
    print("Wrong value")
elif x > 80:
    print("Excellent score")
elif x >= 40:
    print("Good score")
else: 
    print("Fail")
Python

Output:

Good score
Plaintext

Nested if…else

x = 80

if x > 40:
    print("X is greater than 40")
    
    if x >= 80:
        if x == 100:
            print("Perfect Score")
        elif x >= 90:
            print('Excellent score')
        else:
            print("Good score")
    else:
        print("Average score")
        
Python

Output:

X is greater than 40
Good score
Plaintext

Example #1: Discount Calculation

discount_threshold = 500
discount_percentage = 0.25

super_discount_threshold = 2000
super_discount_percentage = 0.4

product_price = float(input("Enter product price: "))

# Check if price satisfies minimum price threshold
# and calculate discount
if product_price >= super_discount_threshold:
    discount = product_price * super_discount_percentage
elif product_price >= discount_threshold:
    discount = product_price * discount_percentage
else:
    discount = 0

price_after_discount = product_price - discount

print(f"Product price after discount: {price_after_discount}")
print(f"Discount amount: {discount}")
Python

Output:

Run the script and enter price “100”-

Enter product price: 100
Product price after discount: 100.0
Discount amount: 0
Plaintext

Rerun the script and enter price “999”-

Enter product price: 999
Product price after discount: 749.25
Discount amount: 249.75
Plaintext

Rerun the script and enter price “5000”-

Enter product price: 5000
Product price after discount: 3000.0
Discount amount: 2000.0
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.