Skip to content

Python Division: Integer Division and Float Division

python division cover image

In this post, you’ll learn Python 3 division, as well as some of its unexpected quirks. You’ll learn how to use both integer and floor methods, as well as how to interpret some of its less expected results.

Python Division – The different ways

Python has two different division operators, / and //. Which one you use depends on the result that you want to achieve.

  • The single forward slash / operator is known as float division, which returns a floating point value.
  • On the other hand, the double forward slash // operator returns a floored value, specifically either a floored integer or floating point value.

Python Floor Division

Python floor division, using the // operator, works by returning the floored value of its quotient. This works different than integer division which would round the number. Instead, this works by returning the floor value.

Let’s see how this works in action, by looking at a few examples:

# Two float values
>> 7.0 // 2.0
3.0

# A single float value
>> 7 // 2.0
3.0

# Two integers
>>  7 // 2
3

One interesting note about this is what happens with negative numbers:

>> -7.0 // 3
-3.0

Logically, this makes sense. The result will be rounded down (i.e., floored), meaning that while we may expect it be equal to -2.0, rounded down, the value is correctly -3.0.

Python Float Division

Python float division uses the / operator and returns, well, a floating point value. This, perhaps, is more of how you’d expect division to work. Let’s look at a few more examples:

# Two integers
>> 7 / 3
2.33

# One floating point value
>> 7.0 / 3
2.33

# Two floating point values
>> 7.0 / 3.0
2.33

As you can see, the results return values you’d expect, regardless of whether you’re dividing integers, floats, or a mix of both.

Conclusion

Dividing in Python offers different ways to, well, divide numbers. Having a firm understanding of these operators makes you a much better programmer by having a solid understanding of the basics. To learn more about these, check out the official documentation.

Check out some other Python tutorials here.

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Tags:

3 thoughts on “Python Division: Integer Division and Float Division”

  1. Pingback: Python Floor: Rounding Down with Python • datagy

  2. Candace Sommer-Van Auken

    Your example with negative numbers shows a different result in the code, (where it’s shown as returning -4) as in your explanation (where it’s shown as returning -3). I believe that -3 is correct. You might want to correct this.

Leave a Reply

Your email address will not be published. Required fields are marked *