Modular Arithmetic in Python
- 44Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What does the modulo operation calculate?
The remainder left over after dividing one number by another.
Or swipe the card
44 cards
What does the modulo operation calculate?
The remainder left over after dividing one number by another.
In amodb, what does a represent?
a is the dividend: the number being divided.
In amodb, what does b represent?
b is the divisor and modulus.
What does the quotient represent in a division used to find a remainder?
The number of times the divisor goes into the dividend.
What does the modulus determine about possible outputs?
It sets the range of possible remainders.
What is 6mod2, and why?
0, because 2 goes into 6 three times with nothing left over.
What is 5mod2, and why?
1, because 2 goes into 5 twice, leaving a remainder of 1.
How can you find the remainder after dividing a by b using the quotient q?
Subtract the product of the quotient and divisor from the dividend: a−qb.
Which Python operator performs a modulo operation?
The percent sign,
%.Is Python's
%modulo operator limited to integers?No. It works with integers and floating-point numbers, though the examples here focus on integers.
What is
fmodused for in Python?It is an alternative function for performing modulo operations.
What should you consult to understand differences between Python's modulo approaches, such as
%andfmod?Documentation or other references that explain how the functions differ.
How can Python's modulo result be checked against a hand calculation?
Compute the remainder manually, then evaluate the same operands with
%and compare the results.What does floor division return in Python?
The whole-number quotient: how many times the divisor goes into the dividend.
How can floor division be used to calculate a remainder from dividend a and divisor b?
Use a−(a//b)×b.
What is the result of 5−(5//2)×2?
1: floor division gives a quotient of 2, and 5−2×2=1.
What are the two inputs to the custom modulo function described in the content?
The dividend and the modulus (divisor).
What does a custom modulo function return after applying the floor-division formula?
The remainder, calculated by subtracting the divisor-times-quotient product from the dividend.
Which two examples were used to check the custom modulo function?
6mod2=0 and 5mod2=1.
How can a custom modulo function be tested over many integer inputs?
Use nested loops over a range of dividends and moduli, and compare the custom result with Python's
%result.In the described loop test, what does a mismatch between the custom result and
%indicate?It indicates a possible error in the custom modulo function.
What is 9mod9?
0, because 9 goes into 9 once with no remainder.
What is 9mod8?
1, because 8 goes into 9 once, leaving 1.
Why use modulo arithmetic when advancing an hour on a 24-hour clock?
It wraps the hour back to the start of the day after it reaches 24.
What modulus is used to wrap hours on a 24-hour clock?
24.
How is an updated clock hour calculated from the current hour and an offset?
Add the offset to the current hour, then take the result modulo 24.
How does the clock function handle an offset of zero?
It displays the current hour, because no hours are added or subtracted.
How can the clock function calculate a future time?
Pass a positive number of hours as the offset.
How can the clock function calculate a past time?
Pass a negative number of hours as the offset.
If the current hour is 23, what hour results from adding one hour modulo 24?
0; the clock wraps around to the beginning of the day.
When the clock function changes the hour, what happens to the minutes?
The minutes remain the current minutes; the function changes only the hour.
How is 9 p.m. written in the military-time style described?
2100.
Why does the clock function add a leading zero to a one-digit hour or minute?
To keep the military-time display in a two-digit hour and minute format, such as
03or09.Does the described clock function print its formatted time or return it?
It prints the formatted time rather than returning a value.
Why does the clock code use a time-zone-aware current time?
Without a specified time zone, the time may be returned in universal time rather than the desired local zone.
What Python package is used in the example to work with time zones?
pytz.What is the example clock function's default time zone?
Eastern Standard Time.
How can the clock function display a time in a different time zone?
Supply that location's time-zone name as the time-zone argument.
How can you find available time-zone names using
pytz?Use its
all_timezoneslist.How is modular arithmetic used in the Diffie–Hellman key exchange?
It is used as part of a cryptographic method that lets two parties securely exchange keys over a public channel.
How can modular arithmetic help grocery stores track inventory?
Checksums associated with product barcodes can use modular arithmetic to help verify UPC codes.
What is the role of a checksum in a UPC code?
It uses modular arithmetic to help verify that the UPC is valid.
How many digits does the content describe as comprising a UPC?
Ten digits.
How is modular arithmetic used with ISBNs?
It is used in processes that create or verify the unique values associated with books.

Ask anything
We've read this whole study set, so questions about it get the best answers.