Division in Python

In the C / C + + language, the division of integer number will be carried out (rounding off the decimal part). For example, int a = 15 / 10; the result of a is 1.

The same is true in Java, so when dividing two int data and returning a floating-point data, you need to force type conversion. For example, float a = (float) BGC, where B and C are int data.

Python is divided into three kinds of division: traditional division, precise division and floor division.

Traditional division

If it is an integer division, perform the floor division, if it is a floating-point division, perform the precise division.

>>>1/2
0
>>>1.0/2.0
0.5

Precise division

Division always returns the real quotient, whether the operands are integer or floating-point. Execute from__ future__ The import division directive can do this.

>>>from __future__ import division
>>>1/2
0.5
>>>1.0/2.0
0.5

Floor removal

Starting from Python 2.2, an operator / / is added to perform floor Division: / / division. Regardless of the numeric type of the operands, the decimal part is always discarded and the nearest number in the number sequence smaller than the real quotient is returned.

>>>1//2
0
>>>1.0//2
0
>>>-1//2.0
-1

Built in function divmod()
divmod (a, b), return (A / / B, a% B)

>>>divmod(1,2)
(0,1)
>>>divmod(3.14159,1.5)
(2.0,0.4159000000000002)
>>>5+6j//3+2j
2+0j
>>>5+6j%3+2j
-1+2j
>>>divmod(5+6j,3+2j)
((2+0j),(-1+2j))

=

Read More: