The divmod() function performs division and remainder calculation at the same time. It returns a tuple containing the quotient and remainder.
Syntax
divmod(dividend, divisor)dividend→ The number being divideddivisor→ The number you divide by
Basic Example
result = divmod(10, 3)
print(result)Output:
(3, 1)This means:
10 ÷ 3
Quotient = 3
Remainder = 1Understanding divmod()
The following:
divmod(17, 5)returns:
(3, 2)Because:
17 = (5 × 3) + 2So:
Quotient → 3
Remainder → 2divmod() vs /
The / operator returns the division result as a floating-point number.
print(10 / 3)Output:
3.3333333333333335divmod() gives both quotient and remainder:
print(divmod(10, 3))Output:
(3, 1)divmod() vs // and %
You can achieve the same result using // and %.
number = 17 divisor = 5
quotient = number // divisor remainder = number % divisor
print(quotient)
print(remainder)Output:
3
2With divmod():
number = 17 divisor = 5
quotient, remainder = divmod(
number,
divisor
)
print(quotient)
print(remainder)Output:
3
2So:
divmod(a, b)is essentially:
(a // b, a % b)Store the Result in a Variable
result = divmod(25, 4)
print(result)Output:
(6, 1)The result is a tuple.
print(type(result))Output:
<class 'tuple'>Unpacking divmod()
You can directly unpack the returned tuple.
quotient, remainder = divmod(25, 4)
print("Quotient:", quotient)
print("Remainder:", remainder)Output:
Quotient: 6
Remainder: 1This is one of the most common ways to use divmod().
Get Only the Quotient
quotient, _ = divmod(25, 4)
print(quotient)Output:
6The _ indicates that the remainder is not needed.
Get Only the Remainder
_, remainder = divmod(25, 4)
print(remainder)Output:
1Using divmod() with Variables
total = 100 divisor = 7
quotient, remainder = divmod(
total,
divisor
)
print("Quotient:", quotient)
print("Remainder:", remainder)Output:
Quotient: 14
Remainder: 2Dividing Items into Groups
Suppose you have 25 students and each group contains 4 students.
students = 25 group_size = 4
groups, remaining = divmod(
students,
group_size
)
print("Groups:", groups)
print("Remaining:", remaining)Output:
Groups: 6
Remaining: 1So there are:
6 complete groups
1 student remainingPractical Example : Chocolates
Suppose you have 53 chocolates and want to distribute 8 chocolates to each student.
chocolates = 53 per_student = 8
students, remaining = divmod(
chocolates,
per_student
)
print("Students:", students)
print("Remaining:", remaining)Output:
Students: 6
Remaining: 5Practical Example : Money Distribution
Suppose you have Rs. 10,000 and want to distribute it equally among 6 people.
money = 10000 people = 6
amount, remaining = divmod(
money,
people
)
print("Each person:", amount)
print("Remaining:", remaining)Output:
Each person: 1666
Remaining: 4Practical Example : Convert Seconds to Minutes
divmod() is very useful for converting units.
Suppose we have 367 seconds.
seconds = 367
minutes, remaining_seconds = divmod(
seconds,
60
)
print("Minutes:", minutes)
print("Seconds:", remaining_seconds)Output:
Minutes: 6
Seconds: 7Therefore:
367 seconds = 6 minutes 7 secondsConvert Seconds to Hours, Minutes and Seconds
total_seconds = 7384
hours, remaining = divmod(
total_seconds,
3600
)
minutes, seconds = divmod(
remaining,
60
)
print("Hours:", hours)
print("Minutes:", minutes)
print("Seconds:", seconds)Output:
Hours: 2
Minutes: 3
Seconds: 4Therefore:
7384 seconds = 2 hours 3 minutes 4 secondsPractical Example : Convert Minutes to Hours
total_minutes = 185
hours, minutes = divmod(
total_minutes,
60
)
print("Hours:", hours)
print("Minutes:", minutes)Output:
Hours: 3
Minutes: 5Practical Example : Convert Days to Weeks
days = 45
weeks, remaining_days = divmod(
days,
7
)
print("Weeks:", weeks)
print("Days:", remaining_days)Output:
Weeks: 6
Days: 3Practical Example : Convert Months into Years
For a simple calculation using 12 months per year:
months = 29
years, remaining_months = divmod(
months,
12
)
print("Years:", years)
print("Months:", remaining_months)Output:
Years: 2
Months: 5divmod() with a Loop
You can use divmod() inside a loop.
numbers = [10, 15, 22, 30]
for number in numbers:
quotient, remainder = divmod(
number,
3
)
print(
number,
"→",
quotient,
remainder
)Output:
10 → 3 1
15 → 5 0
22 → 7 1
30 → 10 0divmod() with List Comprehension
numbers = [10, 15, 20, 25]
results = [
divmod(number, 5)
for number in numbers
]
print(results)Output:
[(2, 0), (3, 0), (4, 0), (5, 0)]divmod() with map()
Since you already learned map(), you can combine it with divmod().
numbers = [10, 15, 22, 30]
results = map(
lambda x: divmod(x, 3),
numbers
)
print(list(results))Output:
[(3, 1), (5, 0), (7, 1), (10, 0)]divmod() with filter()
You can use filter() to select numbers with a particular remainder.
For example, numbers that leave remainder 0 when divided by 3:
numbers = [10, 12, 15, 17, 18, 20]
result = filter(
lambda x: divmod(x, 3)[1] == 0,
numbers
)
print(list(result))Output:
[12, 15, 18]These numbers are divisible by 3.
Check Whether a Number Is Even
You can use the remainder returned by divmod().
number = 24
quotient, remainder = divmod(
number,
2
)
if remainder == 0:
print("Even")
else:
print("Odd")Output:
EvenCheck Whether a Number Is Odd
number = 17
_, remainder = divmod(
number,
2
)
if remainder != 0:
print("Odd")
else:
print("Even")Output:
OddPractical Example : Pagination
divmod() can be useful when calculating pages.
Suppose there are 53 records and each page contains 10 records.
records = 53 per_page = 10
full_pages, remaining = divmod(
records,
per_page
)
pages = full_pages
if remaining:
pages += 1
print("Total Pages:", pages)Output:
Total Pages: 6There are:
5 complete pages
3 records on the final pagePractical Example : Packing Products
Suppose a warehouse has 127 products and each box can hold 12.
products = 127 per_box = 12
boxes, remaining = divmod(
products,
per_box
)
print("Complete Boxes:", boxes)
print("Remaining Products:", remaining)Output:
Complete Boxes: 10
Remaining Products: 7Practical Example : Exam Seating
Suppose there are 125 students and each classroom can accommodate 30.
students = 125 capacity = 30
classrooms, remaining = divmod(
students,
capacity
)
if remaining:
classrooms += 1
print("Classrooms Required:", classrooms)Output:
Classrooms Required: 5Negative Numbers
divmod() also works with negative numbers, but the result follows Python's floor division rules.
print(divmod(-10, 3))Output:
(-4, 2)Because:
-10 = (3 × -4) + 2For beginners, the most important thing is to remember that divmod() follows the same rules as // and %.
Decimal Numbers
divmod() can also work with floating-point numbers.
result = divmod(10.5, 3)
print(result)Output:
(3.0, 1.5)The values are:
Quotient → 3.0
Remainder → 1.5Important: Divisor Cannot Be Zero
This will produce an error:
result = divmod(10, 0)Python raises:
ZeroDivisionErrorAlways make sure the divisor is not zero.
divmod() vs %
Use % when you only need the remainder:
remainder = 17 % 5
print(remainder)Output:
2Use divmod() when you need both:
quotient, remainder = divmod(17, 5)
print(quotient)
print(remainder)Output:
3
2divmod() vs //
Use // when you only need the quotient:
quotient = 17 // 5
print(quotient)Output:
3Use divmod() when you need both quotient and remainder:
quotient, remainder = divmod(17, 5)Important Points to Remember
Basic syntax
divmod(a, b)Returns a tuple
result = divmod(17, 5)
print(result)(3, 2)Unpacking
quotient, remainder = divmod(17, 5)Equivalent operation
divmod(a, b)is equivalent to:
(a // b, a % b)Quotient only
quotient, _ = divmod(17, 5)Remainder only
_, remainder = divmod(17, 5)Easy way to remember
/ → Normal division
// → Quotient
% → Remainder
divmod → Quotient + Remainder