Statements in Python

When our program has a requirement to use condition statements are used.

There are different types of statements depending upon how much conditions are where the program requires to apply conditions.

  • if statement
  • if-else statement
  • if elif statement (Note: Yes it is correct elif, python does not use keyword else if like C, C++, Java, JavaScript)
  • Nested if statement


Python does not use of switch statement.


If statement in python

When the program has only one condition and related output if statement is used.


save the file as

Program: Enter a new number and check number is even.

num=int(input("Enter new number:"))
   print("{} is even number.".format(num))


Enter a new number: 12
12 is even number

Enter a new number: 19 (In this conditioning program will not have any output because the program has only one condition and applied using if statement and one output which will show output if the first condition will true otherwise there is no else statement and containing not output statement, so there will no be program output.)


If-else statement

When a program has one condition and related two outputs, the if-else statement can be used.


The program to check number is even or odd.

num=int(input("Enter a new number: "))
   print("Even number")
   print("Odd number")


Enter a new number: 21
Odd number
Enter a new number: 22
Even number

If elif statement

When a program has multiple conditions and related outputs if elif statement can be used.


The program to display grade as corresponding marks range as shown below.

Marks Range Output(Grade)
>90 A
>=80 B
>=70 C
>=60 D
<60 E

marks=int(input("Enter computer subject marks:"))
if (marks>90):
     print("Grade A")
elif (marks>=80):
     print("Grade B")
elif (marks>=70):
     print("Grade C")
elif (marks>=60)
     print("Grade D")
else :
     print("Grade E")



Enter computer subject marks: 94
Grade A

Enter computer subject marks: 85
Grade B

Enter computer subject marks: 78
Grade C

Enter computer subject marks: 62
Grade D

Enter computer subject marks: 45
Grade E


Nested if statement

When a program have a condition within a condition nested statement can be used, in simple words a condition within other condition.


A program to find out largest number from three given numbers (Note: all numbers must be different integers)

num1=int(input("Enter first number: "))
num2=int(input("Enter second number: "))
num3=int(input("Enter third number: "))

if (num1>num2):
     if (num1>num3):
          print("{} is largest number". format(num1))
          print("{} is largest number". format(num3))
elif (num2>num1):
          print("{} is largest number". format(num2))
         print("{} is largest number". format(num3))


Enter first number: 12
Enter second number: 13
Enter third number: 14
14 is largest number     

Post written by: Vijay Goyal
© 2020 - 2020 All rights are reserved