Back to Course

Introduction to Python

0% Complete
0/0 Steps
  1. Python: Basics
    14Topics
    |
    15 Quizzes
  2. Python: Control Flow
    12Topics
    |
    12 Quizzes
  3. Python: Errors
    5Topics
    |
    5 Quizzes
Lesson 2, Topic 10
In Progress

Python: Else If Statement

Yasin Cakal 20 Oct 2021
Lesson Progress
0% Complete

There are if statements, else statements, and elif statements. You may be wondering what an elif statement is. “Else if” is exactly what it sounds like. After the previous if statement’s conditions are not met, an elif statement checks another condition.

elif statements allow us to specify the order in which our program should check each of our conditional statements. The if statement is checked first, followed by each elif statement in order, and finally the else code is executed if none of the previous conditions are met.

Let’s take a look at an example of how this could be used to determine a student’s grade.

grade = 88

if grade >= 90:
    print("You got an A!")
elif grade >= 80:
    print("You got a B!")
elif grade >= 70:
    print("You got a C!")
elif grade >= 60:
    print("You got a D!")
elif grade <60:
    print("You got an F!")
else:
    print("Your grade is Incomplete!")

Output:

You got a B!

Consider this program for a moment. What if all of the elif statements were replaced with simple if statements? The first four messages would all print if their respective if conditions were satisfied. However, because we used elif statements, the condition is checked sequentially and only one message is printed. Then the rest of the elif and else statements are ignored.

%d bloggers like this: