Conditionals

Overview

Teaching: 10 min
Exercises: 15 min
Questions
  • 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.

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.

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.

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.

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 and or or

Often, you want some combination of things to be true. You can combine relations within a conditional using and and or. 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 if statements to control whether or not a block of code is executed.

  • Conditionals are often used inside loops.

  • Use else to execute a block of code when an if condition is not true.

  • Use elif to specify additional tests.