In earlier versions, Python division is a small algorithm skill.
The first thing to talk about is the division operation in python. in python 2.5, there are two Division operations, namely the true division and the floor division. When division is performed in the form of x/y, if both x and y are integers, the result is truncated and the integer part of the operation is obtained, for example, the calculation result of 2/3 is 0. If one of x and y is a floating point number, the so-called true division will be implemented. For example, the result of 2.0/3 is 0.66666666666666663. Another division is in the form of x // y. Here we use the so-called floor Division to obtain the maximum integer not greater than the result. This operation is irrelevant to the operand. For example, the result of 2 // 3 is 0, the result of-2 // 3 is-1, and the result of-2.0 // 3 is-1.0.
In the future python 3.0, x/y will only execute the true division, regardless of the operand; x // y will execute the floor division. If you need to use python 2.5 in this way, you need to add the declaration of from _ future _ import division before the code. For example:
Copy codeThe Code is as follows:
From _ future _ import division
A = 2/3
From _ future _ import division a = 2/3
The result of variable a is 0.66666666666666663 instead of 3.