Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Advanced Arithmetic | Numbers
Data Types in Python

bookAdvanced Arithmetic

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 β€” i.e., rounds down.

12
print(7 // 3) # 2 print(-7 // 3) # -3 (floors down: -2.333... β†’ -3)
copy

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.

123
print(7 % 3) # 1 print(-7 % 3) # 2 print(7 % -3) # -2
copy

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".

12
print(round(2.5), round(3.5)) # 2 4 print(round(2.675, 2)) # 2.67 (binary float nuance)
copy

The math Module

Import once and access many handy functions/constants.

123456
import 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
copy

1. What value will this code output?

2. What value will this code output?

3. Which call returns -3?

question mark

What value will this code output?

Select the correct answer

question mark

What value will this code output?

Select the correct answer

question mark

Which call returns -3?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain why the remainder has the same sign as the divisor in Python?

What are some practical examples of using floor division and modulo together?

Can you show more examples of using the math module functions?

Awesome!

Completion rate improved to 5.26

bookAdvanced Arithmetic

Swipe to show menu

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 β€” i.e., rounds down.

12
print(7 // 3) # 2 print(-7 // 3) # -3 (floors down: -2.333... β†’ -3)
copy

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.

123
print(7 % 3) # 1 print(-7 % 3) # 2 print(7 % -3) # -2
copy

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".

12
print(round(2.5), round(3.5)) # 2 4 print(round(2.675, 2)) # 2.67 (binary float nuance)
copy

The math Module

Import once and access many handy functions/constants.

123456
import 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
copy

1. What value will this code output?

2. What value will this code output?

3. Which call returns -3?

question mark

What value will this code output?

Select the correct answer

question mark

What value will this code output?

Select the correct answer

question mark

Which call returns -3?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3
some-alt