# Conditional statements¶

## Sources¶

This lesson is based on the Software Carpentry group’s lessons on Programming with Python.

## Basics of conditional statements¶

Conditional statements can change the code behaviour based on meeting certain conditions.

### A simple conditional statement¶

Let’s take a simple example.

:

temperature = 17

if temperature > 25:
print('it is hot')
else:
print('it is not hot')

it is not hot


What did we do here? First, we used the if and else statements to determine what parts of the code to execute. Note that both lines containing if or else end with a : and the text beneath is indented. What do these tests do? The if test checks to see whether the variable value for temperature is greater than 25. If so, 'it is hot' would be written to the screen. Since 17 is smaller than 25, the code beneath the else is executed. The else statement code will run whenever the if test is false.

#### A familiar conditional scenario¶

As it turns out, we all use logic similar to if and else conditional statements daily. Imagine you’re getting ready to leave your home for the day and want to decide what to wear. You might look outside to check the weather conditions. If it is raining, you will wear a rain jacket. Otherwise, you will not. In Python we could say:

weather = 'Rain'

if weather == 'Rain':
print('Wear a raincoat')
else:
print('No raincoat needed')

Wear a raincoat


Note here that we use the == to test if a value is exactly equal to another.

### else is not required¶

The combination of if and else is very common, but both are not strictly required.

:

temperature = 13

:

if temperature > 25:
print('13 is greater than 25')


Note that here we use only the if statement, and because 13 is not greater than 25, nothing is printed to the screen.

### Introducing second test¶

We can also have a second test for an if statment by using the elif (else-if) statement.

:

temperature = -3

:

if temperature > 0:
print(temperature, 'is above freezing')
elif temperature == 0:
print(temperature, 'is freezing')
else:
print(temperature, 'is below freezing')

-3 is below freezing


Makes sense, right? Note here that we again use the == to test if a value is exactly equal to another. The complete list of these comparison operators is given in the table below.

Operator Meaning
< Less than
<= Less than or equal to
== Equal to
>= Greater than or equal to
> Greater than
!= Not equal to

#### Poll question pause¶

Time to check your understanding. Let’s assume that yesterday it was 14°C, it is 10°C outside today, and tomorrow it will be 13°C. The following code compares these temperatures and prints something to the screen based on the comparison.

yesterday = 14
today = 10
tomorrow = 13

if yesterday <= today:
print('A')
elif today != tomorrow:
print('B')
elif yesterday > tomorrow:
print('C')
elif today == today:
print('D')


Which of the letters A, B, C, and D would be printed to the screen? Select your answer from the poll options at https://geo-python.github.io/poll/.

### Combining conditions¶

We can also use and and or to have multiple conditions.

:

if (1 > 0) and (-1 > 0):
print('Both parts are true')
else:
print('One part is not true')

One part is not true

:

if (1 < 0) or (-1 < 0):
print('At least one test is true')

At least one test is true


These are just simple examples, but concepts that can be quite handy.

#### Another familiar conditional scenario¶

Again, making decisions based on multiple conditions is something we regularly do. Imagine that we consider not only the rain, but also whether or not it is windy. If it is windy and raining, we’ll just stay home. Otherwise, we need appropriate clothing to go out. We can again handle this kind of decision with Python.

weather = 'Rain'
wind = 'Windy'

if (weather == 'Rain') and (wind == 'Windy'):
print('Just stay home')
elif weather == 'Rain':
print('Wear a raincoat')
else:
print('No raincoat needed')

Just stay home


As you can see, we better just stay home if it is windy and raining.