Integer Division in Python: //, divmod(), and Floats

Quick Answer

Use / for a decimal quotient, // for floor division, and divmod(a, b) when you need both quotient and remainder. In Python, floor division rounds toward negative infinity, so negative operands need an explicit check.

Python integer division comparison for slash, floor division, and divmod
The / operator returns a quotient, // floors the quotient, and divmod() returns the quotient and remainder together.

Python provides several numeric types, including integers, floating-point numbers, and complex numbers. This article focuses on integer division, more precisely Python’s // floor-division operator. The official Python expression reference defines // as floor division and % as the related modulo operator.

What is Integer Division?

Integer division usually means dividing numbers and keeping the whole-number quotient. In Python 3, the // operator performs floor division: it applies the mathematical floor function to the exact quotient. With two int operands, the result is an int. With a float operand, the result can be a whole-number float, such as 3.0.

Integer Division

In Python, use the // operator for floor division. The result is the largest integer less than or equal to the exact mathematical result. This distinction matters most for negative numbers: -7 // 2 returns -4, not -3.

For example, consider the following code:

print(10 // 3)

The output of the above code will be 3.

This article focuses on Python 3. In Python 3, / performs true division and returns a floating-point result for normal numeric operands, while // performs floor division.

If either operand is a float, // still floors the quotient, but the result type is typically float.

For example:

print(10.0 // 3)
print(10 // 3.0)
print(10.0 / 3)
print(10 / 3.0)

In all of these cases, the result will be a floating-point number, not an integer.

Python Pool infographic comparing slash division, floor division, modulo, and divmod results
Division operators: Slash division, floor division, modulo, and divmod results.

Division vs. Integer Division

It’s important to understand the difference between division and integer division, as they can produce different results. Consider the following example:

print(10 / 3)
print(10 // 3)

The output of the first line will be 3.333333..., while the output of the second line will be 3. In other words, division returns a floating-point number, while integer division returns an integer.

Conversion between Integer and Float

In some cases, you may need to convert an integer to a float or a float to an integer. This can be done using the int() and float() functions in Python.

For example:

print(float(10))
print(int(10.9))

The output of the first line will be 10.0, and the output of the second line will be 10.

Integer division and remainder

The // operator performs integer division, which means that it returns the quotient of the division, discarding any remainder. For example, 7 // 2 would return 3, since 7 divided by 2 is 3 with a remainder of 1.

The % operator returns the remainder of the division. For example, 7 % 2 would return 1, since 7 divided by 2 is 3 with a remainder of 1.

Here is an example that demonstrates the use of these operators:

a = 7
b = 2

quotient = a // b
remainder = a % b

print("The quotient of", a, "divided by", b, "is", quotient)
print("The remainder of", a, "divided by", b, "is", remainder)

Output:

The quotient of 7 divided by 2 is 3
The remainder of 7 divided by 2 is 1

Python Pool infographic showing positive and negative operands moving to the mathematical floor
Floor behavior: Positive and negative operands moving to the mathematical floor.

Python integer division by zero

In Python, integer division by zero raises a ZeroDivisionError. This error occurs when you attempt to divide an integer by zero, either explicitly or as a result of some calculation.

Here is an example of code that would raise a ZeroDivisionError:

a = 10
b = 0
c = a // b  # integer division by zero
# the following code will not be executed since an error has occurred
print("The result of the division is:", c)

Output:

ZeroDivisionError: integer division or modulo by zero

To avoid this error, you can check whether the denominator is zero before performing the division.

Python integer division ceiling

In Python, to get the ceiling value of the result of an integer division, you can use the math module and its ceil function. The ceil function takes a single argument, which is a floating-point number, and returns the smallest integer that is greater than or equal to the argument.

To use math.ceil() to calculate the ceiling value of an integer division, you need to first convert the dividend and divisor to floating-point numbers. Here is an example:

import math

a = 7
b = 2

quotient = a / b
ceiling = math.ceil(quotient)

print("The result of the division is:", quotient)
print("The ceiling value of the division is:", ceiling)

Output:

The result of the division is: 3.5
The ceiling value of the division is: 4

In this example, a and b are both integers. We first perform the division a / b, which results in a floating-point number with a value of 3.5. We then pass this result to math.ceil(), which returns the ceiling value of 4.

Python integer division too large for a float

Python integers have unlimited precision, so floor division between very large integers normally returns another integer rather than overflowing. An OverflowError is more likely when you convert a huge integer to float or call a floating-point function on it.

Here is a safer way to think about the difference:

large_number = 10**1000

# This stays in integer arithmetic.
print(large_number // 3)

# This can raise OverflowError because it asks for a float.
print(float(large_number))

Output:

OverflowError: int too large to convert to float
Python Pool infographic mapping divmod through quotient, remainder, and the division identity
Quotient remainder: Divmod through quotient, remainder, and the division identity.

Integer division python round up

In Python, to perform integer division and round up to the nearest integer, you can use the math module and its ceil function. The ceil function returns the smallest integer greater than or equal to the input.

You can also use // with a remainder check, but be careful with negative values. For positive integers, (a + b - 1) // b is a common round-up pattern.

Python integer division vs floor

In Python, // is the floor-division operator. It is not a separate operation that rounds toward zero. For integers, a // b gives the same quotient part that divmod(a, b) returns. For floats, it still floors the result, but the returned value is a float.

ExpressionResultWhy
7 // 23Floor of 3.5
-7 // 2-4Floor of -3.5
7.0 // 23.0Float operand gives a float result
divmod(7, 2)(3, 1)Returns quotient and remainder together
Examples of Python floor division behavior

For nearby Python number topics, read these Python Pool guides next: Python double slash operator, Python division assignment operator, Python order of operations, Python divmod, and rounding down numbers in Python.

Python Pool infographic testing zero divisor, floats, negatives, signs, and exact results
Division checks: Zero divisor, floats, negatives, signs, and exact results.

FAQs

What happens if the operands of integer division are floating-point numbers?

If the operands of integer division in Python are floating-point numbers, the result will be a floating-point number. The // operator can be used to perform integer division with floating-point numbers.

Can integer division in Python return a fractional part?

No, integer division in Python only returns the floor of the division result, discarding any fractional part.

Conclusion

In this article, we’ve explored the concept of integer division and how it works. We’ve looked at the difference between division and integer division, and how to convert between integers and floats. Whether you’re working with positive or negative numbers, the // operator makes it easy to perform integer division and obtain the desired result. With this understanding, you can use integer division effectively in your code.

True Division, Floor Division, and Remainders

Python has separate operators for decimal division and floor division. The expression 17 / 5 produces 3.4, while 17 // 5 produces 3. Use % for the remainder and divmod() when both values are needed.

total = 17
size = 5

quotient = total // size
remainder = total % size
print(quotient, remainder)  # 3 2
print(divmod(total, size))  # (3, 2)

The identity a == (a // b) * b + (a % b) holds for integer operands when the divisor is nonzero. This makes // and % useful for grouping items, pagination, clocks, and chunked data.

Negative Numbers Use Floor Semantics

Floor division is not truncation toward zero. For example, -17 // 5 is -4, because -3.4 is floored to the next lower integer. The matching remainder is positive: -17 % 5 is 3.

print(-17 / 5)   # -3.4
print(-17 // 5)  # -4
print(-17 % 5)   # 3

If your algorithm needs truncation toward zero, use int(a / b) only when floating-point conversion is safe, or use an integer-specific rule that states the desired behavior. Do not substitute // without checking negative inputs.

Frequently Asked Questions

What does // mean in Python?

The // operator performs floor division. It returns the quotient rounded down toward negative infinity rather than a decimal result.

What is the difference between / and // in Python?

/ performs true division and normally returns a float, while // performs floor division and returns the floor of the quotient.

How do I get the quotient and remainder together?

Call divmod(a, b). For divmod(17, 5), Python returns (3, 2), meaning quotient 3 and remainder 2.

Does Python integer division truncate negative values toward zero?

No. // floors toward negative infinity. For example, -17 // 5 is -4, not -3.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted