Skip to content

If-Else Statement in Python

If-else statement in Python

If-Else Statement in Python is very necessary to check any condition in python . Using if-else statement we can check any condition in python and can write code accordingly .

Syntax

if condition :
    statements
else :
    statements

Condition in if statement is either a boolean value or a boolean equivalent .Boolean value means True or False . We can also get boolean value using comparison operator , logical operator , membership operator and identity operator in Python .

To understand about boolean equivalent click here

Structure

  • Any if-else statement in Python starts with if keyword followed by a condition and : (colon)
  • Then we need to write a block of code with proper indentation which will be considered under if block
  • After that we need to write else keyword followed by : without indentation
  • Again we need to write a block of code with proper indentation which will be considered under else block

What is Indentation in Python ?

Indentation in Python is a very important topic . If our python code is not indented perfectly then it will not compile and give us IndentationError .

Indentation means whitespaces at the beginning of a line to define a block of code . In other language (such as c , c++ etc ) we use {} to define a block of code . All the code written between {} will be treated as a block of code . But in python all the code written continuously with same indentation will be treated as a block of code .

Example 1: Program without indentation

# code without indentation
if 10>15:
print("10 is less than 15") 
# we will get an error
  File "C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_2820/3365060296.py", line 3
    print("10 is less than 15")
    ^
IndentationError: expected an indented block

Example 2: Program with default indentation

# if - else statement
if 20>18:
    print("Rajkumar")
    print("You can vote")
else:
    print("Rajkumar")
    print("You can't vote")
Rajkumar
You can vote
  • In the above code 3rd and 4th line is indented after 2nd line (which is if statement) . So , 3rd and 4th line both is under if block . If the condition given in if statement becomes True then 3rd and 4th line will be executed .
  • Similarly 6th and 7th line is indented after 5th line (which is else statement) . So , 6th and 7th line both is under else block . If the condition given in if statement becomes False then 6th and 7th line will be executed .

Note :

By default Python maintains indentation as 4 whitespaces or 1 tab . But we can use any indentation (such as -1 whitespace ,2 whitespaces etc ) . Only we need to remember that indentation should be same through out the code otherwise it will give error .

If-Else Statement Examples

Example 1:

# Declaring age variable
age = 20

# if - else statement
if age>18:
    print("You can vote")
else:
    print("You can't vote")
You can vote
  • In the above code first we have declared a variable age with value 20
  • Then in the if statement as age>18 returned True so it entered in if block and printed You can vote

Example 2:

# Declaring age variable
age = 14

# if - else statement
if age>18:
    print("You can vote")
else:
    print("You can't vote")
You can't vote
  • In the above code first we have declared a variable age with value 14
  • Then in the if statement as age>14 returned False so it entered in else block and printed You can't vote

In the above examples we have worked on boolean values with if statement . Now we will work on boolean equivalent with if statement .

Example 3:

if 20:
    print("Entered in if block")
else:
    print("Entered in else block")
Entered in if block

In the above code as boolean equivalent of 20 is True so code entered in if block and printed Entered in if block .

Example 4:

if 0:
    print("Entered in if block")
else:
    print("Entered in else block")
Entered in else block

In the above code as boolean equivalent of 0 is False so code entered in else block and printed Entered in else block .

Note :

We can also write if statement without else block . else block is optional in if statement .

Example 1:

# Declaring age variable
age = 14

# if - else statement
if age>18:
    print("You can vote")
You can vote
  • In the above code first we have declared a variable age with value 20
  • Then in the if statement as age>18 returned True so it entered in if block and printed You can vote

Example 2:

# Declaring age variable
age = 14

# if - else statement
if age>18:
    print("You can vote")
  • In the above code first we have declared a variable age with value 14
  • Then in the if statement as age>14 returned False so it did not enter in if block and printed nothing .

Short hand If statement

Short hand if statement is one liner of general if statement .

Example :

# short hand if statement
age =20
if age>18:print("You can vote")
You can vote

Short hand If-Else statement

Short hand if-else statement is one liner of general if-else statement .

Example :

# short hand if-else statement
age =20
print("You can vote") if age>18 else print("You can't vote")
You can vote

Note :

This all one liner technique is known as Ternary Operators, or Conditional Expressions.

If-Elif-Else Statement in Python

Using if-else statement we can check only one condition but to check multiple condition in one go we need to use if-elif-else statement

Syntax

if condition :
    statements
elif condition :
    statements
else :
    statements

Example 1:

# Declaring age variable
age = 21

# if-elif-else statement
if age>35:
    print("Your age is more than 35 years")
elif age>30:
    print("Your age is more than 30 years")
elif age>25:
    print("Your age is more than 25 years")
elif age>20:
    print("Your age is more than 20 years")
else:
    print("Your age is less than or equal to 20 years")
Your age is more than 20 years
  • In the above code first we have declared a variable age with value 21 .
  • Then in the if statement as age>35 returned False so it did not enter in if block and went to first elif block to check next condition .
  • As age>30 returned False so it did not enter in first elif block and went to second elif block to check next condition
  • As age>25 returned False so it did not enter in second elif block also and went to third elif block to check next condition
  • As age>20 returned True so it entered in third elif block and printed Your age is more than 20 years .

Short hand If-Elif-Else statement

Short hand if-elif-else statement is one liner of general if-elif-else statement . But in this technique we can not use elif keyword . Instead of elif keyword we can use both if and else keyword .

Example :

# Short hand if-elif-else statement
age = 21
print("Your age is more than 35 years") if age>35 else print("Your age is more than 30 years") if age>30 else print("Your age is more than 25 years") if age>25 else print("Your age is more than 20 years") if age>20 else print("Your age is less than or equal to 20 years")
Your age is more than 20 years

Nested if statement

If we use an if statement inside another if statement , then it is called as nested if statement .

Example 1:

price=4500
rating=4.2
if price<5000:
    if rating>4:
        print("Product is affordable and rating is high")
    else:
        print("Rating is too low")
else:
    print("Product is not affordable")
Product is affordable and rating is high
  • In the above code first we have declared two variable price and rating
  • Then in the outside if-else statement we have checked if price of the product is below 5000 or not .
  • Then we have written another if-else statement which will be triggered whenever price of the product will below 5000 .
  • In the inside if-else statement we have checked if the rating of the product is more than 4 or not
  • As the price is below 5000 and rating is more than 4 so the code printed Product is affordable and rating is high

Short hand Nested If statement

We can also write a nested if statement in a single line

Example :

price=4500
rating=4.2
(print("Product is affordable and rating is high") if rating>4 else print("Rating is too low")) if price<5000 else print("Product is not affordable")
Product is affordable and rating is high

Note :

In this article we have seen how to write different if-else statement in a single line . Sometimes it is good but sometimes it makes our code unreadable . So , we need to use short hand if statement accordingly .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

1 thought on “If-Else Statement in Python”

Leave a Reply