How to Divide Two Integers in Python 2 and 3?

In this article, you’ll learn about the division operators // and / in Python 2 and 3. You can check out the version in your Python script as shown here.

A short visual overview of the division operator in Python 2 and 3:

Integer (floor) division vs float division in Python 2 and Python 3
Figure: The differences and similarities of the division operators in Python 2 and 3.

Assuming two integer values stored in variables a and b, there are four different cases depending on which Python version and division operator you use:

  • Python 2: The single front-slash operator a/b performs integer division.
  • Python 2: The double front-slash operator a//b performs integer division.
  • Python 3: The single front-slash operator a/b performs float division.
  • Python 3: The double front-slash operator a//b performs integer division.

Let’s dive deeper into these cases with some examples next!

How to Divide Two Integers in Python 2?

For Python 2, dividing two integers uses integer division. This is also known as “floor division” because it applies the floor function after division. For example, 7/2 in Python 2.x would result in the value 3. However, using “/” is deprecated — to perform floor division, use “//” that is available in Python 2.2 and later versions.

# Python 2.x
print(7/2)

Output:

3

Float Division in Python 2

To perform float division in Python 2, use the from __future__ import division statement and use the single front-slash a/b to perform float division as in Python 3. For example, 7/2 will now result in 3.5.

from __future__ import division
print(7/2)
# 3.5

Alternatively, you can multiply with the float 1.0 to “infect” the numerator so that one of the operands of the division operator is a float value and the whole division becomes a float division as well:

result = 1.0 * 7 / 2
print(result)
# 3.5

Alternatively, you can use the float() built-in function on the numerator or denominator to perform float division.

result = float(7) / 2
print(result)
# 3.5

result = 7 / float(2)
print(result)
# 3.5

How to Divide Two Integers in Python 3?

For Python 3, dividing two integers using normal float division. For example, 7/2 in Python 3.x would result in the floating point value 3.5.

print(7/2)

Output:

3.5

You can read more discussions about this here.

Summary

A very important lesson in Python to learn from the beginning is β€œDivision in Python”. What it means to divide in Python, and the different uses of Python arguments or operators. Many people do not learn these subtle differences. When they do not learn, it costs them hours and days of debugging programs.

You can use the division in two different ways:

  • Integer division takes two numbers and divides them to give a result of a whole number. In Python 3, integer division (or floor division) uses the double front-slash // operator. In Python 2, integer division uses the single front-slash / operator.
  • Float division takes two numbers and divides them and results in a decimal value. In Python 3, the use of regular division uses the single front-slash / operator. This operator will result in a decimal value.

You can learn more about division in Python here.