Get quotient and remainder with divmod() in Python
In Python, you can calculate the quotient with
// and the remainder with
q = 10 // 3 mod = 10 % 3 print(q, mod) # 3 1
The built-in function
divmod() is useful when you want both the quotient and the remainder.
divmod(a, b) returns a tuple
(a // b, a % b).
Each can be assigned to a variable using unpacking.
- Related: Unpack a tuple / list in Python
q, mod = divmod(10, 3) print(q, mod) # 3 1
Of course, you can receive it as a tuple.
answer = divmod(10, 3) print(answer) print(answer, answer) # (3, 1) # 3 1