Home »
Python
Conditional statements (if, if...else, if...elif...else, and nested if) in Python
Python conditional statements: In this tutorial, we are going to learn about the conditional statements (if, if...else, if...elif...else, and nested if) with examples.
By Bipin Kumar Last updated : December 18, 2023
Python Conditional Statements
Conditional statements decide the flow of program execution. In programming whenever we need to make execute any special blocks based on the decision then we use the conditional statements.
Types of Python Conditional Statements
Conditional statements available in the Python are:
- if statements
- if...else statements
- if...elif...else statements
- Nested if statements
1) Python if Statement
It is one of the most common conditional statements in which some conditions are provided and if the condition is true then block under the if the condition will be executed.
Syntax
Below is the syntax of Python if statement:
if condition:
# what we want to execute here.
Example of Python if statement
# input age
age=int(input('what is your age: '))
# checking the condition
if age>=18:
print('You are Grown-up now !')
Output
RUN 1:
what is your age: 21
You are Grown-up now !
RUN 2:
what is your age: 15
2) Python if...else statement
In the above, we have seen that if the condition is true then block under if will execute then one thing is, comes to our mind that what happens when the condition will be false. So, to overcome this problem we are using if...else statements.
Syntax
Below is the syntax of Python if else statement:
if condition:
# what we want to execute here.
else:
# what we want to execute here.
If the condition is true, then it will execute the block of if statements otherwise else statement.
Example of Python if else statement
# input age
age=int(input('what is your age: '))
# checking the condition
if age>=18:
print('You are Grown-up now !')
else:
print('You are Young!')
Output
RUN 1:
what is your age: 21
You are Grown-up now !
RUN 2:
what is your age: 15
You are Young!
3) Python if...elif...else Statement
These conditional statements use where we have to check multiple conditions in the program. If these will not true that is false then the else blocks only execute.
Syntax
Below is the syntax of Python if elif else statement:
if condition:
# what we want to execute here.
elif conditions:
# what we want to execute here.
else:
# what we want to execute here.
Example of Python if elif else statement
# input the age
n=int(input('Enter marks: '))
# checking the conditions
if n>=90:
print('Excellent')
elif n<90 and n>=75:
print('Passed')
else:
print('Fail')
Output
RUN 1:
Enter marks: 95
Excellent
RUN 2:
Enter marks: 80
Passed
RUN 3:
Enter marks: 63
Fail
4) Python Nested if Statements
As we all have familiar with the word nested which means one statement inside another statement same in the programming nested if statements mean an if statement inside another if statement that is we can place one if statements inside another if statements.
Syntax
if condition:
# what we want to execute here.
if condition:
# what we want to execute here.
else:
# what we want to execute here.
Note: In Python, true and false are written as True and False. Since Python follow the indentation rule so the statements must write under the indentations of if statements or other.
Now, we will see an example based on the above conditional statements which will show us the grade of students.
Example of Python nested if statement
# input the age
n=int(input('Enter marks: '))
# checking the conditions
if n>=75:
if n >=95:
print('Excellent')
else:
print('Pass')
else:
print('Fail')
Output
RUN 1:
Enter marks: 96
Excellent
RUN 2:
Enter marks: 89
Pass
RUN 3:
Enter marks: 69
Fail
Python Tutorial