Understanding If and Else Conditions in Python

ยท

3 min read

Using if and else conditions

Introduction: If and else conditions are fundamental building blocks of programming languages, including Python. These conditional statements allow us to control the flow of our programs based on certain conditions. In this article, we will explore the concepts of if and else conditions in Python, how they work, and their various applications.

if condition:
    # Code block to be executed if condition is True

The condition is a boolean expression that evaluates to either True or False. If the condition is True, the indented code block beneath it will be executed. Otherwise, it will be skipped.

  1. Adding Else: Often, we want our program to take a different course of action when the condition is False. We can achieve this using the else statement.

    
     if condition:
         # Code block to be executed if condition is True
     else:
         # Code block to be executed if condition is False
    

    In this case, if the condition is True, the code block under the if statement will be executed. Otherwise, the code block under the else statement will be executed.

    1. Elif Statement: Python also provides the elif statement, which stands for "else if." It allows us to check multiple conditions in sequence.

      
       if condition1:
           # Code block if condition1 is True
       elif condition2:
           # Code block if condition2 is True
       else:
           # Code block if all conditions are False
      

      The elif statement allows us to check additional conditions after the initial if statement. If any of the conditions is True, the corresponding code block will be executed. If none of the conditions is True, the code block under the else statement will run.

      1. Nested Conditions: Python also supports nesting if and else statements within each other, allowing for more complex logic.

         if condition1:
             # Code block if condition1 is True
             if condition2:
                 # Code block if condition2 is True
             else:
                 # Code block if condition2 is False
         else:
             # Code block if condition1 is False
        

        In the example above, if condition1 is True, the program will execute the code block within condition1. Then, if condition2 is True, it will execute the nested code block beneath condition2. If condition2 is False, it will execute the code block under the else statement.

        Now let's try this with a practical example using all what we have learnt to create a simple calculator.

        Example code:

         num1 = input("Enter num1: ")
         num2 = input("Enter num2: ")
        
         symbol = input("Enter operand: ")
        
         if symbol == "+":
             res = int(num1) + int(num2)
             print("Sum: " + str(res))
         elif symbol == "-":
             res = int(num1) - int(num2)
             print("Sub: " + str(res))
         elif symbol == "*":
             res = int(num1) * int(num2)
             print("Mult: " + str(res))
         elif symbol == "/":
             res = int(num1) / int(num2)
             print("Div: " + str(res))
         elif symbol == "%":
             res = int(num1) % int(num2)
             print("Mod: " + str(res))
         elif symbol == "**":
             res = int(num1) ** int(num2)
             print("Div: " + str(res))
         else:
             print("Please enter valid operand")
         print("Done")
        

        In the above example we used the input() method to prompt the user to enter two numbers and also the will have to choose and operand, which will perform a calculation on the numbers entered.

        Conclusion: Conditional statements like if and else play a crucial role in controlling the flow of our Python programs based on certain conditions. By using if, else, and elif statements, we can direct our programs to execute specific code blocks based on the evaluation of boolean expressions. Understanding these concepts is essential for writing effective and logical Python programs.

        Remember, practice is key to mastering conditional statements, so don't hesitate to experiment with different conditions and nested structures. Happy coding!

ย