Thứ Ba, Tháng Hai 11, 2025
spot_img
Homedivmod() function in Python - QuanTriMang.com

divmod() function in Python – QuanTriMang.com

Jaw divmod() in Python returns a tuple containing the quotient and remainder when parameter 1 (the dividend) is divided by parameter 2 (the divisor).

Syntax of divmod() function in Python

divmod(x, y)

Parameters of the divmod() function

divmod() has two parameters:

  • x: numerator (divider)
  • y: denominator (divider)

x, y not complex numbers.

Return value from divmod()

  • Jaw divmod() returns a tuple (q,r)with q is the quotient and r is the balance of (x,y).
  • If x and y is an integer, the return value from divmod() will be (a // b, x% y).
  • If x or y To be float as a result (q, x% y). Here, q is the entire quotient.

For example: The divmod() function works in Python

print('divmod(8, 3) = ', divmod(8, 3))
print('divmod(3, 8) = ', divmod(3, 8))
print('divmod(5, 5) = ', divmod(5, 5))

# code by Quantrimang.com
# divmod() với Float

print('divmod(8.0, 3) = ', divmod(8.0, 3))
print('divmod(3, 8.0) = ', divmod(3, 8.0))
print('divmod(7.5, 2.5) = ', divmod(7.5, 2.5))
print('divmod(2.6, 0.5) = ', divmod(2.6, 0.5))

Run the program, the returned results are:

divmod(8, 3) = (2, 2)
divmod(3, 8) = (0, 3)
divmod(5, 5) = (1, 0)
divmod(8.0, 3) = (2.0, 2.0)
divmod(3, 8.0) = (0.0, 3.0)
divmod(7.5, 2.5) = (3.0, 0.0)
divmod(2.6, 0.5) = (5.0, 0.10000000000000009)

Previous lesson: The dir() function in Python

Next lesson: Enumerate() function in Python

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments