Conditionals
Overview
Teaching: 10 min
Exercises: 15 minQuestions
How can programs do different things for different data?
Objectives
Correctly write programs that use if and else statements and simple Boolean expressions (without logical operators).
Use if statements to control whether or not a block of code is executed.
- An
ifstatement (more properly called a conditional statement) controls whether some block of code is executed or not. - Structure is similar to a
forstatement:- First line opens with
ifand ends with a colon - Body containing one or more statements is indented (usually by 4 spaces)
- First line opens with
mass = 3.54
if mass > 3.0:
print(mass, 'is large')
mass = 2.07
if mass > 3.0:
print (mass, 'is large')
3.54 is large
Conditionals are often used inside loops.
- Not much point using a conditional when we know the value (as above).
- But useful when we have a collection to process.
masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for m in masses:
if m > 3.0:
print(m, 'is large')
3.54 is large
9.22 is large
Use else to execute a block of code when an if condition is not true.
elsecan be used following anif.- Allows us to specify an alternative to execute when the
ifbranch isn’t taken.
masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for m in masses:
if m > 3.0:
print(m, 'is large')
else:
print(m, 'is small')
3.54 is large
2.07 is small
9.22 is large
1.86 is small
1.71 is small
Use elif to specify additional tests.
- May want to provide several alternative choices, each with its own test.
- Use
elif(short for “else if”) and a condition to specify these. - Always associated with an
if. - Must come before the
else(which is the “catch all”).
masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for m in masses:
if m > 9.0:
print(m, 'is HUGE')
elif m > 3.0:
print(m, 'is large')
else:
print(m, 'is small')
3.54 is large
2.07 is small
9.22 is HUGE
1.86 is small
1.71 is small
Compound Relations Using
andororOften, you want some combination of things to be true. You can combine relations within a conditional using
andandor. Let’s look at an example with our gapminder data in mind to calculate what quartile a given life expectancy value will fall into.expectancies = [62.5, 57.9, 81.0, -1] for exp in expectancies: if exp > 0 and exp < 58.41: # This observation is in the first quartile quartile = 1 elif exp >= 58.41 and exp < 67.05: # This observation is in the second quartile quartile = 2 elif exp >= 67.05 and exp < 71.70: # This observation is in the third quartile quartile = 3 elif exp >= 71.70: # This observation is in the fourth quartile quartile = 4 else: # This observation has bad data quartile = None print('life expectancy', exp, 'is in quartile', quartile)life expectancy 62.5 is in quartile 2 life expectancy 57.9 is in quartile 1 liife expectancy 81.0 is in quartile 4 life expectancy -1 is in quartile None
Processing Small Files
Modify this program so that it only processes files with fewer than 50 records.
import glob import pandas as pd for filename in glob.glob('data/*.csv'): contents = pd.read_csv(filename) ____: print(filename, len(contents))Solution
import glob import pandas as pd for filename in glob.glob('data/*.csv'): contents = pd.read_csv(filename) if len(contents) < 50: print(filename, len(contents))
Key Points
Use
ifstatements to control whether or not a block of code is executed.Conditionals are often used inside loops.
Use
elseto execute a block of code when anifcondition is not true.Use
elifto specify additional tests.