Detailed explanation of the remaining problem based on python (%)

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

Detailed explanation of the remaining problem based on python (%)
Detailed explanation of data types based on Python
Detailed explanation of the principle of Python super() method
Detailed explanation of the usage of Python decimal module
Detailed explanation of the attribute access process of Python objects
Detailed explanation of python backtracking template
Detailed explanation of python sequence types
Detailed explanation of -u parameter of python command
Detailed explanation of Python guessing algorithm problems
Detailed explanation of the use of pip in Python | summary of third-party library installation
Solve the problem of installing VMwareTools on Ubuntu 18.04
Detailed explanation of python standard library OS module
Solve the problem of python running startup error
Detailed explanation of how python supports concurrent methods
Diagram of using Python process based on FME
Detailed explanation of building Hadoop environment on CentOS 6.5
Solve the problem of installing Theano on Ubuntu19
Implementation of business card management system based on python
Detailed explanation of common tools for Python process control
Detailed explanation of Python web page parser usage examples
Completely solve the problem of slow downloading of Python packages
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
Detailed implementation of Python plug-in mechanism
Detailed explanation of ubuntu using gpg2
Python handles the 4 wheels of Chinese
Python error handling assert detailed explanation
Python simulation of the landlord deal
What is the use of Python
Detailed explanation of the installation and use of SSH in the Ubuntu environment
Detailed usage of dictionary in Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
How to fix the problem of missing system settings on Ubuntu 14.04
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Install the latest Python 3.6 version on Ubuntu
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Draw personal footprint map based on Python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
What is the scope of python variables
Two days of learning the basics of Python
What is the id function of python
Python from attribute to property detailed explanation
Where is the pip path of python3
Centos 8.1.1911 solves the problem of yum reinstallation
The essence of Python language: Itertools library
Check matrix calculation results based on python
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development