2.5. if Statement

When you understand conditional tests, you can start using if statement. We will see several kinds of if statements.

Simple if statements

We already saw some simple if statements in the previous sections. Just like for, indentation and : is important when using if statements. Here's an if statements more than one line:

> Since the conditional test passes, and both print() are indented, both lines are printed.

if-else statements

Sometimes we want to declare an action when the condition holds, and something else when it does not hold. You can use if-else syntax in this case. Here's an example of if-else statement. Note the indentation for else.

The if-else structure works in a situation when you want one of two possible actions to be executed.

> In this case since the conditional test results in False (because drink is coffee), the else block will be executed instead. The last line is not indented so it will always be executed.

if-elif-else chain

Many real-world situations have more than two possible conditions. In this case, this if-elif-else chain can be used. For example, our restaurant serve three kind of menu:

  • Special kids menu for anyone under age 13

  • Special adult menu for anyone between the age 13-65

  • Special senior menu for age 65 or older

The following code tests the age group of a person and offer accordingly.

> Here's what happened:
  • The if at ❶ tests whether someone is under 13 years old. If this is True, Python will print the following block and skips the rest of the test.
  • The elif line at âť· is just another if test, which runs only if the previous test failed.
  • If both if and elif failed, Python will run the else block at ❸.
In that example, since age is 15, the test at ❶ failed, so the code block is skipped. However, the second test of age < 65 is True (since 15 < 65) so the code is executed.

In our example, rather than printing the message within the if-elif-else, it would be simpler to just set the menu name inside that chain, then have one print() at the end like this.

This code gives the same output as the previous example. Instead of displaying a message, the if-elif-else chain simply determine the menu type.

Multiple elif blocks

You can use more than one elif in your code. In our example, let's add a toddler menu for age less than 5 and a teenager menu for age 13-18.

Most of this code is the same as the previous example, we just add two extra elif block for toddler and teenager.

Not using else block

The else block at the end of if-elif chain is not a must. Sometimes it is is clearer to use an extra elif instead of else.

If you have a specific final condition that you want to test, consider using a final elif block and omit the else block. Using else can catch some invalid or bad data.

Testing Multiple Conditions

The if-elif chain is powerful when you just need one test to pass. Once it finds one that passes, it skips the rests. However, sometimes you have multiple conditions you want to check.

Let's say we want to print our order using multiple if.

In this way, because every condition is evaluated, both tea and coffee is added to the list. If we use elif, this is what will happen:

The order for tea is executed since the first if is satisfied. However, the next statement for coffee and beer were not checked.

In conclusion, if you want only one block to run, use an if-elif chain. If you want more than one block to run, use a series of independent if statements.

Exercise 2.5

  1. Which car to buy
    Using the provided variables, write an if statement to decide which car that can be bought.
    Ex: if saving is $29,000, you can buy all cars in range_22500 and range_27500. The output should be
    Client 1
    Here are your car options
    - Impreza
    - Legacy
    - Crosstrek
    - Forester
    - Outback
    - WRX

    Hint: Use car_options += list_name

  2. Price range for a car?
    Using the provided variables, write an if statement to decide what is the price range of the car choice.
    Ex: if the choice is 'Forester', the output should be
    Subaru Forester is in the $27500 range.

  3. Can you buy the car?
    Write an if statement to decide if client_1 can buy the car or not. He can buy the car if the choice is in his car_options.
    If he can, print
    That car is in your price range.
    if not, print
    That car is not in your price range.

  4. Next client
    Copy the necessary code from no. 1-3 and make sure it works using client_2_saving and client_2_choice.