# Initialize Otter
import otter
grader = otter.Notebook("lab02.ipynb")[Fundamentals FA25] Lab 2 – Variables¶
Data 6, Fall 2025¶
Welcome to the second lab section of Data 6!
In today’s lab, you’ll learn how to:
Write more complex expresions involving Python names
Compute and interpret rates, like incidence
Run the autograder’s built in tests and submit files
Question 1: Checking your code¶
In this lab, you’ll start using the built-in tests to check whether your work is correct. Sometimes, there are multiple tests for a single question, and passing all of them is required to receive credit for the question. Please don’t change the contents of the test cells.
Go ahead and attempt the question below. Running the cell directly after it will test whether you have assigned seconds_in_a_decade correctly. If you haven’t, this test will tell you the correct answer. Resist the urge to just copy it, and instead try to adjust your expression. (Sometimes the tests will give hints about what went wrong...)
Question. Assign the name seconds_in_a_decade to the number of seconds between midnight January 1, 2010 and midnight January 1, 2020. Note that there are two leap years in this span of a decade. A non-leap year has 365 days and a leap year has 366 days.
Hint: If you’re stuck, the next section shows you how to get hints.
# Change the next line
# so that it computes the number of seconds in a decade
# and assigns that number the name, seconds_in_a_decade.
seconds_in_a_decade = ...
# We've put this line in this cell
# so that it will print the value you've given to seconds_in_a_decade when you run it.
# You don't need to change this.
seconds_in_a_decadegrader.check("q_1")Part 2: Integer division¶
Su Min, a candy lover, recently returned from a trip to Tokyo, Japan, where she bought a lot of limited edition KitKat flavors. Now, she has 160 KitKats in total! She stores her KitKats in a set of drawers, where each drawer can hold exactly 12 KitKats. Whenever a drawer is full with 12 KitKats, the next KitKat goes into the next drawer. Su Min wants to know how many drawers will be completely filled with 12 KitKats and how many KitKats will be left in the last partially filled drawer.
Question 2a¶
How many full drawers are there? Write an expression that computes the number of full drawers. Your expression shouldn’t have any numbers in it; only arithmetic operators and the names total_kitkats and drawer_capacity. Give the value of your expression the name full_drawers.
Hint: Should full_drawers be an int or a float? Why?
total_kitkats = 160
drawer_capacity = 12
full_drawers = ...
full_drawersgrader.check("q_2_a")Question 2b¶
How many leftover KitKats will be in the last partially full drawer? Write an expression that computes this value and assign that value to the name leftover_kitkats. Your expression shouldn’t have any numbers in it; only arithmetic operators and the names total_kitkats and drawer_capacity.
leftover_kitkats = ...
leftover_kitkatsgrader.check("q_2_b")Question 2c¶
How many Kitkats are in full drawers? Write an expression that computes this value and assign that value to kitkats_in_full_drawers. Your expression shouldn’t have any numbers in it; only arithmetic operators and some subset of the names total_kitkats, drawer_capacity, full_drawers, and leftover_kitkats.
kitkats_in_full_drawers = ...
kitkats_in_full_drawersgrader.check("q_2_c")In this part, we will explore the Tuberculosis dataset we previewed in lecture. Here’s that data again:
Tuberculosis data. CDC MMWR source
| U.S. jurisdiction | 2019 # cases | 2020 # cases | 2021 # cases | 2019 incidence | 2020 incidence | 2021 incidence |
|---|---|---|---|---|---|---|
| Total | 8,900 | 7,173 | 7,860 | 2.71 | 2.16 | 2.37 |
| Alabama | 87 | 72 | 92 | 1.77 | 1.43 | 1.83 |
| ... | ... | ... | ... | ... | ... | ... |
| California | 2,111 | 1,706 | 1,750 | 5.35 | 4.32 | 4.46 |
| ... | ... | ... | ... | ... | ... | ... |
U.S. Census population estimates (2019 source, 2021-2021 source)
| 2019 population | 2020 population | 2021 population | |
|---|---|---|---|
| Total | 328,239,523 | 331,501,080 | 331,893,745 |
| Alabama | 4,903,185 | 5,024,803 | 5,039,877 |
| California | 39,512,223 | 39,499,738 | 39,237,836 |
# Demo: just run this cell
pop_2020 = 331501080
tb_2020 = 7173
incidence_2020 = tb_2020/(pop_2020/100000)
incidence_2020Question 3: Commas¶
In the United States, a comma is used as a thousands separator in large numbers, such as 1,000,000 for one million. Comma separators improve human readability of numbers.
You may have noticed that in Python, values of numeric data types (int, float) do not contain comma separators. Nevertheless, we may need to translate numeric values into human-readable string values.
In the cell below, write an expression that uses tb_2020 to produce the string “7,173”. Constraints/hints:
Your expression must use the name
tb_2020.Your expression can only use one numeric constant,
1000; it cannot use the values 7, 173, etc. Your expression may involve arithmetic operators (+,/,//,%, etc.).Your expression can only use one string literal, “,”. Your expression may also involve string concatenation (
+).Your expression may involve type-casting functions (
str,int, etc.)
Give the value of your expression the name tb_2020_str.
tb_2020 = 7173
tb_2020_str = ...
tb_2020_strgrader.check("q_3")Question 4: Changes in Percentage¶
Consider this line in the abstract of the CDC MMWR report:
Reported TB incidence (cases per 100,000 person) increased 9.4%, from 2.2 during 2020 to 2.4 during 2021...
Let’s verify these numbers.
The Tuberculosis (TB) incidence is defined as the number of TB cases per 100,000 persons in a particular population:
Demo: Named arguments¶
First, notice that the 2020 TB incidence rate provided in the Data 6 lecture notes is a float data type, with much more precision than the rounded numbers provided with the quote.
We can use the built-in round function in Python to construct this number:
# just run this cell
incidence_2020 = 2.1637938555132306 # from our notes document
round(incidence_2020)The default behavior of round is to round to the nearest integer. We can supply a second argument to specify the number of digits of precision after the decimal point. For example, to round to the nearest thousandths place:
# just run this cell
round(incidence_2020, 3)In programming, we generally try to avoid “magic numbers” like the 3 above. If someone else read your code or were unfamiliar with Python, they might not immediately grasp that round(..., 3)'s second argument is for rounding precision.
For certain functions, Python allows you to name arguments. In this case, the second argument of round is named ndigits. You can verify this in the Python documentation, which we’ve quoted in part here:
round(number, ndigits=...)Return number rounded to ndigits precision after the decimal point. If
ndigitsis omitted ..., it returns the nearest integer to its input.
Using this convention, we can adjust our earlier expression and assign our rounded 2020 TB incidence (rounded to tenths) to the name incidence_2020:
# just run this cell
incidence_2020 = round(incidence_2020, ndigits=1)Question 4a¶
Write an expression that computes the 2021 TB incidence. Your expression should involve the names tb_2021 and pop_2021, which have been assigned for you; it should also round the result to the nearest tenths place. Give the value of your expression the name incidence_2021.
Hint: You may need to use one numeric literal, 100000. See Equation 3 from the Data 6 notes (also copied above).
tb_2021 = ...
pop_2021 = ...
incidence_2021 = ...
incidence_2021grader.check("q_4_a")Question 4b¶
The “percent change from measured value to measured value ” is computed as follows:
A useful framework for understanding the values in this formula (particularly the denominator) are that is the “starting point” of the measurement, and all changes are with respect to this starting point.
Assign the name percent_change to an expression that evaluates to the percent change of TB incidences from 2020 to 2021. You do not need to round this number.
percent_change = ...
percent_changegrader.check("q_4_b")Validate: How close is your result to the 9.4% percentage increase as reported in the CDC report? Why is it so “off”?
Our explanation and warning
This exercise is a lesson to avoid rounding until you have finished all computations. If you round in “intermediate steps,” then rates computed on these numbers might have significant roundoff error!
Further consider: What would a negative percent change imply?
Done!¶
To double-check your work, the cell below will rerun all of the autograder tests.
grader.check_all()Submission¶
Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. Please save before exporting!
# Save your notebook first, then run this cell to export your submission.
grader.export(pdf=False)