Python built-in functions (17) -- divmod, pythondivmod

Source: Internet
Author: User
Tags floor division integer division

Python built-in functions (17) -- divmod, pythondivmod

English document:

divmod(A,B)

Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. with mixed operand types, the rules for binary arithmetic operators apply. for integers, the result is the same(a // b, a % b). For floating point numbers the result is(q, a % b), WhereQIs usuallymath.floor(a / b)But may be 1 less than that. In any caseq * b + a % bIs very closeA, Ifa % bIs non-zero it has the same signB, And0 <= abs(a % b) < abs(b).

 

Note:

1. Accept two values (non-plural values), and return the operator obtained by the division of the two values, and a tuple consisting of the remainder.

2. If the parameters are all integers, the floor division is executed, which is equivalent(a//b,a%b)。

>>> divmod(5,2)(2, 1)>>> 5//22>>> 5%21

3. If the parameter is a floating point, it is equivalent (math.floor(a/b),a%b).

>> divmod(5.5,2)(2.0, 1.5)>>> math.floor(5.5/2)2>>> 5.5/22.75>>> math.floor(5.5/2.0)2>>> 5.5%21.5

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.