The formula for taking the remainder:
Remainder = divisor-dividend * quotient
Python's remainder is calculated according to the quotient obtained by dividing (rounding down).
The remaining problems are mainly divided into:
Positive and positive, negative and negative, positive and negative, 0
Positive and positive numbers
# Big number/Decimal: Because the quotient obtained is the same as that obtained by dividing, directly follow this formula (remainder=divisor-被divisor*Quotient).
print(9//7) #1print(9%7) #2
# Decimal/Large number: Because the quotient obtained is the same as the one obtained by dividing, directly follow this formula (remainder=divisor-被divisor*Quotient).
# It can also be said here: as long as the positive and positive numbers are decimals/For large numbers, the quotient is 0, so the remainder is itself.
print(7//9) #0print(7%9) #7
Negative and negative numbers
# Decimal/Large number: Because the quotient obtained is the same as the one obtained by dividing, directly follow this formula (remainder=divisor-被divisor*Quotient).
# Because negative negative makes positive, so the divisible result is 9//The value of 7 is 1, so the remainder is-9-(-7)=-2print(-9//-7) #1print(-9%-7) #-2
# Big number/Decimal: Because the quotient obtained is the same as that obtained by dividing, directly follow this formula (remainder=divisor-被divisor*Quotient).
# It can also be said here: as long as it is a large number in the division of a negative number and a negative number/For decimals, the quotient is 0, so the remainder is itself.
print(-7//-9) #0print(-7%-9) #-7
0
# Since 0 cannot be the dividend, the quotient and remainder of 0 divided by any number (except 0) are all 0.
print(0%-1) #0print(0%1) #0
Positive and negative numbers
# negative number/A positive number
#- 7 /9=-0.77..So the divisible value is-1, which is the value rounded down, and the remainder in python is calculated according to the quotient of the division, so the remainder is-7-(9*(-1))=2print(-7//9) #-1 print(-7%9) #2
# same-9/7=-1.285..So the divisible value is-2. The remainder-9-7*(-2)=5print(-9//7) #-2print(-9%7) #5
# A positive number/negative number
# The same 7/-9=--0.77..So the divisible value is-1, remainder 7-【-9*(-1)】=-2print(7//-9) #-1print(7%-9) #-2
# The same 9/-7=-1.285..So the divisible value is-2. The remainder is 9-【-7*(-2)】=-5print(9//-7) #-2print(9%-7) #-5
**Supplementary knowledge: python slicing and negative subscript **
[ a:b:c] represents the number set of gradually increasing c in [a,b) interval
Example: List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[1:18:3] It means that the numbers from 1 to 17 start from 1 and increase by 3 one by one [1, 4, 7, 10, 13, 16]
Negative subscript articles
List (length 10) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Ordinary subscript | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Negative subscript | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
The above detailed explanation based on the python remaining problem (%) is all the content shared by the editor, I hope to give you a reference.
Recommended Posts