In this article, We are going to use if-elif-else conditional statements. In the last article, I discussed the Python if-else conditional statement and provided some excellent practice examples. There are just two criteria in the if-else conditional expression. If the first condition is true, then the first statement should be execute; if the condition is false, then the other (else condition) statement should be execute. But what if we have more than one condition? How do we handle that? Don’t worry, Python already provides a solution to this issue by using if-elif-else conditional statements.
If you’re unfamiliar with Python’s conditional statements, don’t worry. When we look at some good examples, everything will become clear.
Let’s have a look at an example. In this example, we’ll award grades to students depending on their performance:
Range Grade [90, 100] A [70, 89] B [50, 69] C [40, 49] D [0, 39] F
You must utilize the user’s input to get the student’s grades. The grades are then assigned depending on the students’ grades.
The code of if-elif-else:
print("Grade List") print ("==========") def gradeAssign(marks): assert marks>=0 and marks<=100 if marks>=90: grade="A" elif marks>=70: grade="B" elif marks>=50: grade="C" elif marks>=40: grade="D" else: grade="F" return grade def main(): marks = float(input('Enter your marks: ')) print("Marks: ", marks, "\nGrade: ", gradeAssign(marks)) main()
The output of the above Python program is:
>>> Grade List ========== Enter your marks: 91 Marks: 91.0 Grade: A >>> Grade List ========== Enter your marks: 68.2 Marks: 68.2 Grade: C >>> Grade List ========== Enter your marks: 33 Marks: 33.0 Grade: F
To do this, I utilized a Python function. Because the main function is called last (at the end), it will run first. In the main function, I first accept the user’s marks as input, which will be in float form. Because some students may receive grades such as “65.5,” “83.7,” “88.2,” and so on. As a result, we can’t utilize the int function in this case. Our task will be completed using the float function. After that, I report the results and use the gradeAssign() method to assign grades.
I used the assert statement in the gradeAssign() method. If the supplied condition evaluates to True, the assert statement in Python is used to continue the execution. If the assert condition is False, the AssertionError exception is thrown, with the supplied error message. The only condition is that the grades be in the range of 0 to 100. If the user input values are between 0 and 100, the Python application will throw an AssertionError error.
Following that, I used a conditional statement. The grade will be “A” if the marks collected are more than “90.” Otherwise, the grade will be “B” if (elif) the marks collected are larger than “70.” Continue doing so till we attain the grade “F” condition. You’ll observe that for else conditions, we don’t need to define any statements. If all of the if and elif conditions fail, the function will fall back to the else conditional expression.
General Form of if-elif-else Conditional Statement:
if < condition1 >: < Statement s1 to be executed > elif < condition2 >: < Statement s2 to be executed > elif <condition3 >: < Statement s3 to be executed > . . . . else: < Statement s(n) to be executed >
Nested if-elif-else Conditional Statement
We require frequent a control structure within a control structure. Nesting is a term for this type of mechanism.
With the help of an example, we will be capable of understanding this. We’re going to discover the maximum of three integers in this example. This implies that we will accept three integers from the user and our computer will determine which number is the highest. Isn’t it simple?
Let’s start the code:
print("Find the maximum Number") print ("==========") def FindMaximum(n1,n2,n3): if n1>n2: if n1>n3: maxNumber=n1 else: maxNumber=n3 elif n2>n3: maxNumber=n2 else: maxNumber=n3 return maxNumber def main(): n1 = int(input("Enter first number: ")) n2 = int(input("Enter Second numer: ")) n3 = int(input("Enter Third number: ")) maximum = FindMaximum(n1,n2,n3) print("Maximum number is", maximum) main()
The output of the program is:
>>> Find the maximum Number ================== Enter first number: 65 Enter Second numer: 5646 Enter Third number: 6166 Maximum number is 6166 >>> Find the maximum Number ================== Enter first number: 45 Enter Second numer: 56 Enter Third number: 65 Maximum number is 65
We were able to get the maximum amount of people. Where we received three integers from the user in the main function and redirected the numbers to the FindMaximum() method. We use layered conditional statements in the FindMaximum() method.
We enter the nested conditional expression if the condition (n1>n2) is true. Our software will look for further conditional statements if the condition (n1>n2) is false. Inside the nested control structure, the first number is the maximum if it is more than the third number; otherwise, the third number is the maximum. If you make up some random numbers and try to fit them into the control structure, you’ll get a clearer perspective.