×

Python elif

Python elif

The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them.

Syntax

if expression1:
statement
elif expression2:
statement
elif expression3:
statement
else:
statement

The elif statement can be optional such as else. When the if expression is true, the statement of expression 1 will be printed otherwise control moves to elif expression, if that elif condition evaluates false then control transfers to next elif. It will check all elif expression until it gets a true expression.

Note: Python does not provide a switch or case statement as other programming languages

Flow diagram

Python elif Flow Diagram

 

In the above flow diagram, Python evaluates each expression (i.e., the condition) one by one and if a true condition is found the statement block under that expression will be executed. If the false condition is detected, then statement block under else condition will be executed. In the following example, we have applied if, series of elif, and else to get the type of a variable.

1. Write a program to check the grade of the student

marks = int(input("Enter the marks "))
if marks >85 and marks <= 100:
print("Congrats ! you scored grade A ")
elif marks >60 and marks <= 85:
print("You scored grade B + ")
elif marks >40 and marks <= 60:
print("You scored grade B ")
elif marks >30 and marks <= 40:
print("You scored grade C ")
else:
print("You are fail ")

Output:

Enter the marks 60
You scored grade B

2. Write a program to check whether its morning, noon, or night.

time= int(input("Enter the time"))
if time >= 6 and time <12:
print("Good Morning")
elif time == 12:
print("Good Noon")
elif time >12 and time <= 17:
print("Good Afternoon")
elif time >17 and time <= 20:
print("Good Evening")
elif (time >20 and time <=24) or (time >= 0 and time <=6):
print("Good Night")
else:
print("Invalid time!")

Output:

Enter the time12
Good Noon

3. Write a program of the simple calculator

a=int(input("Enter the first number"))


b=int(input("Enter the second number"))


print("1. Add")


print("2. Sub")


print("3. Mul")


print("4. Div")


choice=input("Enter your choice")


if(choice=='1'):


print("The addition is:",a+b)


elif(choice=='2'):


print("The subtracion is:",a-b)


elif(choice=='3'):


print("The multiplication is:",a*b)


elif(choice=='4'):


print("The divide is:",a/b)


else:


print("Enter the valid choice")

Output:

Enter the first number:30
Enter the second number:20
1. Add
2. Sub
3. Mul
4. Div
Enter your choice:3
The multiplication is: 600 

4. Write a program to check the type of triangle.

a=int(input("Enter the first angle:"))
 
b=int(input("Enter the second angle:"))
 
c=int(input("Enter the third angle:"))
 
if a<b+c or="" b<a+c="" c<a+b:="" print("the="" triangle="" does="" not="" exist")="" elif="" a="=b" and="" #an="" equilateral="" has="" three="" angles="" of="" 60°,="" equal="" sides.="" print("equilateral="" triangle")="" b="=c" c="=a:" #="" an="" isosceles="" two="" that="" are="" equal,="" print("isosceles="" else:="" obtuse="" one="" only="" angle.="" print("obtuse="" <="" pre=""></b+c>

Output:

Enter the first angle: 60
Enter the second angle:60
Enter the third angle:45
Isosceles triangle 

Related Topics

Python pynmea2

Define Pynmea2 Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in...

4 minutes read.

Multiprocessing in python

The word multiprocessing is used to compute the operation in which more than two processors run simultaneously with more than one central processor. The computer's performance increases gradually. Multiprocessing is...

6 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Python Examples

In this tutorial, we will see some examples related to python. This will include some basic example questions and their code in Python. Write a program to print “Hello Python”. print(‘Hello Python’) Output Hello...

5 minutes read.

Python OOPs: Class, Object, Inheritance and Constructor

Basic Object-Oriented Programming (OOP) Concepts in Python Python is similar to most general-purpose programming languages with an Object-Oriented environment system since its existence. Being an Object-Oriented Programming language, Python provides ease...

9 minutes read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

Importing Numpy in Pycharm

Numpy : Numpy is one of the important library in python. The abbreviation of numpy is “Numerical python” or “Numeric Python”. This library is mainly used to access arrays. Numpy was created...

5 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.