How to Divide Without a Remainder in Python
Performing division without a remainder is a common task in Python.
This guide explores several methods for achieving this, focusing on the floor division operator (//
), the math.floor()
and math.ceil()
methods, the round()
function, and the int()
and math.trunc()
functions for truncating floating-point results. You'll learn when to use each approach and what the difference is between the methods.
Floor Division with the //
Operator
The floor division operator (//
) performs division and returns the largest integer less than or equal to the result, removing any fractional part. This is the preferred and most readable way to perform integer division in Python.
result_1 = 30 // 6
print(result_1) # Output: 5
print(type(result_1)) # Output: <class 'int'>
result_2 = 11 // 2
print(result_2) # Output: 5
result_3 = 30 / 6
print(result_3) # Output: 5.0
print(type(result_3)) # Output: <class 'float'>
- The floor division operator will return an integer when dividing two integer values, unlike the division operator
/
which always returns a float. - The floor division operator is equivalent to applying a floor operation after the division.
Using math.floor()
for Floor Division
The math.floor()
method returns the largest integer less than or equal to the given number. It can be used in conjunction with the division operator /
if you need to get the result of the division as a float first.
import math
result_1 = math.floor(11 / 2)
print(result_1) # Output: 5
result_2 = 11 / 2
print(result_2) # Output: 5.5
- First division is performed using
/
which returns a float. - Then the
math.floor()
is called on the result which returns the largest integer less or equal than the value returned by the division.
Using math.ceil()
for Ceiling Division
The math.ceil()
method returns the smallest integer greater than or equal to the given number:
import math
result_1 = math.ceil(11 / 2)
print(result_1) # Output: 6
result_2 = 11 / 2
print(result_2) # Output: 5.5
- The
math.ceil()
method rounds the result of the division operation up to the nearest integer.
Rounding with round()
The round()
function rounds a number to the nearest integer:
result_1 = round(15 / 4)
print(result_1) # Output: 4
result_2 = 15 / 4
print(result_2) # Output: 3.75
- The
round
function can be used when you need to round the result to the closest integer. - If you also provide the
ndigits
argument, it will round the number to the specified number of digits after the decimal.
Truncating with int()
The int()
constructor can remove the fractional part of a floating-point number, truncating it towards zero:
result_1 = int(7 / 2)
print(result_1) # Output: 3
result_2 = int(-7 / 2)
print(result_2) # Output: -3
- The
int()
function truncates the decimal and returns the integer value. This approach is particularly useful if you also want to handle the negative numbers consistently, as it always returns a value closest to0
.
Truncating with math.trunc()
The math.trunc()
method also removes the fractional part of a number, rounding toward zero, which results in the same behavior as using the int()
constructor.
import math
result_1 = math.trunc(7 / 2)
print(result_1) # Output: 3
result_2 = math.trunc(-7 / 2)
print(result_2) # Output: -3
- The
math.trunc()
function works similarly to theint()
constructor and rounds the result towards 0.