Python learning notes (4) Numbers (2) and python learning notes
Python
Division in Python2
1 >>> 3 / 6 2 0 3 >>> 3.0 / 6 4 0.5 5 >>> 3.0 / 6.0 6 0.5 7 >>> 5 / 2 8 2 9 >>> 6.0 / 310 2.011 >>> 10.0 / 312 3.333333333333333513 >>> 0.2 +0.914 1.115 >>> 0.2 +0.1 16 0.30000000000000004
Because the computer will convert the input decimal to binary. When the floating point number is converted to binary, the problem will arise: 0.1 is converted to binary, 0.000110011 is not exactly equal to the decimal 0.1 after conversion, generally, we round the result to two places for calculation.
In Python2, the Division is used to take the integer part, so 3/6 =/2 = 2 is obtained.
The from _ future _ import division module can be referenced.
1 >>> from __future__ import division2 >>> 3 / 63 0.54 >>>
Remainder in Python
Two Methods of Remainder
1.%
2. divmod () to obtain the quotient and remainder
1 >>> 6% 52 13 >>> 6% 44 25 >>> divmod (6, 4) 6 (1, 2) quotient and remainder 7 >>> divmod (6, 5) 8 (1, 1)