Using divmod() built in Python function

preview_player
Показать описание
In this video we will explore built in Python function called divmod() and its usage.
The divmod() method takes two parameters x and y, where x is treated as numerator and y is treated as the denominator. The method calculates both x // y and x % y and returns both the values.

- If x and y are integers, the return value is:
(x // y, x % y)
- If x or y is a float, the result is:
(q, x % y), where q is the whole part of the quotient.

If we write in editor next code:
# divmod() with int
print('(5, 4) = ', divmod(5, 4))
print('(10, 16) = ', divmod(10, 16))
print('(11, 11) = ', divmod(11, 11))
print('(15, 13) = ', divmod(15, 13))

# divmod() with int and Floats
print('(8.0, 3) = ', divmod(8.0, 3))
print('(3, 8.0) = ', divmod(3, 8.0))
print('(7.5, 2.5) = ', divmod(7.5, 2.5))
print('(2.6, 0.5) = ', divmod(2.6, 0.5))

Output will follow:
(5, 4) = (1, 1)
(10, 16) = (0, 10)
(11, 11) = (1, 0)
(15, 13) = (1, 2)

(8.0, 3) = (2.0, 2.0)
(3, 8.0) = (0.0, 3.0)
(7.5, 2.5) = (3.0, 0.0)
(2.6, 0.5) = (5.0, 0.10000000000000009)

If you like the video, like and subscribe to my channel!
Have a nice day!
Рекомендации по теме