The elif statements are used when we do not want nested if-else at all, mostly.

There are a lot of other scenarios in which we use elif.

Say, let’s take the situation of grading again. If a student gets marks above 90, he gets A grade, between 80 and 90 then B grade, between 70 and 80 then C grade and below 70 then D grade. So we can represent this situation as shown in the following piece of code. The variable names are self-explanatory.

if Student_marks>=90:
   Student_grade='A'
elif Student_marks>=80:
   Student_grade='B'
elif Student_marks>=70:
   Student_grade='C'
else:
   Student_grade='D'
print(Student_grade)

I suggest you to declare the variable Student_marks and initialize it with some value and check out the code to see how the control flow is working. According to the marks, the grade will be given by that statement which satisfies the appropriate conditions.

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019