AppDividend
Latest Code Tutorials

# Python Division: Guide on Division operators

The division is a standard mathematical operation in any programming language, and Python is no exception. However, in Python 2, there is only one kind of division called integer division.

In general, the python definition of division(/) depended solely on the arguments. For example, in python 2.7, dividing 11/4 was 2 because both arguments were integers.

However, 20.0/7 will generate 2.857142857142857 as output because the arguments were floating-point numbers.

The above definition of ‘/’ often caused problems for applications where data types were used that the author hadn’t expected.

## Python Division

Python 3 has two kinds of division.

1. Integer Division( // )
2. Float Division( / )

The standard division symbol (/) operates differently in Python 3 and Python 2 when applied to integers. For example, when dividing an integer by another integer in Python 3, the division operation x / y represents a true division (uses __truediv__ method) and produces a floating-point result.

Meanwhile, the same operation in Python 2 represents a classic division that rounds the result down toward negative infinity (also known as taking the floor).

## Python float division

Python 3 provides ‘/’ operator that does floating-point division for both int and float arguments.

`print(42 / 2)`

#### Output

`21.0`

You can see that the output is in the floating-point. This is because, during the time of Python 2, when you divided one integer by another integer, no matter what, the result would always be an integer.

Now, let’s see the following code.

`print(41 / 2)`

#### Output

`20.5`

Now, we have divided 2 with an odd number, so we got the floating-point value.

### Python 2 division

In Python 2.2 or later, in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.

To clarify for the Python 2.x line, / is neither floor division nor true division. The currently accepted answer is not clear on this. The / is floor division when both args are int, but is true division when either or both of the args are float.

```from operator import truediv, floordiv
print(truediv(10, 8) == 1.25)           # equivalent to `/` in Python 3
print(floordiv(10, 8) == 1)
```

```True
True```

## Python integer division

Since Python doesn’t declare data types in advance, you never know when you want to use integers and when you want to use the float. Furthermore, since floats lose precision, it’s not advised to use them in integral calculations.

To solve this problem, future Python modules included a new type of division called integer division given by the floor division operator (//).

Now, / performs float division and // performs integer division.

In Python 3, you can perform integer division using (//) operator.

`print(42 // 2)`

#### Output

`21`

You can see that the returned value is an integer and not float.

Now, let’s divide odd value with 2 and see the output.

```print(41 // 2)
```

#### Output

`20`

Here, you can see that it rounds off to 20. This is because float division rounds down to the nearest integer.

## Python division with negative values

Some other programming languages use rounding toward zero (truncation) rather than rounding down toward negative infinity as Python does (i.e., in those languages -3 / 2 == -1). This behavior may create confusion when porting or comparing code.

```print(-3 // 2)
```

#### Output

`-2`

If we try float division, then you will see the different results.

```print(-3 / 2)
```

`-1.5`

## Conclusion

If you want a floating-point number in your division result, then you can use float division ( / ), or if you wish to integer-based division, then you can use ( // ) operator in Python. You have to take care of data type conversion in the long program to avoid any error or unexpected behavior.