What is divmod in python

爱喝马黛茶的安东尼
Release: 2019-06-21 15:23:29
Original
27669 people have browsed it

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"

What is divmod in python

1. In the tuple returned by the integer parameter

>>> divmod(9, 5)
(1, 4)
>>> type(divmod(9, 5))
<class &#39;tuple&#39;>
Copy after login

, 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
Copy after login

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(&#39;a&#39;,&#39;A&#39;)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for divmod(): &#39;str&#39; and &#39;str&#39;
Copy after login

2. The return value type of the divmod function is a tuple

>>> type(divmod(10,5))
<class &#39;tuple&#39;>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template