Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
tuple
communitycreator

What is the divmod() function in Python?

AKASH BAJWA

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

divmod() is a built-in method in Python that takes two real numbers as arguments and returns a pair of numbers (tuple) consisting of quotient q and remainder r values. Although modulus and division are two different operations, they are indirectly related as one returns the quotient, and the other one returns the remainder.

Syntax

The syntax of the divmod() method in Python is as follows.

divmod(dividend, divisor)

Parameters

  • dividend or numerator
  • divisor or denominator

This function takes two non-complex integers as parameters.

Return value

divmod() returns a tuple that contains the quotient and remainder.

  • If x (dividend) and y (divisor) are integers, then the return value is the pair (x / y, x % y).
  • If x (dividend) and y (divisor) are floats, the result is the pair of the whole part of the quotient and x modulus y.

Application: Checking a number, either a Prime number or not.

Question

Check if a number is prime or not by using the divmod() function.

Show Answer

Code

Below are some examples to understand the functionality of the divmod() method.

  • Case #1: both arguments are integer values, which results in a tuple of integer values.
  • Casen#2: both arguments are floating-point or double types, which results in a tuple of float values.
  • Casen#3: one value is a floating-point value and the other is an integer, which results in a tuple of float values.
# Python divmod() function  example
# Calling function
result = divmod(20,2)
# Displaying result
print(result)
print(type(result))

RELATED TAGS

python
tuple
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring