×

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

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

N2 in Python

N2 is known as Nearest Neighbor Algorithm, because it contains 2 N's (N-Nearest, N-Neighbor). This is a library in the python build using C++ and Python. Before N2 was made,...

3 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

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.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Python Skyline

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.

Compound Interest GUI Calculator using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.