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 List remove()
Python Program to Get the Class Name of an Instance
Python String find()
Python String ljust()
Python String count()
Python Program to Print the Fibonacci sequence
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Get File Creation and Modification Date
Python Program to Copy a File
Python Program to Display the multiplication Table
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python String isdecimal()
Python List insert()
Python Dictionary
Python Program to Convert Bytes to a String
Python Program to Find Factorial of Number Using Recursion
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Multiple Inheritance
Python Set pop()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python String encode()
Python List clear()
Python Program to Add Two Matrices
Python Program to Convert Kilometers to Miles
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python range()
Python Program to Check Armstrong Number
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python List Comprehension
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Find the Factorial of a Number