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:
1 | 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?
1 2 3 4 5 6 7 8 9 | 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
1 2 3 4 5 6 7 | 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 Check If a String Is a Number (Float)
Python Dictionary values()
Python Input, Output and Import
Python exec()
Python Program to Print Hello world!
Python String maketrans()
Python Data Types
Python String rstrip()
Python Shallow Copy and Deep Copy
Python Program to Print the Fibonacci sequence
Python Program to Convert Bytes to a String
Deep Learning with Python - Francois Chollet
Python Set difference_update()
Python String zfill()
Python Program to Convert Decimal to Binary Using Recursion
Python del Statement
Python Program to Find the Largest Among Three Numbers
Python Dictionary popitem()
Python enumerate()
Python min()
Python Program to Check the File Size
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python if...else Statement
Python Program to Print Colored Text to the Terminal
Python open()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Illustrate Different Set Operations
Python Program to Find HCF or GCD
Python Dictionary copy()
Python Program to Get the Full Path of the Current Working Directory
Python Tuple index()
Python String split()