Python if else statements

If Else statements in Python are used for decision making. Decision making is needed when we want to execute only a certain piece of code based on a certain condition. Python provides us if, elif, and else statements for decision making. Let’s discuss each statement one by one.


The if statement

The if statement executes it’s body only if the given condition evaluates to True, otherwise it skips the execution.

Python if statement syntax:

if condition:
    body

Here, if the condition specified in the If statement evaluates to True, then only the body of the statement is executed. All the code inside the indented block is known as the body of the if statement.


Example:

# A program to demonstrate the If statement in Python

a = 10 
b = 20

if a<b:
    print('a is less than b')

Output:

a is less than b

Note: The code which is not the part of the indented block is always executed. Refer the example below:

# A program to demonstrate if statement in Python

a = -10

if a<0:
    print('a is a negative number.')
print('I am always printed.')    

Output:

a is a negative number.
I am always printed.

Inline If Statement

In Python, it is also possible to write the body of the If statement and the If statement itself in the same line. It’s is known as an inline If statement.

# A program to demonstrate inline if statement in Python.

a = 10 
b = 20

if a<b: print('a is less than b.')

Output:

a is less than b.

The else statement

The else statement is always used with an if statement. If the condition specified in the if statement evaluates to False, the code inside the If block is skipped and the code inside the else block is executed.

Python if else statements

The else statement syntax:

if condition:
    some code
else:
    some code
    

Example:

# A program to explain the else statement in Python

a = 10 
b = 20

if a>b:
    print('a is greater than b.')
else:
    print('b is greater than a')

Output:

b is greater than a

Inline If Else Statements

Similar to inline if statement, it is also possible to write if and else both statements in the same line. Refer below example.

# A program to demonstrate inline if else statements in Python

a = 10 
b = 20
print('a is greater than b.') if a>b else print('a is less than b.')

Output:

a is less than b.

The elif statement

The elif statement is the intermediate of if and else statements. An elif statement is executed if the condition specified in if statement evaluates to False and you want to check for one more condition before moving to the else statement. If condition specified in if and elif both statement evaluates to False then the else block is executed.

The elif statement syntax:

# Syntax for if..elif
if condition1:
    some code
elif condition2:
    some code
    

# Syntax for if..elif..else
if condition1:
    some code
elif condition2:
    some code
else:
    some code

Example 1:

# A program to demonstrate the if elif statement in Python

a = 10
b = 20

if a>b:
    print('a is greater than b.')
elif a<b:
    print('a is less than b')

Output:

a is less than b

Example 2:

# A program to demonstrate if elif else statements in Python

a = 20
b = 10

if a<b:
    print('a is less than b.')
elif a>b:
    print('a is greater than b.')
else:
    print('a and b are equal.')
    

Output:

a is greater than b.