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:
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.
## Hello
If we change the value of x
so that the expression x == 2
returns False
:
Inside the block, several instructions can be written that will be evaluated if the expression is True
:
## 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:
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:
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
if-elif' conditional instructions over writing several
if’ 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 named
country. Another variable, named
continent` will take values depending on the content of the former one, such that:
- if the country value is present in the
europe
list, the variablecontinent
should be set toEurope
- if the country value is present in the
asia
list, the variablecontinent
should be set toAsia
- if the country value is not present in
europe
orasia
, the variablecontinent
will be set toOther
.
To do this:
- Create the two lists
europe
andasia
as well as the variablecountry
(setting the value toSpain
) and the variablecontinent
(initiated with an empty character string). - Write the code to achieve the explained objective, and display the content of the
continent
variable at the end of the execution. - Change the initial value of
country
toChina
thenBrazil
and in each case, execute the code written in the previous question.