In this article, you’ll learn about the division operators
/ 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:
Assuming two integer values stored in variables
b, there are four different cases depending on which Python version and division operator you use:
- Python 2: The single front-slash operator
a/bperforms integer division.
- Python 2: The double front-slash operator
a//bperforms integer division.
- Python 3: The single front-slash operator
a/bperforms float division.
- Python 3: The double front-slash operator
a//bperforms 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)
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
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
You can read more discussions about this here.
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
- 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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.