Home >Backend Development >Python Tutorial >What is divmod in python
What is divmod in python? The following will bring you a relevant introduction to divmod.
The divmod function is Python's built-in function. It can combine the results of the divisor and remainder operations and return a tuple containing the quotient and remainder (a // b, a % b).
Grammar
divmod(dividend, divisor)
Related recommendations: "Python Video Tutorial"
1. In the tuple returned by the integer parameter
>>> divmod(9, 5) (1, 4) >>> type(divmod(9, 5)) <class 'tuple'>
, the first element is the result of 9//5, and the second element is the result of 9 % 5.
2. Floating point parameter
>>> divmod(2.3, 0.2) (11.0, 0.0999999999999997) >>> a, b = divmod(2.3, 0.2) >>> a 11.0 >>> b 0.0999999999999997
can separate the integer division result and remainder through tuple unpacking.
Notes
1. Parameters cannot handle strings
The divmod function can only accept parameters of integer or floating point type. For example, when the parameter is a string, Python reports an error.
>>> divmod('a','A') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for divmod(): 'str' and 'str'
2. The return value type of the divmod function is a tuple
>>> type(divmod(10,5)) <class 'tuple'>
The above is the detailed content of What is divmod in python. For more information, please follow other related articles on the PHP Chinese website!