Home > Article > Backend Development > How to calculate the remainder after dividing two numbers in python
The remainder in Python can be calculated through the modulo operator % or through the divmod() function.
1. Modulo operator %:
The so-called modulo operation is to calculate the remainder after dividing two numbers, and the symbol is %. For example, a % b is the remainder of dividing a by b. To describe it in mathematical language, if there are integers n and m, among which 0
The modulo operation Both operands must be integers and can be negative integers, but b cannot be 0 because the dividend cannot be 0.
When there are negative integers in a and b, |a|%|b|=c is first calculated, and then the sign of a%b is consistent with b. That is to say, if b>0, then a%b=c; if b
Example:
Example:
>>>divmod(7, 2) (3, 1) >>> divmod(8, 2) (4, 0)For more Python related technical articles, please visit
Python tutorial column for learning!
The above is the detailed content of How to calculate the remainder after dividing two numbers in python. For more information, please follow other related articles on the PHP Chinese website!