Calculating Quotient and Remainder in Python: Using Arithmetic Operators vs divmod

In programming, you often need to find the “quotient” (the answer to division) and the “remainder” (what is left over). Common examples include converting total seconds into “minutes and seconds” or calculating the leftovers when distributing items evenly.

In Python, there are two ways to do this: using separate arithmetic operators or using the built-in divmod() function to calculate both at once.

This article explains both methods and how to choose between them.

目次

1. Using Arithmetic Operators (// and %)

Python provides specific operators for finding the quotient and remainder.

  • // (Floor Division): Calculates the quotient (integer part) of the division.
  • % (Modulo): Calculates the remainder of the division.

Specific Usage Example: Converting Time

Here is a program that converts a video playback time of “250 seconds” into “minutes and seconds”.

# Total video duration in seconds
total_seconds = 250

# 1 minute is 60 seconds
seconds_per_minute = 60

# Calculate quotient (minutes)
minutes = total_seconds // seconds_per_minute

# Calculate remainder (seconds)
remaining_seconds = total_seconds % seconds_per_minute

print(f"Total seconds: {total_seconds}")
print(f"Playback time: {minutes} min {remaining_seconds} sec")

Execution Result:

Total seconds: 250
Playback time: 4 min 10 sec

Since 250 divided by 60 is 4 with a remainder of 10, the calculation is correct.

2. Using the divmod() Function (Recommended)

If you need both the quotient and the remainder, using the built-in divmod() function allows you to get both values in a single calculation.

Syntax:

quotient, remainder = divmod(dividend, divisor)

The return value is a tuple (quotient, remainder), so it is common to use unpacking (assigning to multiple variables at once) to receive the values.

Specific Usage Example: Distributing Items

Consider a case where you distribute 45 cookies evenly among 7 students.

# Total number of cookies
cookie_count = 45

# Number of students
student_count = 7

# Get amount per student (quotient) and leftovers (remainder) at once
per_student, leftover = divmod(cookie_count, student_count)

print(f"Cookies per student: {per_student}")
print(f"Leftover cookies: {leftover}")

Execution Result:

Cookies per student: 6
Leftover cookies: 3

45 divided by 7 is 6 with a remainder of 3. Compared to using operators twice (// and %), calling divmod() once makes the code more concise and can be slightly more efficient.

Note: Handling Negative Numbers

A feature of Python’s modulo operator (%) is that the result takes the same sign as the “divisor” (denominator).

# Dividing a negative number
val_a = -10
divisor = 3

# -10 // 3 becomes -4 (rounded towards the smaller integer)
quotient = val_a // divisor

# -10 % 3 becomes 2
# Calculation: -10 = 3 * (-4) + 2
remainder = val_a % divisor

print(f"Quotient: {quotient}, Remainder: {remainder}")

Execution Result:

Quotient: -4, Remainder: 2

In other programming languages (like C or Java), the remainder of -10 / 3 often results in -1. Be careful with this specification when handling negative numbers in Python.

Summary

  • x // y: Calculates the quotient (integer).
  • x % y: Calculates the remainder.
  • divmod(x, y): Returns both as a tuple (quotient, remainder).

If you need both values, using divmod() is recommended for better readability and efficiency.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次