Python divmod() built-in function

From the Python 3 documentation

Take two (non-complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators apply.

Introduction

The divmod() function takes two numbers as arguments and returns a tuple containing the quotient and the remainder of their integer division. It’s a convenient way to get both results in a single call.

Examples

# Get quotient and remainder
quotient, remainder = divmod(10, 3)
print(quotient)   # Output: 3
print(remainder)  # Output: 1
print(divmod(2, 2))   # Output: (1, 0)
print(divmod(10, 2))  # Output: (5, 0)
print(divmod(7, 2))   # Output: (3, 1)

Subscribe to pythoncheatsheet.org

Join 16,702+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.