Understand Python divmod() with Examples – Python Tutorial

By | January 30, 2022

In this tutorial, we will use some examples to show you understand and use python divmod() function correctly.

divmod() syntax

This function is defined as:

divmod(a, b)

It will receive integer or float to compute a//b and a%b.

Understand Python divmod() with Examples - Python Tutorial

How to use divmod() in python?

We will use some examples to show you how to use.

1.When a and b are integer

a = 10
b = 3

result = divmod(a, b)
print(result)

Run this code, we will get:

(3, 1)

2. When a and b are float

a = 11.2
b = 3.2

result = divmod(a, b)
print(result)

We will get:

(3.0, 1.5999999999999988)

3.When a is integer, b is float

a = 11
b = 3.2

result = divmod(a, b)
print(result)

We will get:

(3.0, 1.3999999999999995)

4.When a is float, b is integer

a = 11.1
b = 3

result = divmod(a, b)
print(result)

We will get:

(3.0, 2.0999999999999996)

From examples above, we can find: one of a or b is float, we will get two float numbers.

Leave a Reply