Home >Backend Development >Python Tutorial >How to calculate the remainder after dividing two numbers in python

How to calculate the remainder after dividing two numbers in python

尚
Original
2019-06-28 11:04:1420984browse

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:

How to calculate the remainder after dividing two numbers in python

##2. divmod() function


The divmod() function in python combines the results of the divisor and remainder operations and returns a tuple containing the quotient and remainder (a // b, a % b).

Plural numbers are not allowed to be processed before python2.3.

Function syntax:


divmod(a, b)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn