If Statement, Else, and the ‘elif’ in Python

Sari Lakkis
5 min readMay 15, 2021

In this tutorial, we will learn about the if-statement and how to perform different scenarios in our code based on a certain condition.

We will learn these concepts while solving a small problem.

Our problem for this tutorial is about calculating the price of a flight ticket, where the flat price for example is 100$ for everyone except for the children under 12 it’s 50$ and for seniors above 70, it’s 80$.

In our application, We will ask the user to enter his age and then we output the ticket price.

Creating the Project “FlightTicketPrice”

Let’s create a new folder called “FlightTicketPrice” and open VS Code inside it using the cmd commands:

mkdir FlightTicketPrice
cd FlightTicketPrice
code .

Inside Vs Code create a new file called “main.py”.

Now, we can read the age from the user and store it in a variable called “age”, as we learned in part three of this series using the input() method.

Since we’ll mostly use age as an int so let’s cast the input method to int, thus age will have int data type.

age = int(input("Enter your age: "))

We want to calculate the price based on age value but we have more than one possibility or case, the way to consider them all is to use the “if-statement”.

If-Statement

As its name says, it allows us to test whether a condition is true or false.

For example if we want to test if the age of the passenger is less than 12 we say:

if age < 12:

We also end the condition with a colon “:” like we are saying then do the code that follows if the condition is true or skip it if it’s false.

Now to specify which lines of code we want to include in the “if-statement” so that they are executed when the condition is true, we need to add indentation.

Python uses indentation to determine which code belongs to the if statement. Indentation is the whitespace at the beginning of a line. For example, we want to create a variable price with value 50 when the condition (age < 12) is true so we write the following:

Indentation in if-statement

Notice how line number three in the figure above has an indentation.

If we want to add more statements to the “if-block”, we keep the same indentation but if we want to add a general statement that is outside we remove it and move back to the start of the line.

What happens if we don’t add indentation?

If we had only one statement under the if and we remove the indentation by mistake, we get an error:

IndentationError: expected an indented block

but if we have more than one statement then this might cause a runtime error or an unexpected result in our application which we call “bug”.

let’s test this by printing the result to the user, add this line of code to the if-block with the same indentation as the previous line:

print("The price of your ticket is: " + str(price))
if-block with two lines of code

So, we expect the value of the price to be printed only if the entered age is less than 12. And if we remove the indentation then the print statement will always execute since it’s now outside the if-block.

What about the other cases?

We still need to see when age is greater than 70, we may do that by using another if-statement like

if age > 70:
price = 80

But in this case we will always be testing even when the age is less than 12. Also imagine if we have more cases, we’ll be doing many unnecessary checks.

So, to tell the Python compiler to only check if age is greater than 70, we use “elif” which means else if the previous condition is false then check this condition and if the previous was true no need to do this check. Thus saving some computing power.

Append the following to our code:

elif age > 70:
price = 80
print("The price of your ticket is: "+ str(price))

And for all remaining cases where the age is greater than 12 and less than 70, we will use the “else” keyword which is self-explanatory. The “else” will catch all the remaining cases.

Let’s add the following lines:

else:
price = 100
print("The price of your ticket is: "+ str(price))

Improving the code (Refactoring)

We noticed that the print statement is repeated three times, we can enhance our code and add it just one time after the if-statement ends.

That way we used the if-statement to calculate price and after we did it we printed the results.

This is the new code:

age = int(input("Enter your age: "))
if age < 12:
price = 50
elif age > 70:
price = 80
else:
price = 100
print("The price of your ticket is: "+ str(price))

Run your code and test it several times with different values for the age to validate it.

Conclusion

In this tutorial, we explored the if-statement and how to perform different scenarios in our code according to a certain condition. We also saw how to use the “elif” and “else” keywords.

In the next tutorial, we’ll discuss loops, a very interesting subject, see you there.

--

--

Sari Lakkis

Professor | Consultant | Researcher. I give advice on strategy, design, and implementation of software that solves business problems to companies and startups.