Conditionals
Conditional Statements¶
A conditional statement is a programming concept that describes whether a region of code runs based on if a condition is true or false. The keywords involved in conditional statements are if
, and optionally elif
and else
.
The if
Statement¶
A conditional statement starts with the if
keyword, followed by the condition to check, and ends with a :
. After the :
is an indented block of code that will only run if the condition evaluates to a value of True
. For example,
temperature = 35
if temperature > 32:
print('Temperature is above freezing.')
The code above is checking whether the value of the variable temperature
is greater than 32, the freezing point of water in degrees Fahrenheit at mean sea level pressure. If the condition is true, the program prints
Temperature is above freezing.
Note the >
is the greater-than "operator" in the usual arithmetic sense. If the condition evaluates to False
, then the code in the indented block is not executed. We can check the results by looking at the value of the condition, as we do here:
temperature > 32
We see that the value of the condition is True
, which explains why the print function was called. We can change the value of temperature
to see what happens if the condition is False
:
temperature = 30
if temperature > 32:
print('Temperature is above freezing.')
In this example, nothing is printed since the value of temperature
is not greater than 32. What do we do if we want to run one part of the code if a condition is true, and another if the condition is false? We can make use of the else
keyword.
The Optional else
Block¶
To run code if a condition is false, we employ the else
keyword. The else
part of the code is created by following the indented code of an if
statement by an unindented else:
, followed by an indented block of code. This else
block of code will execute if the condition in the if
statement evaluates to False
. For example, going back to the code we examined earlier, we can augement it to print the message
Temperature is at or below freezing.
in the else
block for the case where the condition is False
(i.e., temperature
is not greater than 32):
temperature = 30
if temperature > 32:
print('Temperature is above freezing.')
else:
print('Temperature is at or below freezing.')
The Optional elif
Block(s)¶
The if
and else
keywords are adequate when you have a binary choice such as examining a temperature value that is above or below freezing, but consider a scenario where you want more options. In this case, you will employ the elif
keyword that will follow the same :
, indented code block syntax. There can be zero or more elif
keywords in an if
statement. Let's write code that will categorize maximum sustained winds in units of knots according to the Saffir–Simpson hurricane intensity scale.
wind_speed = 65
if wind_speed >= 64 and wind_speed <= 82:
print('Category one hurricane')
elif wind_speed >= 83 and wind_speed <= 95:
print('Category two hurricane')
elif wind_speed >= 96 and wind_speed <= 112:
print('Category three hurricane')
elif wind_speed >= 113 and wind_speed <= 136:
print('Category four hurricane')
elif wind_speed > 136:
print('Category five hurricane')
else:
print('Below hurricane level wind speed')
Note, the >=
arithmetic "operator" signifies greater than or equal, and the <=
means less than or equal. We are also using the and
keyword to describe a range of values. E.g., wind_speed >= 64 and wind_speed <= 82
describes the range of values between 64
and 82
knots, end points inclusive. Also, observe that if the wind_speed
value does not meet any of the if
or elif
choices, the code ends up executing the else
block. For example, a wind_speed
of 15
will print:
Below hurricane level wind speed.