×

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 for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Python Scikit-image | Image Processing Using Scikit-Image

What is Image Processing? The world is defined with images and, every image has its different specialties. An image can contain much-needed information that can be helpful in various ways. The process...

4 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

4 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

SKLearn Clustering

These are ml methods thatare responsible for detecting patterns and the similarities within the data.The clustering methods are unsupervised.Here the data is clustered to form groups with the help of...

3 minutes read.

How to Concat two Dataframes in Python

Using Pandas dataframe, we can concat two dataframes or series in Python. So let's take a brief introduction to what is Pandas in Python. Pandas is a library typically used for...

7 minutes read.

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Periodogram in Python

Python:  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 is said...

3 minutes read.

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep...

2 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

Curdir Python

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 a...

3 minutes read.

Python getattr() function

Python getattr() function The getattr() function in Python returns the value of the named attribute for the given object. Syntax getattr(object, name[, default]) Parameter object: It is a required parameter which represents an object. name: This parameter represents the...

1 minute read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 minutes read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

FOO 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.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.