Aritmética Avanzada
Learn how Python handles floor division and modulo (including negative numbers) and explore the math module for common numeric operations.
Floor Division (//)
Returns the floor of the exact quotient, meaning it rounds the result down.
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 %
Returns the remainder of division. In Python, the remainder always has the same sign as the divisor.
123print(7 % 3) # 1 print(-7 % 3) # 2 print(7 % -3) # -2
Why it matters: "every Nth" item, wrap-around (e.g., clock arithmetic), cycling through buckets.
Examples:
- Keeping track of hours on a clock →
14 % 12 = 2- (2 PM); - Selecting every 3rd item in a list →
if i % 3 == 0:.
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
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
1. What value will this code output?
2. What value will this code output?
3. Which call returns -3?
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Genial!
Completion tasa mejorada a 3.45
Aritmética Avanzada
Desliza para mostrar el menú
Learn how Python handles floor division and modulo (including negative numbers) and explore the math module for common numeric operations.
Floor Division (//)
Returns the floor of the exact quotient, meaning it rounds the result down.
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 %
Returns the remainder of division. In Python, the remainder always has the same sign as the divisor.
123print(7 % 3) # 1 print(-7 % 3) # 2 print(7 % -3) # -2
Why it matters: "every Nth" item, wrap-around (e.g., clock arithmetic), cycling through buckets.
Examples:
- Keeping track of hours on a clock →
14 % 12 = 2- (2 PM); - Selecting every 3rd item in a list →
if i % 3 == 0:.
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
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
1. What value will this code output?
2. What value will this code output?
3. Which call returns -3?
¡Gracias por tus comentarios!