In the module "Arithmetic Expressions" we talked about integer division operations.
Recall them again.
// - integer division, when the fractional part is dropped as a result of the division operation
% - calculation of the remainder of the division
The operation of calculating the remainder for negative numbers in Python is slightly different than in other programming languages, such as C ++ or Pascal
In Python, the operation of calculating the remainder is performed according to mathematical rules, that is, as is commonly believed in Number Theory, the remainder is a non-negative number.
Example
c = 10 // 3 # с = 3
d = 10 % 3 # d = 1
e = -7 // 4 # e = -2
f = -7 % 4 # f = 1
The values of the variables e and f turned out so, because
-7 = (-2*4)+1
REMEMBER!
In Python, the operation of calculating the remainder for negative numbers is performed according to mathematical rules, i.e. -7% 4 = 1
Integer operations are very important in programming. They need to be understood and used correctly. And this requires practice!