4-2. The if – else Statement

The if-else statement in Python allows your program to execute one block of code if a condition is true, and another block of code if the condition is false. This helps in creating more complex decision-making processes within your code.

  • If Condition: The program first evaluates the condition following the if keyword.
  • True Condition: If the condition is true, the code block under the if statement is executed.
  • False Condition: If the condition is false, the code block under the else statement is executed.

Here’s the basic syntax of an if-else statement in Python:

                if condition:

                                # code to execute if the condition is true

                else:

                                # code to execute if the condition is false

Let's look at an example to understand how the if-else statement works:

a = 100
b = 50
if b > a:
    print("b is greater than a")
else:
    print("b is not greater than a")

 

Explanation:

  • Variables: We have two variables, a and b, with values 100 and 50, respectively.
  • Condition: The condition b > a is evaluated.
  • True Path: If b were greater than a, the program would print "b is greater than a".
  • False Path: Since b is not greater than a (50 is not greater than 100), the program executes the code block under the else statement and prints "b is not greater than a".

This example shows how you can control the flow of your program based on conditions. The if-else statement ensures that different blocks of code are executed depending on whether the specified condition is met.

Importance of Indentation

Just like with simple if statements, proper indentation is crucial for if-else statements. Python relies on indentation to understand the scope of the code blocks. Incorrect indentation will lead to errors.

Example With Proper Indentation:

a = 100
b = 50
if b > a:
    print("b is greater than a")
else:
    print("b is not greater than a")

Example With Improper Indentation:

a = 100
b = 50
if b > a:
print("b is greater than a") # This will cause an error
else:
print("b is not greater than a") # This will also cause an error

In the incorrect example, the lack of indentation for the print statements will cause an error because Python does not recognize them as part of the if-else blocks.

The if-else statement is a fundamental tool in Python programming, allowing you to execute different code paths based on conditions. Mastering this concept is essential for writing dynamic and responsive programs.

 

Example:

Problem Statement: Write a Python program that takes an input number from the user and checks if it is even or odd. If the number is even, print "The number is even". If the number is odd, print "The number is odd".

Instructions:

  • Prompt the user to enter a number.
  • Convert the input to an integer.
  • Use an if-else statement to check if the number is divisible by 2 (i.e., even).
  • If the condition is true, print "The number is even".
  • If the condition is false, print "The number is odd".

Example Output:

                Enter a number: 4

                The number is even

 

                Enter a number: 7

                The number is odd

Solution:

# Step 1: Prompt the user to enter a number
number = int(input("Enter a number: "))
# Step 2: Check if the number is even or odd
if number % 2 == 0:
    # Step 3: Print the result if the number is even
    print("The number is even")
else:
    # Step 4: Print the result if the number is odd
    print("The number is odd")