8. if else and elif in python – python course for absolute begineers – in most easy way

Photo of author

By Waqas Ali

Introduction

Programming is all about making decisions, and Python provides powerful tools to implement decision-making in your code. In this article, we will explore three essential components for making decisions in Python: the if statement, the else statement, and the elif statement. Understanding how these statements work will enable you to create dynamic and responsive programs that handle different scenarios effectively.

The if else Statement

if statement in python

The if statement is the most fundamental conditional statement in Python. It allows you to execute a block of code only if a specific condition is true. The syntax of the if statement is straightforward:

if condition:
    # Code block to execute if the condition is True

The code block under the if statement will only be executed if the condition specified evaluates to True. If the condition is False, the code block will be skipped.

Example:

x = 10
if x > 5:
    print("x is greater than 5")

In this example, the code block will be executed because the condition x > 5 is true (x is 10).

The else Statement

else statement in python

The else statement is used in conjunction with the if statement to specify an alternative code block to be executed when the if condition is false. The syntax is as follows:

if condition:
    # Code block to execute if the condition is True
else:
    # Code block to execute if the condition is False

The code block under the else statement will be executed only if the condition specified in the if statement is False.

Example:

x = 3
if x > 5:
    print("x is greater than 5")
else:
    print("x is not greater than 5")

In this example, since x is 3 and the condition x > 5 is false, the code block under the else statement will be executed, and the output will be “x is not greater than 5.”

The elif Statement

The elif statement is short for “else if” or “if else” and is used when you have multiple conditions to check. It allows you to specify additional conditions to be evaluated if the previous conditions are false. The syntax 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
# Add more elif blocks as needed
else:
    # Code block to execute if all conditions are False

Python will evaluate each condition in order, and as soon as it finds a condition that is true, it will execute the corresponding code block and skip the rest of the elif and else blocks.

Example:

x = 10
if x > 15:
    print("x is greater than 15")
elif x > 5:
    print("x is greater than 5 but not greater than 15")
else:
    print("x is not greater than 5")

In this example, since x is 10, the condition x > 5 is true, so the code block under the corresponding elif statement will be executed, and the output will be “x is greater than 5 but not greater than 15.”

Also read : What are strings in python

let’s dive deeper into if, else, and elif statements with more examples.

Nested if Statements

In Python, you can also nest if statements inside other if statements. This allows you to create more complex decision-making logic.

Example:

x = 10
if x > 5:
    print("x is greater than 5")
    if x > 8:
        print("x is also greater than 8")

In this example, the first if statement checks if x is greater than 5. If the condition is true, it executes the corresponding code block and then proceeds to check if x is greater than 8 using the nested if statement. If this condition is also true, it will print “x is also greater than 8.”

Multiple Conditions with elif

elif or if else statements are particularly useful when you have multiple conditions to check. You can chain multiple elif statements after the initial if statement to cover various scenarios.

Example:

age = 25
if age < 18:
    print("You are a minor")
elif age >= 18 and age < 65:
    print("You are an adult")
else:
    print("You are a senior citizen")

In this example, the code checks the value of the variable age and prints the appropriate message based on whether the person is a minor, an adult, or a senior citizen.

Short-Circuit Evaluation

Python utilizes short-circuit evaluation to optimize if else statements with multiple conditions. In a logical expression involving and, if the first condition is False, Python will not evaluate the remaining conditions, as the overall result will be False. Similarly, in a logical expression involving or, if the first condition is True, Python will not evaluate the remaining conditions, as the overall result will be True.

Example:

x = 10
y = 5

if x > 5 and y > 10:
    print("Both conditions are True")

In this example, the if statement uses and to check if both x is greater than 5 and y is greater than 10. However, since x is 10 (which is not greater than 5), Python will not evaluate the second condition, as it already knows the overall result will be False.

Best Practices for Using if else, and elif Statements

When using conditional statements, it’s essential to follow some best practices to write clean and maintainable code:

  1. Indentation: Ensure proper indentation for code blocks under if, else, and elif statements to maintain readability.
  2. Use Parentheses for Complex Conditions: When you have complex conditions involving multiple logical operators, use parentheses to explicitly define the order of evaluation.
  3. Be Clear and Concise: Use meaningful variable names and comments to make your code easier to understand.
  4. Avoid Nested if Statements: While nesting if statements can be useful, excessive nesting can make the code hard to read. Try to keep the nesting to a minimum.

Use this to code for free : click here to go to replit to use it for free

Conclusion

The if else, and elif statements are powerful tools in Python that allow you to make decisions in your code based on specific conditions. By combining these if else and elif statements and following best practices, you can create flexible and efficient programs that respond to different scenarios appropriately. Now you may have got enough knowledge of if else and elif statement in python. If else are very important in python. so practice these examples and become a coding master. Happy Coding!

Leave a comment