Lecture 4: Conversions
Overview
The lecture teaches unit conversion in Python through explicit intermediate calculations: convert input strings to numbers, multiply when moving to smaller units, and divide when moving to larger ones. It then uses integer division (`//`) and modulo (`%`) to express seconds as years, days, hours, minutes, and seconds, before applying the same step-by-step reasoning to estimate Washington's tree population at roughly 2 billion.
Key takeaways
- Python's `input()` produces a string, so numeric responses must be converted with `int()` or another numeric type before arithmetic; otherwise, multiplying a digit string repeats its characters.
- For unit conversions, multiply when moving from larger units to smaller ones and divide when moving from smaller units to larger ones, keeping each factor explicit to make errors easier to find.
- Integer division (`//`) gives the count of complete units and modulo (`%`) gives the leftover amount, enabling seconds to be reported as whole years, days, hours, minutes, and seconds.
- A decimal result such as 1.58 years can be converted into more useful units by repeatedly separating whole units from remainders, as in the 50,000,000-second example.
- A Fermi estimate becomes manageable by stating assumptions and converting units: 50% forest cover, 640 acres per square mile, and about 100 trees per acre yield roughly 2 billion trees for Washington.
- Estimation problems should be judged by order of magnitude and transparent assumptions, not false precision; Randall Munroe's What If? uses a similar approach for questions such as Google's data capacity in punch cards.
Chapters
- Professor Rosen directs students to Project 3 through Canvas Module 2 or the Runestone Foundations of Python Programming textbook.
- The lecture uses the textbook's years-to-minutes exercise to introduce a reusable programming approach relevant to Lab 2.
- Python's `input()` returns a string even when the user enters digits, so the example converts `years` with `int(years)` before doing arithmetic.
- Multiplying the string `324` by an integer repeats its characters instead of calculating a numeric result.
- Entering the word `three` causes `int()` to fail, while surrounding whitespace around a numeric input is accepted.
- The conversion can be nested directly inside `input()`, but using a separate assignment can be more readable for beginners.
- The conversion chain multiplies years by 365 days per year, then by 24 hours per day, then by 60 minutes per hour.
- To reach seconds, multiply the computed minutes by another 60, keeping each conversion step visible rather than hiding all factors in one expression.
- After changing the calculation, update the printed label and output statement too, a common checklist item when debugging code.
- For a seconds-to-years program, Professor Rosen temporarily assigns a test value such as 5,000 seconds instead of repeatedly stopping for interactive input.
- Dividing seconds by 60 gives minutes, and dividing minutes by 60 gives hours; division reverses the multiplication used for smaller units.
- Testing with 600 seconds and 3,600 seconds checks whether the minute and hour conversions produce sensible results.
- The input prompt is added after the conversion logic, keeping input, calculation, and output as separate parts of the program.
- The reverse conversion continues from hours to days by dividing by 24, then from days to years by dividing by 365.
- Months are skipped because they have variable lengths, including February's 28 or 29 days and months with 30 or 31 days.
- The program uses 365 days as a classroom approximation, although the calendar year is closer to 365.25 days.
- A result such as 1.58 years is numerically useful but difficult to interpret as everyday time units.
- To express elapsed time as whole units, use integer division (`//`) to find the number of complete years and modulo (`%`) to find leftover days.
- The quotient and remainder preserve information that a single decimal result such as 1.58 years does not present in a human-friendly form.
- The example progressively prints whole years and remaining days, then checks whether the result is plausible against the decimal-year calculation.
- Continue from the leftover days: divide by 24 with `//` for whole days and use `% 24` for remaining hours.
- Repeat the quotient-and-remainder pattern with 60 minutes per hour and 60 seconds per minute.
- The 50,000,000-second example resolves to 1 year, 213 days, 16 hours, 53 minutes, and 20 seconds.
- Professor Rosen recommends building the solution incrementally and connects the seconds-to-hours-minutes-seconds pattern to Lab 2, Problem 4.
- The lecture shifts from exact unit conversion to estimation, using the classic question of how many trees grow in Washington State.
- The first simplifying assumption divides land into forest and non-forest, treating non-forest as treeless and forest as having a representative tree density.
- Satellite imagery and a rough visual estimate place forest cover in a broad range of about 40% to 60%, with 50% chosen for the calculation.
- The method reduces the large question to two estimates: Washington's area and the number of trees per unit of forest.
- The calculation uses Washington's land area, assumes 50% is forest, and converts square miles to acres using 640 acres per square mile.
- A rough density of about 100 trees per acre is used for a mature forest.
- Keeping quantities in named variables such as forest area, forest acres, and trees per acre makes the calculation's assumptions visible.
- Professor Rosen briefly compares state flags, including Alaska, Texas, and Pennsylvania, before returning to the area and tree-density estimates.
- Multiplying the estimated forest acreage by 100 trees per acre produces an estimate of roughly 2 billion trees in Washington.
- The goal of the estimate is the correct order of magnitude, meaning the answer has a plausible number of zeros rather than exact tree counts.
- Professor Rosen points to Randall Munroe's XKCD blog, What If?, as another example of answering unusual questions through explicit estimates.
- Munroe's punch-card data-storage question estimates Google's data warehouse from power use and computer storage, then compares the resulting pile with the area of New England.
Transcript excerpt
0:01 Let's go ahead and start. So, for today, what you're going to want to do is go to uh project three in the textbook. You can either go there directly or you can scroll down to module 2 overview and click on the link for Thursday's exercise. Oh, did I not Oh, okay. Not logged in for some reason. I'll log in and it brings me there. Exit assignment. So, okay. Um, so I'm going to be going over a pretty
0:50 good paradigm for the first couple of weeks when it comes to trying to use uh programming, especially for our lab two. In fact, if you've been if you've read over your lab two and especially looked at problem four, then the first program is going to look familiar. Um we're also going to go over like uh some other problem solving strategies that really don't fit anywhere into a particular curriculum. Okay. So um this first problem's pretty interesting uh I find. So again go to module two in the textbook over sorry module two on canvas. You can click here and that'll bring us to problem three as well as just simply going, you know, if
1:37 you're in your, if we're in our textbook, you go to runstone directly. You click foundations of Python programming and then you scroll all the way down to the appendices. The problem is how many handshakes, which is the last problem in this one, although the title is is um comput pondering face input compute output. So, is that an emoji or is it um a um Yeah, it is an emoji. So, I know because I can just paste it. All right. Anyway, so this is our first program over this is a program over here that we're going to be playing with for for some time
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Rosen.