a=29
b=5
my_tuple=divmod(a,b)
print(my_tuple) # (5,4)
The first element of the output is same as output of Floor division and second element is same as output of Modulus.
print(a//b) # 5
print(a%b) # 4
a=53.67
b=7.2
print(divmod(a,b))
Output
(7.0, 3.2700000000000005)
The output is always tuple type.
a=54.6
b=4
print(divmod(a,b))
print(type(divmod(a,b)))
Output
(13.0, 2.6000000000000014)
Here is the data type of the element of the output tuple
a=29
b=5
my_tuple=divmod(a,b)
print(my_tuple) # (5,4)
print(type(my_tuple[1])) # <class 'int'>
print(divmod(-20, 3)) # Output: (-7, 1)
for i in range(1, 6):
q, r = divmod(20, i)
print(f"Dividing 20 by {i}: Quotient = {q}, Remainder = {r}")
Output
Dividing 20 by 1: Quotient = 20, Remainder = 0
Dividing 20 by 2: Quotient = 10, Remainder = 0
Dividing 20 by 3: Quotient = 6, Remainder = 2
Dividing 20 by 4: Quotient = 5, Remainder = 0
Dividing 20 by 5: Quotient = 4, Remainder = 0
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.