Python if…elif…else


Table of contents

Python Conditional Statements (Decision Making)

While programming, you will encounter many situations where your program has to execute statements based on certain conditions. These statements are called conditional statements.

The conditional statements provided by python are ‘if…elif…else’ statements that can be used for decision making in programs. Unlike many other programming languages, Python doesn’t support ‘switch…case’ statements.

The operators which can be used in test expressions or conditions in if…elif…else statements are briefly explained below.

Comparison (Relational) Operators

The comparison or relational operators are used to compare objects. These operators return a boolean value (True or False) based on the result. The relational operators supported by python are as follows:

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • >= (Greater than or equal to)
  • < (Less than)
  • <= (Less than or equal to)

Membership Operators

In python, membership operators return True or False based on either sequence like strings, lists, tuples, sets, and dictionaries contain an object or not. The two membership operators are:

  • in: Returns True if an object is in the sequence, otherwise returns False.
  • not in: Returns True if an object is not in the sequence, otherwise returns False.

Identity Operators

The identity operators compare the two objects’ memory locations. There are two identity operators supported by python such as:

  • is: Returns True if the two objects are in the same memory location, otherwise returns False.
  • is not: Returns True if the two objects are not in the same memory location, otherwise returns False.

Logical Operators

Logical operators in python are used to combine two or more comparison or membership or identity expressions in the same statement. The logical operators supported by python are:

  • and: Returns True if both of the test expressions return True, otherwise returns False.
  • or: Returns True if one of the test expression returns True, otherwise returns False.
  • not: Returns True if the test expression returns False, otherwise returns True.

Now, let us use these operators with the if…elif…else statements.

Python if Statement

The ‘if’ statement in python is used to execute the statements inside the ‘if’ block if the condition satisfies. If the test expression in the ‘if’ statement is not True, then the program skips the code inside the ‘if’ block.

For example:

a = 5 # assign value 5 to a
if a == 5:
    print('Value of a is 5.')
if a == 6:
    print('Value of a is 6.')

Output:

Value of a is 5.

In the above example, the test expression ‘a == 5’ in the first ‘if’ statement returns True, thus ‘Value of a is 5.’ is printed. However, the test expression ‘a == 6’ in the second ‘if’ statement returns False, thus code inside that ‘if block’ is skipped.

Python if…else Statement

The ‘if’ block in the ‘if…else’ statement works similar to the ‘if’ statements. The difference is that, if the test expression in the ‘if’ statement is not true, then the code inside ‘else’ block is executed.

For example:

b = [1, 2, 3, 4, 5, 6]
if 1 in b:
    print('1 is in list b.')
else:
    print('1 is not in list b.')
if 7 in b:
    print('7 is in list b.')
else:
    print('7 is not in list b.')

Output:

1 is in list b.
7 is not in list b.

In the above example, the test expression in the first ‘if’ statement tests either 1 is a member of list b or not. Since the test expression returns true, ‘1 is in list b.’ is printed and code inside else block is skipped. However, in the second if statement, the test expression ‘7 in b’ returns False, thus the code inside ‘if’ block is skipped and the print statement inside ‘else’ block is executed.

Python if…elif…else Statement

In python, ‘if…elif…else’ allows us to have multiple ‘if’ statements, that is to test for more than one test expression in the same ‘if…elif…else block’. The ‘elif’ stands for ‘else if’. If the test expression in the ‘if’ statement returns False, the program will check the test expression of next ‘elif’ block and so on.

Python allows you to have multiple ‘elif’ statements in the same ‘if…elif…else’ block, however, there can be only one ‘else’ block. One important thing to remember is that only code inside one of ‘if’ or ‘elif’ or ‘else’ block is executed. If test expressions in multiple ‘if’ or ‘elif’ block return True, the first ‘if’ or ‘elif’ block that returns True is executed and all the ‘elif’ or ‘else’ blocks after that are skipped.

For example:

a = 10
b = 20
if a is b:
    print('Values of a and b are same.')
elif a is not b:
    print('Values of a and b are not same.')
elif a < 5:
    print('a is less than 5.')
elif b < 25:
    print('b is less than 25.')
else:
    print('All test expressions returned False.')

Output:

Values of a and b are not same.

In the above example, the test expression in the ‘if’ statement returns False, thus, the program moves forward to test for the condition in the next ‘elif’ statement which returns True. Now, the code inside the ‘elif’ block is executed. All other ‘elif’ and ‘else’ blocks are not executed even though test expressions in some of them return True.

Multiple Test Expressions or Conditions in ‘if’ or ‘elif’ Statements

Python also allows having multiple test expressions or conditions in the same ‘if’ or ‘elif’ statements. This is done with the use of logical operators.

For example:

a = 5 
b = [1, 2, 3, 4, 5] 
c = 6 
if (a > 0) and (a < 6):
    print('a is greater than 0 and less than 6.')
elif (a == 4) or (a in b) or (a in c):
    print('a is either equal to 5 or a is in list b or a is same as c.')
else:
    print('None of the test expressions return True.')

Output:

a is either equal to 5 or a is in list b or a is same as c.

In the above example, even though one of the test expression, that is ‘a < 6’ return True, the code inside ‘if’ block is not executed as operator ‘and’ needs both of the conditions to be satisfied in order to execute the code inside the ‘if’ block. However, in the ‘elif’ block, the logical operator ‘or’ is used. That means the code inside ‘elif’ block is executed if at least one condition is True. In our case, condition ‘a in b’ returns True, thus the code block is executed.

Python Nested if…elif…else Statements

Python allows having ‘if…elif…else’ statements within other ‘if…elif…else’ statements. These types of statements are called nested ‘if…elif…else’ statements. You can nest conditional statements up to any level and indentation is used to separate the level of these blocks.

For example:

a = 6
b = [1, 2, 3, 4, 5]
if a == 6:
    if a in b:
        print('a is equal to 6 and a is in list b.')
    else:
        print('a is equal to 6 but a is not in list b.')
else:
    print('None of conditions satisfy.')

Output:

a is equal to 6 but a is not in list b.

In the above example, test expression ‘a == 6’ in outer ‘if’ block is True, thus program control goes to the nested ‘if…else’ block. However, the condition ‘a in b’ in the nested ‘if’ block returns False. Thus, the program skips to the nested ‘else’ block and executes it.