# The differences and similarities of division operations in Python 2.6 and Python 3.0 are discussed in detail

- 2020-05-30 20:32:00
- OfStack

There are two operators for division in Python: '/' and '//'; There are three types of division: traditional division, Floor division, and true division.

X/Y type:

Before or before Python 2.6, this operation will leave out the decimal part for integer operations and keep the decimal part for floating point operations; Becomes true division in Python 3.0 (any type will keep the decimal part, and even a divisible division will be expressed as a floating point number).

Sample code:

Results in Python 2.7:

```
>>> 3/2
1
>>> 3/2.0
1.5
>>> 4/2
2
>>> 4/2.0
2.0
```

Results in version 3.4 of Python:

```
>>> 3/2
1.5
>>> 3/2.0
1.5
>>> 4/2
2.0
>>> 4/2.0
2.0
```

X // Y type:

Floor division: the operation added in Python 2.2 is available in Python2.6 and Python3.0, regardless of the type of object to be operated on. This operation always omits the decimal part, leaving the smallest divisible integer part.

Sample code:

Results in Python 2.7:

```
>>> 3//2
1
>>> 3//2.0
1.0
>>> 4//2
2
>>> 4//2.0
2.0
```

Results in version 3.4 of Python (same as version 1 of version 2.7) :

summary

```
>>> 3//2
1
>>> 3//2.0
1.0
>>> 4//2
2
>>> 4//2.0
2.0
```

Speaking:

The & # 8226; In Python 2.6, '/' performs traditional division, truncated integer division if the operands are all integers (that is, only integer parts are retained for the result), otherwise floating-point division (resists); '//' performs Floor division, like Python 3.01, truncated division for integers, and floating-point division for floating-point Numbers.

The & # 8226; In Python 3.0, '/' always performs true division and returns a floating point result containing any remainder, regardless of the type of operand. '//' performs Floor division, intercepts the remainder and returns an integer for the integer operand, if any of the operands are floating point, returns a floating point.

-------------------------------------------------

Supplement:

Floor division: the effect is equivalent to floor function in math module:

math.floor (x) : returns an integer not greater than x

So when the operand is negative: the result will be rounded down.

```
>>> 5//3 #1.6666666666666667
1
>>> -5//3
-2
>>>
```

There are many other functions similar to floor(), such as trunc() :

```
>>> import math
>>> math.trunc(-1.6)
-1
>>> math.trunc(1.6)
1
```