Python
Math, random & Decimal
37 flashcards · answers and spaced-repetition review in the KnowCard app
What does this print?
```
print(round(0.5), round(2.5))
print(round(1.5), round(3.5))
```
You need to round prices half-up (2.5 -> 3, never 2). Why won't built-in round() do it, and what does?
What is the difference in result and return type between math.floor(-2.5), math.trunc(-2.5), and math.ceil(-2.5)?
You compare two values near zero with math.isclose(a, 0.0). Why might it return False even when a is tiny, and how do you fix it?
What does this print?
```
import math
nan = math.nan
print(nan == nan)
print(math.isnan(nan))
```
What does this print?
```
from decimal import Decimal
print(Decimal("0.1") + Decimal("0.2"))
```
What does this print, and what's the bug?
```
from decimal import Decimal
print(Decimal(0.1))
```
What does this print?
```
from decimal import Decimal
print(Decimal("0.7") == 0.7)
print(Decimal(0.7) == 0.7)
```
You set decimal.getcontext().prec = 3 then compute Decimal("1.23456789") * Decimal("2.3456789"). What does prec control, and what surprises people?
What does this print, and why use Fraction here?
```
from fractions import Fraction
print(Fraction(1, 3) + Fraction(1, 6))
```
Two runs of your program must produce the identical random sequence for a test. What single call guarantees this, and what's the common mistake?
What range of values can random.randint(1, 6) return, and how does that differ from range(1, 6)?
What does this print?
```
import random
l = [1, 2, 3, 4]
result = random.shuffle(l)
print(result)
```
When do you reach for random.choice vs random.sample vs random.choices?
You're generating a password reset token with random.randbytes / random.random. Why is that a security bug, and what should you use?
What do statistics.mean and statistics.median return for [1, 2, 2, 100], and when does the difference matter?
statistics has both stdev and pstdev. Which do you call for a sample drawn from a larger population, and why does it matter?
You want to pause a random-number-driven simulation, save exactly where the generator is, and resume later so the sequence continues unbroken. Which two random functions do this?
In the random module, when do you use random(), uniform(a, b), and gauss(mu, sigma)?
You need a random even number in [0, 48], and separately a random 8-bit integer. randint can't express either cleanly — what does?
statistics has mean, geometric_mean, and harmonic_mean. When would you reach for the geometric or harmonic one?
Why is if x == math.nan: always wrong, no matter what x is?
What does this print?
import math, cmath
print(cmath.sqrt(-1))
print(math.sqrt(-1))
What do these print, and when do you need multimode instead of mode?
import statistics
print(statistics.mode([1, 1, 2, 2, 3]))
print(statistics.multimode([1, 1, 2, 2, 3]))
Before Python 3.9, math.gcd(a, b) took exactly two arguments. What changed, and what do gcd/lcm return?
What does this print, and what's the underlying rule?
import math
print(math.fmod(-7, 3))
print(-7 % 3)
What does this print, and why does it matter?
import math
print(sum([1e16, 1, -1e16]))
print(math.fsum([1e16, 1, -1e16]))
You have two points (x1, y1) and (x2, y2) and want the straight-line distance between them. Which math function, and how does it differ from hypot?
What does random.SystemRandom give you over the plain random module, and which of its methods stop working?
What do these print, and how do median_low / median_high differ from median for even-length data?
import statistics
data = [1, 3, 5, 7]
print(statistics.median(data))
print(statistics.median_low(data))
print(statistics.median_high(data))
statistics.mean([1, 2.0, Fraction(1,2)]) mixes int, float and Fraction. Why is that risky?
Besides a string, a Decimal can be built from a tuple. What does this produce, and what do the three elements mean?
from decimal import Decimal
print(Decimal((0, (3, 1, 4, 1), -3)))
What does this print, and why is this behavior specifically wanted for money?
from decimal import Decimal
print(Decimal("2.50") + Decimal("4.20"))
You do math.sqrt(Decimal("2")) to keep full precision. Why does that defeat the purpose, and what's the fix?
What does Decimal("9").fma(2, -7) compute, and why prefer fma over writing d * 2 - 7?
A Decimal can hold NaN and sNaN. What's the difference when you compute with them?
from decimal import Decimal
Decimal("NaN") + Decimal("42.42")
Decimal("sNaN") + Decimal("42.42")
You set decimal.getcontext().Emax = 9 and then multiply two large Decimals. What happens?
Decimal("1e100") * Decimal("1e100")
Start learning today
Free to start — download the app or use it in your browser.
