Python Built in functions in Python divmod(x,y) pair of numbers as tuple consisting of quotient and remainder
x : Non complex input number ( numerator or divident )
y : Non complex input number ( denominator or divisor).
Returns tuple with quotient and remainder.
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
Using float
input numbers are float .
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'>
Example 2: Using divmod() with Negative Numbers
Handling negative values.
print(divmod(-20, 3)) # Output: (-7, 1)
Example 3: Applying divmod() in a Loop
Using `divmod()` in a loop to track quotient and remainder through each iteration.
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
Applications of divmod()
Time Calculations : Easily break down minutes into hours and minutes.
Data Processing : Quickly get both quotient and remainder in single operations.
Financial Calculations : Determine exact whole parts and remaining parts in inventory or budgeting.
« All Built in Functions in Python
bin() int()
float()
← Subscribe to our YouTube Channel here