Ever thought how would I implement a certain block of code if it satisfies certain condition? If, else and elif are conditional statements in Python. The if statement gives a specific condition to the code. If the program satisfies that condition, then the program get executed otherwise it will jump to else statement.

The syntax is :-

if expression:
 statement(s)
elif expression:
 statement(s)
else:
 statement(s)

An else statement can be combined with an if statement. An else statement contains the block of code that executes only if the conditional expression in the if statement resolves to a false value.

The else statement is an optional statement and there can be at most one else statement following the if statement.

There can be as many elif statements. Elif is stands for else if statement in a program. Just like else, elif is also an optional statement. There can only be one if statement but as many elif statements as you want. If there are more than one true elif statements, then the program will run the first one and the first one will get executed.

Example:

people = int ( input ( "enter no. of people: " ) )
cars = int ( input ( "enter no. of cars: " ) )
trucks = int ( input ( "enter no. of trucks: " ) )
if cars > people:
  print ( "We should take the cars." )
elif cars < people:
  print ( "We should not take the cars. people are more." )
else:
  print ( "We can't decide whether we should take cars or not. " )
if trucks > cars:
  print ( "That's too many trucks." )
elif trucks < cars:
  print ( "Maybe we could take the trucks." )
else:
  print ( "We still can't decide. trucks are equal to cars" )
if people > trucks:
  print ( "Alright, let's just take the trucks." )
else:
  print ( "Fine, let's stay home then." )

Output:

enter no. of people: 35
enter no. of cars: 13
enter no. of trucks: 5
We should not take the cars. people are more.
Maybe we could take the trucks.
Alright, let's just take the trucks.

Nested If Statement

Sometimes, you want to check another condition if a condition is true. Then the nested if is the most useful construct. In nested if statement, there are more than one if statements under a single if statement.

The syntax for nested if construct is,


if expression1:
  statement(s)
  if expression2:
    statement(s)
  elif expression3:
    statement(s)
  else:
    statement(s)
elif expression4:
  statement(s)
else:
  statement(s)

In a nested-if construct, the first if statement will get executed. If the first condition is true then it will move to the condition inside the statement. Then it will check whether the condition is true or false. If the condition is true, then the statement inside the inner condition will get executed otherwise it will jump to the outer elif statement or else statement. The python program operates from top to bottom. So the first condition will get executed first and the last one will get executed at last.

Example:

var = int ( input ("enter the number: "))
if var < 100:
 print("value is less than 100")
  if var == 50:
    print("value is 50")
  elif var == 10:
    print("value is 10")
  elif var == 5:
    print("value is 5")
  elif var < 50:
    print("value is less than 50")
else:
  print("value is greater than 100")

Output:

enter the number: 5
value is less than 100
value is 5

Akarsh Singh
Follow me

Akarsh Singh

Founder, Code in Python. Web and Android App Developer. He loves Python and wants to share his knowledge via this website. Follow him on Facebook, Twitter and Github.
Akarsh Singh
Follow me

Latest posts by Akarsh Singh (see all)