In this article, we will explain the basic concepts of division in Python, with a particular focus on floor division. In the upcoming sections, we will deepen practical knowledge by providing concrete code examples and cautions.
2. Python Division: The Basic / Operator
2-1. / (slash) Operator Basics
When you use / in Python, the result is always a floating-point number (float). This is also true for division between integers.
Although they appear similar, int() first converts the result of / to a float before truncating, so using // directly is preferable.
3-3. Beware of Truncating Negative Numbers (-10 // 3 Behavior)
When calculating with negative numbers using //, the result follows the mathematical floor function (rounding down), which can produce unintuitive outcomes.
Example: Floor Division with Negative Numbers
print(-10 // 3) # Output: -4
In standard mathematics, -10 ÷ 3 = -3.333..., which might suggest -3, but Python’s // always rounds toward the smaller (more negative) direction, resulting in -4.
// always rounds toward the smaller (more negative) direction, so you should verify that it yields the intended result for negative numbers.
When you want to quickly process calculations that don’t need decimals
int(10 / 3) is slower than 10 // 3.
4. Python Ceiling Division (math.ceil())
4-1. Basics of math.ceil()
Python’s math.ceil() is a function that unconditionally rounds up the fractional part and returns an integer. This function is included in the math module, so you need to import math when using it.
For negative numbers, it rounds toward the larger direction
4-3. Practical examples using math.ceil()
4-3-1. Calculating number of pages
For example, when dividing data into pages, taking into account the number of items per page, you need to round up the page count even if there is a remainder.
import math
items = 45 # total number of items
per_page = 10 # number of items per page
total_pages = math.ceil(items / per_page)
print(total_pages) # Output: 5
4-3-2. Calculating required number of work assignments
For example, suppose 100 tasks are processed by 5 people. When calculating the number of tasks each person should handle, if there is a remainder, you need to process one extra.
import math
tasks = 100 # total number of tasks
workers = 6 # number of workers
tasks_per_worker = math.ceil(tasks / workers)
print(tasks_per_worker) # Output: 17
4-4. How to achieve ceiling without using math.ceil()
math.ceil() can be avoided by using integer division to achieve ceiling. This is the following ‘integer arithmetic using addition and subtraction‘.
Using (a + b – 1) // b
This method adds (b - 1) to the dividend a and divides by b, achieving a ceiling effect.
def ceil_div(a, b):
return (a + b - 1) // b
print(ceil_div(10, 3)) # Output: 4
print(ceil_div(5, 2)) # Output: 3
4-5. Uses and summary of math.ceil()
Python’s math.ceil() is useful when you need to round up the fractional part to get an integer. It is especially effective in situations such as:
a % b returns the remainder of a divided by b. For example, with 10 % 3, 10 ÷ 3 = 3 ... 1, so the remainder 1 is produced.
Be Careful with Remainders of Negative Numbers
The Python % operator is affected by the sign of the dividend (the number being divided), so you need to be careful when dealing with negative numbers.
By leveraging divmod(), you can easily calculate how to break an amount into specific coin denominations.
amount = 758 # 758 yen
yen_100, remainder = divmod(amount, 100) # number of 100-yen coins
yen_50, remainder = divmod(remainder, 50) # number of 50-yen coins
yen_10, remainder = divmod(remainder, 10) # number of 10-yen coins
print(f"100 yen: {yen_100} coins, 50 yen: {yen_50} coins, 10 yen: {yen_10} coins, remainder: {remainder} yen")
5-5. Summary of Division and Remainder Calculation Methods
When calculating quotients and remainders in Python, it’s important to choose the method appropriate to the use case.
Method
Usage
Quotient
Remainder
//
a // b
○
×
%
a % b
×
○
divmod()
divmod(a, b)
○
○
In particular, divmod() can retrieve both the quotient and remainder at once, reducing repeated calculations and allowing you to write more efficient code.
6. Frequently Asked Questions (FAQ) about Division in Python
6-1. How to make division results integers in Python?
There are three main ways to get integer results from division in Python.
① Use // (floor division)
// yields an integer result with the fractional part truncated.
In Python, dividing by zero raises a ZeroDivisionError.
print(10 / 0) # ZeroDivisionError: division by zero
To prevent this error, you need to check for zero beforehand.
a, b = 10, 0
if b != 0:
print(a / b)
else:
print("Cannot divide by zero")
6-7. What are the benefits of divmod()?
Using Python’s divmod() function lets you obtain the quotient and remainder simultaneously, reducing the number of calculations and enabling more efficient code.
Python offers many ways to perform division, each suited to different use cases. Summarizing the content covered in this article, we have the following:
Standard division (/) returns a result of type float.
If you want an integer result, use // (floor division).
When dealing with negative numbers, be aware of //‘s behavior (it truncates toward the negative direction).
Use round() for rounding to the nearest integer.
Use math.ceil() to round up, and math.floor() to round down.
Use % to get the remainder, and divmod() to obtain both quotient and remainder simultaneously.
If high precision is required, use decimal.Decimal.
If you can use division in Python properly, you can handle data processing and calculations more flexibly and accurately. This concludes the article. Have you deepened your understanding of Python’s division and become able to choose the appropriate method? If you have questions, refer to the official Python documentation or try writing code yourself. Continue learning to make Python programming even more comfortable in the future!