Advanced Arithmetic
A step beyond basics: learn how Python handles floor division and modulo (including negatives) and get a quick tour of the built-in math
module you'll use for everyday numeric work.
Floor Division (//
)
Returns the floor of the exact quotient — i.e., rounds down toward −∞.
12print(7 // 3) # 2 print(-7 // 3) # -3 (floors down: -2.333... → -3)
Why it matters: indexing chunks/pages, time splitting (hours from seconds), and any "how many full groups fit" calculation.
Modulo %
Gives the remainder in the identity:
a == (a // b) * b + (a % b)
In Python, the remainder has the same sign as the divisor b
.
123print(7 % 3) # 1 print(-7 % 3) # 2 (because -7 == (-3)*3 + 2) print(7 % -3) # -2 (because 7 == (-2)*(-3) + -2)
Why it matters: "every Nth" item, wrap-around (e.g., clock arithmetic), cycling through buckets.
Quick Note on Rounding
Built-in round(x, ndigits)
uses "round half to even".
12print(round(2.5), round(3.5)) # 2 4 print(round(2.675, 2)) # 2.67 (binary float nuance)
The math
Module (essentials)
Import once and access many handy functions/constants.
123456import math print(math.floor(2.9), math.ceil(2.1), math.trunc(-2.9)) # 2 3 -2 print(math.sqrt(9)) # 3.0 print(math.pi, math.e) # 3.14159... 2.71828... print(math.isfinite(1.0), math.isfinite(float('inf'))) # True False
floor/ceil/trunc
: down / up / toward zero (watch negatives);sqrt
: square root (float result);pi
,e
: common constants;isfinite
,isnan
,isinf
: sanity checks for special float values.
1. What value will this code output?
2. What value will this code output?
3. Which call returns -3
?
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you explain more about how floor division works with negative numbers?
What are some practical examples where modulo with negatives is useful?
Can you show more functions from the math module?
Awesome!
Completion rate improved to 5
Advanced Arithmetic
Scorri per mostrare il menu
A step beyond basics: learn how Python handles floor division and modulo (including negatives) and get a quick tour of the built-in math
module you'll use for everyday numeric work.
Floor Division (//
)
Returns the floor of the exact quotient — i.e., rounds down toward −∞.
12print(7 // 3) # 2 print(-7 // 3) # -3 (floors down: -2.333... → -3)
Why it matters: indexing chunks/pages, time splitting (hours from seconds), and any "how many full groups fit" calculation.
Modulo %
Gives the remainder in the identity:
a == (a // b) * b + (a % b)
In Python, the remainder has the same sign as the divisor b
.
123print(7 % 3) # 1 print(-7 % 3) # 2 (because -7 == (-3)*3 + 2) print(7 % -3) # -2 (because 7 == (-2)*(-3) + -2)
Why it matters: "every Nth" item, wrap-around (e.g., clock arithmetic), cycling through buckets.
Quick Note on Rounding
Built-in round(x, ndigits)
uses "round half to even".
12print(round(2.5), round(3.5)) # 2 4 print(round(2.675, 2)) # 2.67 (binary float nuance)
The math
Module (essentials)
Import once and access many handy functions/constants.
123456import math print(math.floor(2.9), math.ceil(2.1), math.trunc(-2.9)) # 2 3 -2 print(math.sqrt(9)) # 3.0 print(math.pi, math.e) # 3.14159... 2.71828... print(math.isfinite(1.0), math.isfinite(float('inf'))) # True False
floor/ceil/trunc
: down / up / toward zero (watch negatives);sqrt
: square root (float result);pi
,e
: common constants;isfinite
,isnan
,isinf
: sanity checks for special float values.
1. What value will this code output?
2. What value will this code output?
3. Which call returns -3
?
Grazie per i tuoi commenti!