What Does "elif" Mean in Python?

iD Tech in action

In Python, elif is short for "else if" and is used when the first if statement isn't true, but you want to check for another condition. Meaning, if statements pair up with elif and else statements to perform a series of checks.

A full if/elif/else block is in the code example below.

player_age = 12

if player_age >= 18:
    print("You could be in college.")
elif player_age >= 13:
    print("You can also attend iD Academies!")
elif player_age >= 7:
    print("You can attend iD Tech Camps!")
else:
    print("You're young.")

When you run the code and input multiple ages, you can see which part of the if/elif/else block prints out.

Using the example above, if and elif statements always look for a specific condition:

  •  if player_age > 18 : #Happens if the age is greater than 18.
  •  elif player_age > 13 : #Only happens if the age is not greater than 18 and greater than 13.

An else statement doesn't look for a specific condition. Else statements occur after an if or elif statement in your code.

  • if player_age > 18 : #Happens if the age is greater than 18.
  • else : #Would happen for any other age not specified above.

Every if/elif/else block must begin with one regular if statement and end with a single else statement, but you can have as many elif statements in the middle as you want!

A photo of Ryan

Ryan manages blog content at iD Tech, starting with the company in 2008. He earned his MBA from Santa Clara University after obtaining his Bachelor’s degree from Arizona State. Connect on LinkedIn!

Featured Posts

Categories

Authors

About iD Tech

iD Tech is the #1 tech camp on the planet, and world leader in youth STEM education, with programs held online and at 150+ global locations offering 50+ innovative tech courses: 

Coding camps
Video game camps
Robotics classes & camps
Creative arts classes & camps
All STEM camps

We've bet our reputation on recruiting the top instructors in the country. Our small classes ensure customized learning, leading to "a-ha moments" and awesome outcomes. Programs include:

On-Campus Programs

Online Tutoring

All Coding Courses