if elif else in Python

  • Last updated Apr 25, 2024

In Python, the if, elif (short for "else if"), and else statements are used to create conditional logic. These statements allow you to execute different blocks of code based on specific conditions.

if statement

The if statement is used in a program to test a condition. If the condition evaluates to True, the code indented within the code block is executed. If the condition is False, the code within the code block is skipped, and the program execution proceeds to the next statement.

Example:

x = 5
y = 8
z = 5

if(x > y):
    print("x is greater than y")
if(x < y):
    print("y is greater than x")
if(x == y):
    print("x is equal to y")
if(x == z):
    print("x is equal to z")

In this example, there are three variables: x, y, and z. x is assigned the value 5. y is assigned the value 8. z is assigned the value 5. Then there is a series of if statements to compare these variables and print messages based on the comparisons. The first if statement checks if x is greater than y. Since x (5) is not greater than y (8), this condition is False, and the associated print statement is skipped. The second if statement checks if x is less than y. This condition is True because x (5) is indeed less than y (8), so the message "y is greater than x" is printed. The third if statement checks if x is equal to y. This condition is False because x (5) is not equal to y (8), so the associated print statement is skipped. The fourth if statement checks if x is equal to z. This condition is True because both x and z have the same value (5), so the message "x is equal to z" is printed.

The output of the above example is as follows:

y is greater than x
x is equal to z
elif statement

The elif statement is used to test another condition only if the previous conditions are False. It allows a program to check for multiple logical conditional tests and runs one block of code only if the test returns True. The elif statement is optional and is similar to the else if statement in other programming languages.

Example:

x = 2
y = 8

if(x > y):
    print("x is greater than y")
elif(x < y):
    print("y is greater than x")
elif(x == y):
    print("x is equal to y")
elif(x != y):
    print("x is not equal to y")

In this example, there are two variables: x and y. x is assigned the value 2. y is assigned the value 8. Then there are a series of if and elif (else if) statements to compare these variables and print messages based on the comparisons. The first if statement checks if x is greater than y. Since x (2) is not greater than y (8), this condition is False, and the associated block of code is skipped. The first elif statement checks if x is less than y. This condition is True because x (2) is indeed less than y (8), so the message "y is greater than x" is printed. The second elif statement checks if x is equal to y. This condition is False because x (2) is not equal to y (8), so the associated block of code is skipped. The third elif statement checks if x is not equal to y. This condition is True because x (2) is indeed not equal to y (8), but the message "x is not equal to y" is printed because it works in a way that once one of the conditions is true and its corresponding block of code is executed, the program doesn't evaluate the subsequent elif conditions. It stops at the first true condition it encounters.

The output of the above code is as follows:

y is greater than x
else statement

The else statement is used in a program to execute a block of code if none of the previous conditions (from if and elif statements) are True. The else statement is optional.

Example:

x = 4
y = 7

if x == y:
    print("x is equal to y")
elif x == 5:
    print("x is equal to 5")
elif y == 2:
    print("y is equal to 2")
else:
    print("Neither x nor y matches any previous condition")

In this example, there are two variables: x and y. x is assigned the value 4. y is assigned the value 7. The code uses a series of if, elif (else if), and else statements to check the values of x and y against certain conditions and print messages based on the results of these comparisons. The first if statement checks if x is equal to y. Since x (4) is not equal to y (7), this condition is False, and the associated block of code is skipped. The first elif statement checks if x is equal to 5. This condition is False because x (4) is not equal to 5, so the associated block of code is also skipped. The second elif statement checks if y is equal to 2. This condition is False because y (7) is not equal to 2, so the associated block of code is skipped as well. Since none of the previous conditions are met, the else block is executed. It prints the message "Neither x nor y matches any previous condition".

The output of the above code is as follows:

Neither x nor y matches any previous condition