Python Reference Library →

Python divmod() Function – Quotient and Remainder in One Go

Let’s look at divmod() function – quotient and remainder in one go

  1. Notice

    divmod(a, b) returns a pair containing the floor quotient and remainder. The divisor must be nonzero.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 7
print(divmod(number, 2))
Live state

Top-level scope

number→Object 1
Object 1 · int7
This step

divmod(a, b) returns a pair containing the floor quotient and remainder. The divisor must be nonzero.

Picture divmod() function – quotient and remainder in one go

divmod(a, b) returns a pair containing the floor quotient and remainder. The divisor must be nonzero.

Image brief

Alt text
A diagram for Python divmod() Function – Quotient and Remainder in One Go. Instructions: number = 7 print(divmod(number, 2)). Displayed result: (3, 1). The final example bindings are number --> object 1 (int). object 1: 7
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python divmod() Function – Quotient and Remainder in One Go". Teaching focus: divmod(a, b) returns a pair containing the floor quotient and remainder. The divisor must be nonzero. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 7 print(divmod(number, 2)) | v DISPLAYED RESULT (3, 1) FINAL BINDINGS number --> object 1 (int) object 1: 7 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python divmod() Function – Quotient and Remainder in One Go. Instructions: number = 7 print(divmod(number, 2)). Displayed result: (3, 1). The final example bindings are number --> object 1 (int). object 1: 7

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 7
print(divmod(number, 2))
Live state

Top-level scope

number→Object 1
Object 1 · int7
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
(3, 1)

What do you expect this program to print?

What does this example display?

number = 7
print(divmod(number, 2))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

(3, 1)

Follow the printed values in order. The output is: (3, 1)

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

divmod(a, b) returns a pair containing the floor quotient and remainder. The divisor must be nonzero.

divmod(a, b) returns a pair containing the floor quotient and remainder. The divisor must be nonzero.

More examples and reference

Python divmod() Function

The divmod() function in Python returns a tuple containing the quotient and the remainder when dividing two numbers. It's a quick way to get both results in one line.

Syntax

divmod(a, b)

Parameters:

  • a – The dividend (numerator)
  • b – The divisor (denominator); must not be 0

Returns:

  • A tuple (quotient, remainder)

Example 1: Integer Division

result = divmod(10, 3)
print(result)
(3, 1)

Why? Because 10 ÷ 3 = 3 with a remainder of 1.

Example 2: Assigning Quotient and Remainder

q, r = divmod(22, 5)
print("Quotient:", q)
print("Remainder:", r)
Quotient: 4
Remainder: 2

Example 3: Using with Floats

print(divmod(7.5, 2.5))
(3.0, 0.0)

Use Cases

  • Efficiently calculate both quotient and remainder in a single step
  • Common in loops where both values are needed
  • Useful in modular arithmetic problems

Common Mistakes

  • Passing 0 as the second argument will raise ZeroDivisionError
  • Both arguments must be numeric (int or float)

Interview Tip

In coding interviews, divmod() can simplify logic in problems involving floor division and modulo operations.

Summary

  • divmod(a, b) returns (a // b, a % b)
  • Works with both integers and floats
  • Returns a tuple with two elements: quotient and remainder

Practice Problem

Write a program that reads two integers from the user and prints their quotient and remainder using divmod().

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
q, r = divmod(a, b)
print("Quotient:", q)
print("Remainder:", r)

What to remember

Connect what you noticed in the example with the idea below.

Remember

divmod(a, b) returns a pair containing the floor quotient and remainder. The divisor must be nonzero.

Watch out for this idea

divmod() returns only the true-division result.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →