6 Conditions

Often, depending on the evaluation of an expression, one wants to perform one operation rather than another. For example, when a new variable is created in a statistical analysis, and this variable takes its values according to another, it may be necessary to use conditional instructions : “if the value is less than \(x\), then… otherwise, …”.

In this short chapter, we look at how to write conditional instructions.

6.1 Conditional if Instructions

The simplest conditional instruction that can be found is if. If and only if an expression is evaluated at True, then an instruction will be evaluated.

The syntax is as follows:

if expression:
  instruction

The lines after the colon (:) must be placed in a block, using a tab stop.

A code block is a grouping of statements. Nested codes indented at the same position are part of the same block:

block 1 line
block 1 line
  block2 line
  block2 line
block line1

In the code below, we define a variable, x, that contains the integer \(2\). The following instruction evaluates the expression x == 2 (see Section @ref(#operateurs-comparaison) for reminders on comparison operators). If the result of this expression is True, then the content of the block is evaluated.

x = 2
if x == 2:
  print("Hello")
## Hello

If we change the value of x so that the expression x == 2 returns False:

x = 3
if x == 2:
  print("Hello")

Inside the block, several instructions can be written that will be evaluated if the expression is True:

x = 2
if x == 2:
  y = "Hello"
  print(y + ", x vaut : " + str(x))
## Hello, x vaut : 2

When writing a code, it may be practical to use `if’ conditional instructions to evaluate or not certain parts of the code. For example, when we write a script, there are times when we have to re-evaluate the beginning, but some parts don’t need to be re-evaluated every time, like graphical outputs (which takes time). Of course, it is possible to comment on these parts of codes that do not require a new evaluation. But we can also put the instructions in a conditional block:

  • at the beginning of the script, we create a variable graph = False;
  • before creating a graph, it is placed in a block if graph:
When executing the script, it is then possible to choose to create and export the graphs of the if graph: blocks by modifying the graph variable as desired.

6.2 if-else Conditional Instructions

If the condition is not verified, other instructions can be evaluated using the `if-else’ instructions.

The syntax is as follows:

if expression:
  instructions
else:
  other_instructions

For example, suppose we want to create a variable related to temperature, taking the value warm if the value of the variable temperature exceeds 28 degrees C, otherwise cold. Let’s say the temperature is 26 degrees C:

temperature = 26
heat = ""

if temperature > 28:
  heat = "hot"
else:
  heat = "cold"

print("It is " + heat + " out there")
## It is cold out there

If the temperature is now 32 degrees C:

temperature = 32
heat = ""

if temperature > 28:
  heat = "hot"
else:
  heat = "cold"

print("It is " + heat + " out there")
## It is hot out there

6.3 if-elif Conditional Instructions

If the condition is not verified, another one can be tested and then other instructions evaluated if the second one is verified. Otherwise, another one can be tested, and so on. Instructions may also be evaluated if none of the conditions have been assessed at True. To do this, conditional `if-elif’ instructions can be used.

The syntax is as follows:

if expression:
  instructions
elif expression_2:
  instructions_2
elif expression_3:
  instructions_3
else:
  other_instruction

The previous example lacks some common sense. Can we say that the fact that it is 28 degrees C or less it is cold? Let’s add a few nuances:

temperature = -4
heat = ""

if temperature > 28:
  heat = "hot"
elif temperature <= 28 and temperature > 15:
  heat = "not too hot"
elif temperature <= 15 and temperature > 0:
  heat = "cold"
else:
  heat = "very cold"

print("It is " + heat + " out there")
## It is very cold out there
The advantage of using if-elif' conditional instructions over writing severalif’ conditional instructions in succession is that with the first way of doing things, comparisons stop as soon as one is completed, which is more efficient.

6.4 Exercise

Let us consider a list named europe containing the following values, as strings: “Germany”, “France” and “Spain”.

Let us consider a second list, named asia, containing in the form of strings: “Vietnam”, “China” and “India”.

The objective will be to create a continent variable that will indicate either Europe, Asia or other at the end of the code execution.

Using conditional instructions of the if-elif' type, write a code that checks the value of a variable namedcountry. Another variable, namedcontinent` will take values depending on the content of the former one, such that:

  • if the country value is present in the europe list, the variable continent should be set to Europe
  • if the country value is present in the asia list, the variable continent should be set to Asia
  • if the country value is not present in europe or asia, the variable continent will be set to Other.

To do this:

  1. Create the two lists europe and asia as well as the variable country (setting the value to Spain) and the variable continent (initiated with an empty character string).
  2. Write the code to achieve the explained objective, and display the content of the continent variable at the end of the execution.
  3. Change the initial value of country to China then Brazil and in each case, execute the code written in the previous question.