Python if else elif Statement

Often, you need to execute some statements only when some condition holds.

You can use following conditional statements in your code to do this.

  • if Statement: use it to execute a block of code, if a specified condition is true
  • else Statement: use it to execute a block of code, if the same condition is false
  • elif (else if) Statement: use it to specify a new condition to test, if the first condition is false

The if Statement

Use if statement to execute a block of Python code, if the condition is true.

Syntax

Python if Statement Syntax

Basic Example

Example:

x, y = 7, 5
if x > y:
	print('x is greater')

Likewise, you can use following comparison operators to compare two values:

Comparison operators with if statement
OperatorMeaningExample
==Equalsif x == y
!=Not equalsif x != y
>Greater thanif x > y
>=Greater than or equal toif x >= y
<Less thanif x < y
<=Less than or equal toif x <= y

More Examples

In Python, any non-zero value or nonempty container is considered TRUE, whereas Zero, None, and empty container is considered FALSE.

That’s why all the below if statements are valid.

Examples:

# any non-zero value
if -3:
    print('True')
# mathematical expression
x, y = 7, 5
if x + y:
    print('True')
# nonempty container
L = ['red','green']
if L:
    print('True')

Significance of Indentation

Indentation has a special significance in Python. It is used to define a block of code (often referred to as, a suite).

Contiguous statements that are indented to the same level are considered as part of the same block.

Example: if statement without indentation raises error

x, y = 7, 5
if x > y:
print('x is greater')

Nested if Statement

You can nest statements within a code block to begin a new code block, as long as they follow their respective indentations.

Example:

x, y, z = 7, 4, 2
if x > y:
    print("x is greater than y")
    if x > z:
        print("x is greater than y and z")

The else Statement

Use else statement to execute a block of Python code, if the condition is false.

Syntax

Python if else Statement Syntax

Basic Example

Example:

x, y = 7, 5
if x < y:
    print('y is greater')
else:
    print('x is greater')

The elif (else if) Statement

Use elif statement to specify a new condition to test, if the first condition is false.

Syntax

Python elif Statement Syntax

Basic Example

Example:

x, y = 5, 5
if x > y:
    print('x is greater')
elif x < y:
    print('y is greater')
else:
    print('x and y are equal')

Substitute for Switch Case

Unlike other programming languages, Python does not have a ‘switch‘ statement.

You can use if…elif…elif sequence as a substitute.

Example:

if choice == 1:
	print('case 1')
elif choice == 2:
	print('case 2')
elif choice == 3:
	print('case 3')
elif choice == 4:
	print('case 4')
else:
	print('default case')

Multiple Conditions

To join two or more conditions into a single if statement, use logical operators viz. and, or and not.

and expression is True, if all the conditions are true.

Example: Using and operator

x, y, z = 7, 4, 2
if x > y and x > z:
    print('x is greater')

or expression is True, if at least one of the conditions is True.

Example: Using or operator

x, y, z = 7, 4, 9
if x > y or x > z:
    print('x is greater than y or z')

not expression is True, if the condition is false.

Example: Using not operator

x, y = 7, 5
if not x < y:
    print('x is greater')

One Line if Statement

Python allows us to write an entire if statement on one line.

Example: Short Hand If – single statement

x, y = 5, 7
if x > y: print('x is greater')

You can even keep several lines of code on just one line, simply by separating them with a semicolon ; .

Example: Short Hand If – multiple statements

x, y = 7, 5
if x > y: print('x is greater'); print('y is smaller'); print('x and y are not equal')

Conditional Expressions (ternary operator)

Conditional expression (sometimes referred to as ‘ternary operator’) allows us to select one of two statements depending on the specified condition.

The syntax of the conditional expression is :

Syntax

Python Ternary Operator Syntax

Examples

Example:

x, y = 7, 5
print('x is greater') if x > y else print('y is greater')

You can also use it to select variable assignment.

Example:

x, y = 7, 5
max = x if x > y else y
print(max)  # 7

Check If Item Present in a Sequence

The in operator is used to check if a value is present in a sequence (list, tuple, string etc.).

Example:

# list
L = ['red', 'green', 'blue']
if 'red' in L:
    print('yes')
# tuple
T = ('red', 'green', 'blue')
if 'red' in T:
    print('yes')
# string
S = 'Hello, World!'
if 'Hello' in S:
    print('Yes')