Lecture 6 – Control

Data 6, Summer 2021

Motivating Example


The most basic if-statements only include an if.

You don't always need to use else.

The above is equivalent to this; I personally prefer the above.

def safe_divide(a, b):
    if b == 0:
        return 0
        return a / b

Quick Check 1

Be concise!


Let's build a grades calculator, according to this table:

Letter Range
A [90, 100]
B [80, 90)
C [70, 80)
D [60, 70)
F [0, 60)

Note, $[a, b)$ refers to the set of numbers that are greater than or equal to $a$, but less than $b$.

Here it's worth illustrating the difference between using elif and many ifs.

The fact that we're using return and elif changes the behavior of our code dramatically. Watch out for this!

Quick Check 2

It's worth mentioning that if we were returning instead of printing here, we could have used if both times instead of if and elif.

Truthy Values


You should ignore all of the following code, except for the cell containing the definition of the function state_color.

Now, don't ignore this cell!