Python – if, elif, else Conditions

  • December 3, 2022
  • python
JOIN Clause

Python’s conditional statements carry out various calculations or operations according to whether a particular Boolean constraint evaluates to true or false. In Python, if statements deal with conditional statements.

The conditional statements that Python offers are listed below.

  • if
  • if..else
  • Nested if
  • if-elif statements.
if Statement:

To make decisions, use the if statement in Python. It has a body of code that only executes when the if statement’s condition is met. The optional else statement, which includes some code for the else condition, runs if the condition is false.

Python’s if-else statement is used when you want to justify one condition while the other is false.


if condition:           
   # Statements to execute if
   # condition is true
  • The if keyword and the conditional expression are separated by a colon.
  • [conditional expression] introduces a conditional expression that is supposed to return a boolean value, i.e., True or False.
  • If the result is True, the [statement to execute] is executed,
  • Note: Indentation is very important.


# if statement example
if 1 > 5:
    print("1 greater than 5")

print("Program ended")


Program ended
if..else Statement:

When the if condition is false, the additional block of code is combined as an else statement, which is executed.


if (condition):
    # Executes this block if
    # condition is true
    # Executes this block if
    # condition is false


price = 69
quantity = 5
if price*quantity < 500:
    print("Total is less than 500")
    print("Total is more than 500")


Total is less than 500
Nested if Statements:

If statements can be checked inside other if statements. This type of conditional statement is known as a nested if statement. This means that the inner if condition will be checked only if the outer if condition is true, and we can see that multiple conditions must be satisfied as a result.


if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here


# Nested if statement example
a = 11

if a > 5:
    print("Bigger than 5")
    if a <= 25:
    	print("Between 5 and 15")


Bigger than 5
Between 5 and 15
If-elif Statement:

The if-elif statement is a shorthand for the if…else chain. When using an if-elif statement, an else block is added at the end that is executed if none of the preceding if-elif statements are true.


if (condition):
elif (condition):


price = 109

if price > 100:
    print("price is greater than 100")
elif price == 100:
    print("price is 100")
elif price < 100:
    print("price is less than 100")


price is greater than 100

Note: also read about Relational & Logical Operators in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

Leave a Reply

Your email address will not be published. Required fields are marked *