Python If Else Statements – Conditional Statements
In Python, If-Else is fundamental conditional statement used for decision making in programming. If..Else statement allows to execute specific blocks of code depending on the condition is True or False.
If Statement
if statement is the most simple decision-making statement. If condition execute certain statement if given condition is met.
# Python code to understand If statement
i = 10
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
If Else Statement
if…else statement is a control statement that helps in decision-making based on specific conditions. It allows you to execute specific blocks of code based on whether a given condition is True or False.
Syntax of If Else in Python
if (condition):
# Executes this block if condition is true
else:
# Executes this block if condition is false
Example:
# python program to understand if-else
i = 20
if (i > 0):
print("i is positive")
else:
print("i is 0 or Negative")
If Else in One-line
If we need to execute a single statement inside the if or else block then one-line shorthand can be used.
number = -2
result = "Positive" if number >= 0 else "Negative"
print(result)
Using Logical Operators with If..Else
We can combine multiple conditions using logical operators such as and, or, and not.
age = 25
experience = 10
# Using > & 'and' with IF..ELSE
if age > 23 and experience > 8:
print("Eligible.")
else:
print("Not eligible.")
Nested If Else Statement
Nested if…else statement occurs when if…else structure is placed inside another if or else block. Nested If..else allows the execution of specific code blocks based on a series of conditional checks.
In this example, we are showing nested if conditions in the code, All the If condition in Python will be executed one by one.
# python code to illustrate nested If statement
i = 10
if (i == 10):
# First if statement
if (i < 15):
print("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
else:
print("i is not equal to 10")
If-Elif-Else condition
if-elif-else statement in Python is used for multi-way decision-making. This allows you to check multiple conditions sequentially and execute a specific block of code when a condition is True. If none of the conditions are true, the else block is executed.
# Python program to illustrate if-elif-else ladder
i = 25
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")
Flowchart of Elif Statement in Python
Let’s look at the flow of control in if-elif-else ladder:

Flowchart of if-elif-else ladder
Similar Reads:
- Python3 – if , if..else, Nested if, if-elif statements
- Using Else Conditional Statement With For loop in Python
- How to use if, else & elif in Python Lambda Functions
Python If Else Statements – Conditional Statements – FAQs
What is the conditional statement of if-else?
The
if-elsestatement in Python is used to control the flow of the program based on a condition. It has the following syntax:if condition: # Execute this block if condition is True else: # Execute this block if condition is FalseFor example:
x = 10 if x > 5: print("x is greater than 5") else: print("x is not greater than 5")
How many else statements can a single if condition have in Python?
A single
ifcondition can have at most oneelsestatement. However, you can have multipleelif(else if) statements to check additional conditions if needed:x = 10 if x > 15: print("x is greater than 15") elif x > 5: print("x is greater than 5 but not greater than 15") else: print("x is 5 or less")
What are the different types of control statements in Python?
In Python, control statements are used to alter the flow of execution based on specific conditions or looping requirements. The main types of control statements are:
- Conditional statements:
if,else,elif- Looping statements:
for,while- Control flow statements:
break,continue,pass,return
What are the two types of control statements?
The two primary types of control statements in Python are:
- Conditional statements: Used to execute code based on certain conditions (
if,else,elif).- Looping statements: Used to execute code repeatedly until a condition is met (
for,while).
Are control statements and conditional statements the same?
No, control statements and conditional statements are not exactly the same.
- Conditional statements (
if,else,elif) specifically deal with checking conditions and executing code based on whether those conditions areTrueorFalse.- Control statements encompass a broader category that includes both conditional statements (
if,else,elif) and looping statements (for,while), as well as other statements (break,continue,pass,return) that control the flow of execution in a program.


