Python divmod() with Examples

Python divmod() function is used to perform division on two input numbers. The numbers should be non-complex and can be written in any format such as decimal, binary, hexadecimal etc.

Python divmod()

Python divmod() syntax is:

The output is a tuple consisting of their quotient and remainder when using integer division.

For integer arguments, the result is the same as (a // b, a % b).

For floating point numbers the result is (q, a % b), where q is usually (math.floor(a / b), a % b). In any case q * b + a % b is very close to a.

If a % b is non-zero it has the same sign as b, and 0 <= abs(a % b) < abs(b).

Python divmod() example with integers

Output:

Python divmod() example with floats

Output:

Notice that in case of floating points, the q * b + a % b is very close to a in some cases.

divmod() with complex numbers

If we pass complex numbers as argument, we will get TypeError.

Output: TypeError: can't take floor or mod of complex number.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: