Please enable JavaScript to use CodeHS

Python Conditionals: If, Else, Elif

Use conditionals to teach your program to make decisions based on the information it receives.

By Ryan Molyneaux

Python Conditional Statements

Conditional statements are computations that are performed based on the boolean values or bool that is created within the program. They help us to navigate trickier problems that may require the program to choose between multiple statements or skip over specific statements. These conditional statements are considered to be control structures, giving us flow control as they analyze and navigate the appropriate action to take.

A real-world example of conditional statements can be viewed through the eyes of a student:

“If I put in at least 2 hours of studying per weekday, I will increase my GPA.”


If Statements

Based on the scenario above, our program’s result depends on whether the student’s studying condition was met. Because we are guiding the flow of this scenario, implementing a control structure such an if statement, would give us the type of response we’re looking for.

Very much in the likeness of a hypothesis in the scientific method, the structure of an if statement provides that:

If { a specific expression occurs }, then { an anticipated result will follow }.

In other words…

If { bool }, then { statement }.

The syntax for this in Python would resemble the following format.


if <bool>:
       <statement>
Plain text


Note: The code within each statement block should be indented one level until the end of that code. Let’s take a closer in the code below!

If/Else Statements

Now that we know what control structures are, let’s consider the possibility of multiple statements. In the code above, the print statement will only run if <study_hours > is equal to 2. Meaning if the student studies less than or more than 2 hours per weekday, nothing will print onto the screen.

But what if we want the program to let us know whether the GPA will either increase or decrease? How would this look? We could write code that will perform like the pseudocode below:


If student enters more than 2 hours:
       Print (“GPA is increased”)

Else:
       Print (“GPA is not increased”)

The “else” prevents redundancy and implies that if the number entered is less than 2, the GPA will not be increased.

In order to code a program that will evaluate a condition and either choose one path if true or choose another path if false, we will need to create an if/else statement. To mentally map out your program, take a look at the flow chart below.


Now, let's view this in code! 


Elif Statements

There’s another way for the program to choose between several paths. To navigate 3 or more statements, we can use an an else if statement, otherwise known as elif. The program below shows that if the student studies more than 2 hours per day, their GPA increases. If they study exactly 2 hours per day, their GPA remains the same. If they study less than 2 hours per day, their GPA decreases.


If, Elif, Else Recap

Check Your Understanding

To recap, conditionals are control structures that allow us to effectively navigate multiple outcomes. We have discussed the meaning of and how to use if, if/else, and elif statements. To check your understanding, take a look at the vocabulary list below and try to recall how each term is either used or its definition.

Vocabulary Terms

  • If Statement
  • If/Else Statement
  • Elif Statement
  • Boolean
  • Bool Expression
  • Control Structures


Try It Out!
Practice is never a bad thing! Feel free to manipulate the code editor below to get a better grasp of syntax and using conditionals in general. Are there any other real-world situations that navigate the flow of If, If/Else, and Elif statements? For an extra challenge, create code for the following scenarios!

Challenge Scenarios

  1. Wellness App: Write a program that will tell the user to make tea if they have a sore throat or grab tissues for a runny nose.
  2. Fashion Guide: Ask the user to choose a Season. Write a program that will tell the user which clothing item to order depending on their input: Boots for Winter, Raincoat for Spring, Swimsuit for Summer, and Parka for Autumn.
  3. Meal Planner: Ask the user for their dietary preferences. Based on their response, print specific lunch options: Grilled Seitan for Vegan, Seared Salmon for Pescatarian, Caprese Salad for Vegetarian, and Club Sandwich for None.


Hint: Remember to store your boolean expressions into variables.