python operators - arithmetic operators

Addition, subtraction, multiplication and division are relatively simple. I won't go into details here, print(2 +-*/ 3), the only thing to pay attention to is the integer division operation, the symbol is "//", and the integer division operation takes the integer part, not rounding!

print(5 / 2) The result of this run is 2, not 3.

 

Exponentiation operator, exponentiation operation, the operator is "**"

print(5 ** 5)

The modulo operation is actually the remainder (remainder) operation, the operation symbol is "%", and the result of the following code operation is 1.

print(5 % 4)

 

 

In fact, the division // and remainder % can be applied to many scenarios, such as the following nine-square grid, find out which row and column 6 is in

We can use this code

num = 6 
row = num // 4
col = num % 4
The above is to treat the first row and first column as zero row and zero column. If you want to change to one row and one column, you only need to add 1 to "4".


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324534523&siteId=291194637