Table of Contents
The divmod() method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.
The syntax of divmod() is:
divmod(x, y)
1. divmod() Parameters
divmod() takes two parameters:
- x – a non-complex number (numerator)
- y – a non-complex number (denominator)
2. Return Value from divmod()
divmod() returns
(q, r)– a pair of numbers (a tuple) consisting of quotient q and remainder r
If x and y are integers, the return value from divmod() is same as (a // b, x % y).
If either x or y is a float, the result is (q, x%y). Here, q is the whole part of the quotient.
3. Example: How divmod() works in Python?
print('divmod(8, 3) = ', divmod(8, 3))
print('divmod(3, 8) = ', divmod(3, 8))
print('divmod(5, 5) = ', divmod(5, 5))
# divmod() with Floats
print('divmod(8.0, 3) = ', divmod(8.0, 3))
print('divmod(3, 8.0) = ', divmod(3, 8.0))
print('divmod(7.5, 2.5) = ', divmod(7.5, 2.5))
print('divmod(2.6, 0.5) = ', divmod(2.6, 0.5))
Output
divmod(8, 3) = (2, 2) divmod(3, 8) = (0, 3) divmod(5, 5) = (1, 0) divmod(8.0, 3) = (2.0, 2.0) divmod(3, 8.0) = (0.0, 3.0) divmod(7.5, 2.5) = (3.0, 0.0) divmod(2.6, 0.5) = (5.0, 0.10000000000000009)
Related posts:
Python Program to Count the Number of Occurrence of a Character in String
Python Function Arguments
Python sorted()
Python enumerate()
Python print()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python String split()
Python zip()
Python Closures
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Convert String to Datetime
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Find the Size (Resolution) of a Image
Python Program to Calculate the Area of a Triangle
Python String islower()
Python Set copy()
Python List clear()
Python Program to Check if a Number is Positive, Negative or 0
Python Dictionary setdefault()
Python Program to Find the Factors of a Number
Python Program to Check If a List is Empty
Python Program to Get Line Count of a File
Python callable()
Python getattr()
Python str()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Solve Quadratic Equation
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python String isalnum()
Python break and continue
Introduction to Scientific Programming with Python - Joakim Sundnes