Python3 – if , if..else, Nested if, if-elif statements
Last Updated :
09 Aug, 2024
There are situations in real life when we need to do some specific task and based on some specific conditions, we decide what we should do next. Similarly, there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases, conditional statements can be used. The following are the conditional statements provided by Python.
- if
- if..else
- Nested if
- if-elif statements.
Let us go through all of them.
if Statement in Python
If the simple code of block is to be performed if the condition holds true then the if statement is used. Here the condition mentioned holds then the code of the block runs otherwise not.
Python if Statement Syntax
Syntax: if condition:
# Statements to execute if
# condition is true
Flowchart of if Statement in Python
Below is the flowchart by which we can understand how to use if statement in Python:

Example: Basic Conditional Check with if Statement
In this example, an if statement checks if 10 is greater than 5. If true, it prints “10 greater than 5”; regardless, it then prints “Program ended” as the next statement, indicating the program flow.
Python
# if statement example
if 10 > 5:
print("10 greater than 5")
print("Program ended")
Output10 greater than 5
Program ended
Indentation(White space) is used to delimit the block of code. As shown in the above example it is mandatory to use indentation in Python3 coding.
if else Statement in Python
In conditional if Statement the additional block of code is merged as else statement which is performed when if condition is false.
Python if-else Statement Syntax
Syntax: if (condition): # Executes this block if # condition is trueelse: # Executes this block if # condition is false
Flow Chart of if-else Statement in Python
Below is the flowchart by which we can understand how to use if-else statement in Python:

Example 1: Handling Conditional Scenarios with if-else
In this example, the code assigns the value 3 to variable x and uses an if..else statement to check if x is equal to 4. If true, it prints “Yes”; otherwise, it prints “No,” demonstrating a conditional branching structure.
Python
# if..else statement example
x = 3
if x == 4:
print("Yes")
else:
print("No")
Example 2: Nested if..else Chain for Multiple Conditions
You can also chain if..else statement with more than one condition. In this example, the code uses a nested if..else chain to check the value of the variable letter. It prints a corresponding message based on whether letter is “B,” “C,” “A,” or none of the specified values, illustrating a hierarchical conditional structure.
Python
# if..else chain statement
letter = "A"
if letter == "B":
print("letter is B")
else:
if letter == "C":
print("letter is C")
else:
if letter == "A":
print("letter is A")
else:
print("letter isn't A, B and C")
Nested if Statement
if statement can also be checked inside other if statement. This conditional statement is called a nested if statement. This means that inner if condition will be checked only if outer if condition is true and by this, we can see multiple conditions to be satisfied.
Python Nested If Statement Syntax
Syntax: if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # if Block is end here# if Block is end here
Flow chart of Nested If Statement In Python
Below is the flowchart by which we can understand how to use nested if statement in Python:

Example: Managing Nested Conditions for Refined Control
In this example, the code uses a nested if statement to check if the variable num is greater than 5. If true, it further checks if num is less than or equal to 15, printing “Bigger than 5” and “Between 5 and 15” accordingly, showcasing a hierarchical condition for refined control flow.
Python
# Nested if statement example
num = 10
if num > 5:
print("Bigger than 5")
if num <= 15:
print("Between 5 and 15")
OutputBigger than 5
Between 5 and 15
if-elif Statement in Python
The if-elif statement is shortcut of if..else chain. While using if-elif statement at the end else block is added which is performed if none of the above if-elif statement is true.
Python if-elif Statement Syntax:-
Syntax: if (condition): statementelif (condition): statement..else: statement
Flow Chart of Python if-elif Statement
Below is the flowchart by which we can understand how to use elif in Python:

Example: Sequential Evaluation with if-elif-else Structure
In this example, the code uses an if-elif-else statement to evaluate the value of the variable letter. It prints a corresponding message based on whether letter is “B,” “C,” “A,” or none of the specified values, demonstrating a sequential evaluation of conditions for controlled branching.
Python
# if-elif statement example
letter = "A"
if letter == "B":
print("letter is B")
elif letter == "C":
print("letter is C")
elif letter == "A":
print("letter is A")
else:
print("letter isn't A, B or C")
Can We Use Elif in Nested If?
Yes, you can use elif within nested if statements in Python. This allows for more complex decision structures within a branch of another decision. For example:
x = 10
y = 5
if x > 5:
if y > 5:
print("x and y are greater than 5")
elif y == 5:
print("x is greater than 5 and y is 5")
else:
print("x is greater than 5 and y is less than 5")
This structure provides conditional checks within another conditional check, enhancing the decision-making capabilities of your code.
Are You Allowed to Nest If Statements Inside Other If Statements in Python?
Yes, you are allowed to nest if statements inside other if statements in Python. This is a common practice used to make more complex conditional logic possible. Nested if statements can be as deep as you need, although deep nesting can make your code harder to read and maintain.
Python3 – if , if..else, Nested if, if-elif statements – FAQs
Can We Use Multiple If Instead of Elif?
Yes, you can use multiple if statements instead of elif, but the behavior of your code will change. elif allows for mutually exclusive conditions; only one branch can execute. With multiple if statements, each if condition is checked independently of others, so multiple branches might execute.
Example:
x = 10
y = 5
if x > 5:
if y > 5:
print("x and y are greater than 5")
elif y == 5:
print("x is greater than 5 and y is 5")
else:
print("x is greater than 5 and y is less than 5")
Using elif, the second condition would only be checked if the first condition failed.
What is the Difference Between if-else and Nested If Statements in Python?
- if-else: Refers to a simple conditional structure where if a condition is true, the
if block executes; otherwise, the else block executes. There is only one alternative path. - Nested if statements: Involve placing one or more
if or if-else structures inside another if or else block. This setup is used to test for further conditions after a previous condition has been found to be true.
What is the Maximum Number of Elif Clauses You Can Have in a Conditional?
Python does not explicitly limit the number of elif clauses you can have after an if statement. The practical limit is generally dictated by the readability and complexity of your code. Technically, you could have hundreds or even thousands, but this would be poor practice in terms of code clarity and maintainability. If you find yourself using many elif statements, consider refactoring your code, possibly using a different data structure like a dictionary to map conditions to functions or outcomes, or using a different logic flow altogether.
Similar Reads
Python3 - if , if..else, Nested if, if-elif statements
There are situations in real life when we need to do some specific task and based on some specific conditions, we decide what we should do next. Similarly, there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases, conditional stat
7 min read
Nested-if statement in Python
For more complex decision trees, Python allows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively. Python Nested if StatementA nested if statement in Pytho
2 min read
Check multiple conditions in if statement - Python
If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true. Syntax: if (condition): code1else: code2[on_true] if [expression] else [on_false]Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-eli
4 min read
Python - Multi-Line Statements
In this article, we are going to understand the concept of Multi-Line statements in the Python programming language. Statements in Python: In Python, a statement is a logical command that a Python interpreter can read and carry out. It might be an assignment statement or an expression in Python. Mul
3 min read
Python If Else in One Line
In Python, if-else conditions allow us to control the flow of execution based on certain conditions. While traditional if-else statements are usually written across multiple lines, Python offers a more compact and elegant way to express these conditions on a single line. Python if-else in One LineIn
3 min read
Conditional Statements in Python
Conditional statements in Python are used to execute certain blocks of code based on specific conditions. These statements help control the flow of a program, making it behave differently in different situations. If Conditional Statement in PythonIf statement is the simplest form of a conditional st
5 min read
How to use if, else & elif in Python Lambda Functions
Lambda function can have multiple parameters but have only one expression. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to use if, else & elif in Lambda Functions. Using if-else in lambda functionThe lam
2 min read
Python If Else Statements - Conditional Statements
In Python, If-Else is a fundamental conditional statement used for decision-making in programming. If...Else statement allows to execution of specific blocks of code depending on the condition is True or False. if Statementif statement is the most simple decision-making statement. If the condition e
4 min read
Python Match Case Statement
Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement
8 min read
Different Ways of Using Inline if in Python
Python offers a concise and expressive way to handle conditional logic in your code by using inline if. Whether you need an essential conditional expression or want to nest multiple conditions, inline can make your code more readable and maintainable. Among these tools is the inline if statement, an
3 min read
Using Else Conditional Statement With For loop in Python
Using else conditional statement with for loop in python In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops. The else block just after for/while
2 min read
Jump Statements in Python
In any programming language, a command written by the programmer for the computer to act is known as a statement. In simple words, a statement can be thought of as an instruction that programmers give to the computer and upon receiving them, the computer acts accordingly. There are various types of
4 min read
Python pass Statement
Pass statement in Python is a null operation or a placeholder. It is used when a statement is syntactically required but we don't want to execute any code. It does nothing but allows us to maintain the structure of our program. Example Use of Pass Keyword in a FunctionPass keyword in a function is u
3 min read
Python Continue Statement
Python Continue Statement skips the execution of the program block after the continue statement and forces the control to start the next iteration. Python Continue StatementPython Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the
4 min read
Alternatives to Case/Switch Statements in Python
In many programming languages, the case or switch statement is a control flow mechanism that allows a variable to be tested for equality against a list of values, with each value associated with a block of code to be executed. However, Python does not have a built-in case or switch statement. Instea
3 min read
Check if element exists in list in Python
In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example: [GFGTABS] Python a = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Eleme
3 min read
How to Use IF Statement in MySQL Using Python
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to use if statements in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python
2 min read
Check If Value Is Int or Float in Python
In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer
4 min read
Lambda with if but without else in Python
In Python, Lambda function is an anonymous function, which means that it is a function without a name. It can have any number of arguments but only one expression, which is evaluated and returned. It must have a return value. Since a lambda function must have a return value for every valid input, we
3 min read