Python If else statement is a conditional statement that helps in decision making in Programming languages. If, else, and else if are statements that help in making crucial decisions when you are writing code.
In this blog, we will learn the uses of Python If else statements using some examples of if else statements.
If Statement Definition

The If statement is a keyword followed by the condition. When the condition satisfies the if statement inside the conditional statement is executed. While if the condition is not satisfied the execution stays outside the if statement.
First, the execution starts with checking the condition followed by if statement. If the statement followed by the condition is true, then the body of the if statement is executed while if the condition is not true then the code outside the if statement is evaluated.
Let us take a simple example to understand the concept of Python if statement below.
age = 25
if age >=18: print (“You are an adult.”) |
Now, checking in this statement the variable “age” is set to 25 which is greater than 18 and hence satisfies the given if condition. Hence, we will execute the if statement and print the output with “You are an adult”. Check the output below.
Output
![]() |
Syntax of Python If Else Statement
Let us first check the syntax of the Python If else statement below.
if condition:
# Code to execute if the condition is True else: # Code to execute if the condition is False |
In the statement above you can check the if condition and else statement. When the if condition satisfies then the code is executed. However, when the if condition does not satisfy then the else condition is executed.
- The if condition is the beginning of the conditional statement. Python evaluates whether or not the condition statement is either true or false.
- The line containing the if keyword must end with a colon as it starts the if block.
- Else statement is optional block which will be executed when the if condition is false and must be indented.
if (some condition is true):
do these things else: do these other things |
Make sure that the else block is executed only when the if condition evaluates to false. When the if condition is true the if block is executed and the else block is skipped.
Python If Else Statement: Syntax & Examples
The Python If else statement is used to check for a condition which when satisfied then the if statement block is executed. However, if the condition is not satisfied then the else condition statement is executed. Let us check an example of Python if else statement below.
When the if condition is false then the body of else statement is executed. Let us understand the following with a simple example below.
temperature = 15
if temperature > 20: print(“It’s warm!”) else: print(“It’s a bit chilly.”) |
In the example above where the temperature variable is 15, we check the if condition whether or not it is greater than 20. We can see the temperature is not greater than 20 and hence the if statement is not executed and we execute the else statement and print “It is a bit chilly.”
Output
![]() |
Nested If Statement In Python Programming: Syntax
The nested if statement comprises an if statement inside an if statement. This helps you check for multiple levels of condition in Python conditional statements.
Let us now check the complete nested if statement in Python programming below.
if condition1:
# Code to execute if condition1 is True if condition2: # Code to execute if condition1 is True AND condition2 is True else: # Code to execute if condition1 is True AND condition2 is False else: # Code to execute if condition1 is False if condition3: # Code to execute if condition1 is False AND condition3 is True else: # Code to execute if condition1 is False AND condition3 is False |
In the above syntax of Python if else statement we can see there are multiple if, elif, and else statements within the blocks of outer if and else statements. The indentation is crucial here to define which block of code belongs to which condition in the condition.
Let us take a simple example to understand the nested if statement in Python below.
number = 10
if number > 0: print(“The number is positive.”) if number % 2 == 0: print(“And it is even.”) else: print(“And it is odd.”) else: print(“The number is not positive.”) |
In the above example, the number variable is 10 the outer if statement checks whether the number is greater than 0, if it is greater than 0 then we print the statement that the given value is positive. Now, the inner if check if the number is divisible by 2 and if it is then we print that the value is even.
However, if the condition fails the output is the number getting odd. Also, if the number is not greater than 0 then the output will print that the number is not positive.
Output
![]() |
If we had kept the value of the number as 7, then the statement would have generated an output as The number is positive and it is odd.
![]() |
If Elif-Else Statement In Python Programming: Syntax & Examples
In this type of if else statement there is an “elif” condition which checks for a second condition after the if statement when it gets false. When the if statement is evaluated to false then the elif condition is evaluated and when the elif condition is also false, the else statement is evaluated.
if (condition):
statement elif (condition): statement . , else: statement |
In this syntax the if-elif-else ladder works starting with if statement and condition is checked and if the condition is true, the body is executed while if it gets false then the elif condition is checked and evaluated. However, if the elif condition is also false then the body of the else condition is evaluated.
This flowchart depicts the working of the If-elif- else statement in Python programming language. Let us understand the working of if-elif-else statements more precisely using an example.
Read More: Apply for Python Internships for Freshers in April 2025
score = 85
if score >= 90: grade = “A” elif score >= 80: grade = “B” elif score >= 70: grade = “C” elif score >= 60: grade = “D” else: grade = “F” print(f”The grade for a score of {score} is: {grade}”) |
In the above example, we are checking the score starting with 85. Now the first condition if the condition is checked for a statement (score >=90) is false.
Now, we will check if the condition is greater than or equal to 80. Similarly, we keep on checking till the grade “F” for the marks. Now, we will print the grade for a score using the score value in Python if else statement.
What Happens When the If Statement Condition Fails?
In the Python If else statement when the if condition fails then the statement inside the if statement is not evaluated and the statement outside the if block is executed.
If there is an else statement then the else statement is executed. While, if there is an elif condition then it is evaluated and checked whether the elif condition satisfies.
Can There Be More Than One If Statement In Python?
Yes there can be more than one Python if else statement in case of nested Python if else statement. In this type of conditional statement the program is checked for multiple conditions which are divided into outer and multiple inner conditions. In these Python if else statement the if statements are dependent on each other for execution.
One of the most important criteria that you need to take care of is indentation. Make sure that there are no indentation error as it might fail the if else execution.
Read More: Deloitte Data analytics Certification With Stipend For Freshers
What Does “==” Means In Python Programming?
The “==” operator is mostly used with the Python if else statement used to check whether or not a condition satisfies for the given condition. Let us take an example to understand it completely.
number = 10
if number > 0: print(“The number is positive.”) if number % 2 == 0: print(“And it is even.”) else: print(“And it is odd.”) else: print(“The number is not positive.”) |
We took this example above where we are checking whether or not the number is even. To do this we use the modulus operator to check whether or not the number is even. if number % 2 == 0: This statement makes use of the “==” operator which checks whether the result of the modulus operator comes to 0. If it does then the number is even.
Master Data Analytics & Python with PW Skills
Become a master in extracting data and performing operations based on data such as cleaning, processing, and other operations with PW Skills Data Analytics Course. Learn to use Python in extracting data from the dataset. Master top tools like SQL, Python, Django, mySQL, Power BI, AWS, Jupyter, Pandas, and more.
Get the latest industry curriculum with live sessions every weekend. Get capstone projects, practice exercises, assignments, and more. Get career guidance and prepare for an interview with a Data analytics course on PW Skills.
Python If else statement FAQs
Q1. What is the if else statement in Python?
Ans: The Python if else statement is a conditional statement used to check for a condition based on if and else statement. When the if condition gets true the block of statement inside the if block is executed. However, when the if statement fails then the block statement inside the else is printed or evaluated.
Q2. What does if mean in Python?
Ans: The if statement in Python is used to check for an statement which if true evaluates the statement inside the if block while if the condition fails then the statement inside the if block is not executed.
Q3. What is if elif statement in Python?
Ans: The if elif statement in Python programming language is used to check for if condition which when true evaluates the statement inside the if block. The elif statement checks for another condition and if it get fails then the else statement is executed.
Q4. What are the four conditional statements in Python?
Ans: The four conditional statements in Python are if, if else, if-elif-else conditions and nested if statement.