2.4. Conditional Test

In each if statements, for any programming language, there is an expression that can be processed as True or False. This is called a conditional test. This True or False value decides whether the if statement should be executed or not.

Here's a simple example of if statement:

We will see what happened in each part of that code in this section. Let's started with the kinds of conditional test:

Equality ( == )

The basic conditional test checks whether the value of a variable is equal to the value we want. Let's see the following example.

Here's what happened:

  • In line ❶, we put the value coffee for variable drink using =, as we did in previous sections.

  • At line âť·, we check whether the value is coffee using ==.

  • This returns True if the values on the left and right side are the same, and False if they are not the same.

This equality is also case sensitive. The value coffee and Coffee are not the same, see the following example.

This can be fixed using the lower() method, to convert the original drink variable into lower-case.

> Data and user input is full of inconsistencies in lower and uppercase. Make sure to put lower() for checking those kind of things.

Inequality ( != )

When we checked equality, we use ==. To check inequality, we simply change that into !=. Here's an example.

The first line we define the drink to be coffee. If the two values do not match, that is if drink is not soda, it returns True and executes the code after the if statement.

Most of the time, you will write test for equality but in some cases using inequality can be more efficient.

Other Inequalities

When you test numerical value, you can use other mathematical comparison such as <, <=, >, and >=.

Here we use it in a simple if statement:

Multiple Conditions

You can use the and operator to check whether two conditions holds. Operator or can also be used in a similar fashion. Let's look at an example:

In this example both age <= 21 and drink == 'beer' returns True value so age <= 21 and drink == 'beer' is also True. To improve readibility, you can also use parentheses like this:

(age <= 21) and (drink == 'beer')

Checking Item in a List

Checking whether a value appear or not in a list can be important. Let's see the following example:

Using in operator, we can check whether an item is in a list. Since beer is in the order list, it returns True.

We can also use not if we want to check if the item is not in a list.

Here, the statement 'beer' not in order will return False since beer is in that list.

Boolean Expressions

When we evaluated our conditions, the value True and False is called a boolean value. We can put the value True and False into a variable, just like number and string.

Boolean value provide an efficient way to track the state of a program that is important in your program.

Exercise 2.4

  1. Conditional Tests
    Using the provided variables, print the following:
    Checking car name...
    Audi : False
    Subaru : True
    Jaguar : False
    Peugeot : False
    Hint: Save the conditional test in a variable. Use \t after car name, not spaces .

  2. Checking Availability
    Using the provided variables, print the following:
    Checking availability...
    Subaru Impreza : False
    Subaru Outback : True
    Subaru Forester : False
    Subaru Legacy : True
    Subaru Ascent : False
    Subaru WRX : True

  3. Buy the car?
    The car in the stock cost around $25,000. Write an if statement that if saving is at least (>=) than $25,000 and the choice is in the car stocks, print:
    {client_name}, you can buy this car.