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

The code which is not part of the indented block is always executed. Refer to 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 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 the 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 the inline if statement, it is also possible to write if and else both statements in the same line.

# 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.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

Leave a Comment