Programming fundamentalsLesson 3 of 4 in the course Expressions

Python Division and Modulo for Units, Time, and Coin Change

5 flashcards · 5 quiz questions · 5 match pairs · 5 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

This lesson explains how Python divides numbers using floor division and modulo, and how these operators work together to convert units and calculate time or money amounts.

Python has two common division operators. True division (/) produces a floating-point result: 7 / 4 evaluates to 1.75. Floor division (//) gives the quotient rounded down to an integer for positive whole-number calculations: 7 // 4 evaluates to 1.

All flashcards

What is floor division in Python?
Floor division uses `//` to produce the quotient rounded down. For example, `7 // 4` is `1`.
What does the modulo operator do?
The `%` operator returns the remainder after division. For example, `7 % 4` is `3`.
How do `//` and `%` help convert centimeters to meters?
Divide by 100 with `//` to count whole meters, then use `% 100` to find the remaining centimeters. For 193 cm, the result is 1 m and 93 cm.
How do you convert a clock time to total minutes?
Multiply the hour by 60 and add the minute: `hour * 60 + minute`.
How can a program break change into coin counts?
Represent the change as whole cents, then use floor division to count each coin denomination and modulo to find the cents left for the next denomination.

Quiz questions

  1. What does the Python expression `7 / 4` evaluate to?Answer: 1.75
  2. What time is 340 minutes after 13:25?Answer: 19:05
  3. Why can a program apply modulo 1440 to total minutes when calculating a clock time?Answer: A day has 1440 minutes, so the result fits within one 24-hour day.
  4. For change of 124 cents, what coin counts result from using dollars, quarters, dimes, nickels, and pennies in that order?Answer: 1 dollar, 0 quarters, 2 dimes, 0 nickels, and 4 pennies.
  5. When converting a decimal dollar amount to cents, which Python function do the notes recommend using?Answer: `round`

Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.

All Programming fundamentals lessons