Home » If Else In Python

If Else In Python

If Else In Python

If Statement

Understanding if else in Python. Sometimes the programmer needs to check the evaluation of certain expression(s), whether the expression(s) evaluate to True or False. If the expression evaluates to False, then the program execution follows a different path then it would have if the expression had evaluated to True.

Based on this, the conditional statements are further classified into following types; if, if……else, elif, nested if.

If Statement in Python

A simple if statement works on following principle,

  1. execute the block of code inside if statement if the expression evaluates True.
  2. ignore the block of code inside if statement if the expression evaluates False and return to the code outside if statement.
if-stement-python.png

Example:

applePrice = 180
budget = 200
if (applePrice <= budget):
    print("Alexa, add 1kg Apples to the cart.")
Python

Output:

Alexa, add 1kg Apples to the cart.
Python

If-Else Statement

An if……else statement works on the following principle,

  1. execute the block of code inside if statement if the expression evaluates True. After execution return to the code out of the if……else block.
  2. execute the block of code inside else statement if the expression evaluates False. After execution return to the code out of the if……else block.
if-else-python-statement.png

If-Else Statement Example:

applePrice = 210
budget = 200
if (applePrice <= budget):
    print("Alexa, add 1kg Apples to the cart.")
else:
    print("Alexa, do not add Apples to the cart.")
Python

Output:

Alexa, do not add Apples to the cart.
Python

Age Verification:

age = 17

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote yet.")
Python

This code checks whether a person’s age is 18 or above. If the age is 18 or greater, it informs the person that they are eligible to vote; otherwise, it informs them that they are not yet eligible.

Discount Calculation

total_amount = 150
discount_threshold = 100

if total_amount >= discount_threshold:
    discount = 0.1 * total_amount  # 10% discount
    final_amount = total_amount - discount
    print(f"You qualify for a 10% discount. Your final amount is ${final_amount}.")
else:
    print("Sorry, you need to spend more to qualify for a discount.")
Python

In this example, if the total_amount is greater than or equal to the discount_threshold, a 10% discount is applied; otherwise, it informs the customer that they need to spend more to qualify for a discount.

Conclusion

The if else statement in Python is fundamental for building conditional logic in your programs. It allows you to control the flow of execution based on specific conditions. By mastering the if else statement, you can create powerful algorithms and applications that respond dynamically to different scenarios. Remember to write clear and concise conditions, and always test your code with various inputs to ensure it behaves as expected. With practice, you’ll become proficient in using if else statements to solve a wide range of programming problems.

Frequently Asked Questions