Variables, lists, dictionaries, and f-strings, each attached to a physical quantity with a unit.
| File | Source | Coverage | Used for |
|---|---|---|---|
| iowa_city_weather_daily.csv | Iowa Environmental Mesonet, station IOW | Daily, 2019–2025, 2,549 rows | Temperatures, wind, the station record |
| Station metadata, from the same file | Station IOW: 41.64 N, 91.54 W, 204 m | One row per day, constant columns | The dictionary example |
A name that carries its unit is the cheapest error check you have.
Python
temp_c = -2.1 # daily mean, deg C temp_f = temp_c * 9 / 5 + 32 # unit conversion station_id = "IOW" # text, not a number print(temp_c, temp_f, station_id)
Python
>>> 3 + 4 7 >>> "3" + "4" '34' >>> "elevation: " + 204.0 TypeError: can only concatenate str (not "float") to str
f"Daily mean: {temp_c:.1f} deg C"
f"..." = formatted string literal
{temp_c} = insert the variable's value
:.1f = one decimal place, fixed point
deg C = the unit, written by you
?
Without the :.1f, Python prints -2.0991512345679015. Why is printing twelve decimals of a daily mean temperature scientifically misleading?
Seven days of temperature, indexed from zero.
Python
week_temps = [-2.1, -4.9, -1.3, 0.8, 2.4, 1.1, -0.6] week_temps[0] # -2.1 first item week_temps[-1] # -0.6 last item week_temps[1:4] # [-4.9, -1.3, 0.8] three items, not four
Seven daily mean temperatures from the first week of the record.
Route 1: sum(temps) / len(temps). Route 2: pandas .mean() on the same column.
Both give the same value. Two independent routes agreeing is your first correctness test.
One station, asked by name rather than by position.
Python
station = {
"id": "IOW",
"latitude": 41.64,
"longitude": -91.54,
"elevation_m": 204.0,
}
station["elevation_ft"] = station["elevation_m"] * 3.28084Python
for key, value in station.items():
print(key, "=", value)
# id = IOW
# latitude = 41.64
# longitude = -91.54
# elevation_m = 204.0
# elevation_ft = 669.29Ordered sequence. Ask by position: temps[0]. Seven days of temperature.
Labeled storage. Ask by name: station["id"]. One station's metadata.
Which one? Ask how you would request an item: by position, list; by name, dict.
?
You need latitude, longitude, and elevation for 17 stream stations. What structure fits: one list, one dictionary, or a dictionary per station inside a list?
Where pandas stops, plain Python starts, and how conversions go wrong.
First row of the weather table: station id, coordinates, elevation in meters.
Build a dictionary, add elevation_ft as a derived entry, loop and print each pair.
One f-string sentence: station, elevation in both units, formatted to sensible precision.
Lab 2 continues as Part B in Week 3: one notebook, two graded checkpoints.
| This week | Next week | Bridge idea |
|---|---|---|
| Store and label single values | Conditionals and loops | From holding data to making decisions over it |
?
Name one quantity from your own field and the variable name you would give it, unit included.