Skip to content

Simplify Division

Sourcery refactoring id: simplify-division

Description:

Use Python's built-in feature for succinct division syntax.

Before:

result = int(42 / 10)

After:

result = 42 // 10

Before:

result = 42 // 10
remainder = 42 % 10

After:

result, remainder = divmod(42, 10)

Explanation:

Python has some great features to simplify division expressions. If you're interested only in the whole number component of the quotient, you can use the // integer division operator. If you want to have the whole number component and the remainder in separate variables, the built-in divmod function comes handy.

Back to top