We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

Python / vs //: Floor Division Explained

Lane Wagner
Lane WagnerBoot.dev co-founder and backend engineer

Last published

Table of Contents

Python has two division operators: / performs regular division, while // performs floor division.

All the content from our Boot.dev courses are available for free here on the blog. This one is from the "Computing" chapter of Learn Python for Beginners. If you want to try the far more immersive version of the course, do check it out!

Integers, Floats, and Division

Numbers without a decimal point are integers — whole numbers, positive or negative. Numbers with a decimal point are floats.

my_int = 5
my_float = 5.5

Basic arithmetic works the way you'd expect:

2 + 1   # 3 (addition)
2 - 1   # 1 (subtraction)
2 * 2   # 4 (multiplication)
3 / 2   # 1.5 (division)

One gotcha: division with / always returns a float, even if both operands are integers. 6 / 3 gives you 2.0, not 2.

What Is Floor Division in Python?

Click to play video

Floor division is like normal division except the result is rounded down to the nearest integer. Use the // operator:

7 // 3    # 2 (rounded down from 2.333)
-7 // 3   # -3 (rounded down from -2.333)

Note that "rounded down" means toward negative infinity, not toward zero. That's why -7 // 3 is -3, not -2.

Use / when you need the ordinary quotient. Use // when you deliberately need the quotient rounded down.

Frequently Asked Questions

What is the difference between / and // in Python?

The / operator performs regular division and always returns a float. The // operator performs floor division, rounding the quotient down to the nearest integer.

Why does -7 // 3 equal -3 in Python?

Floor division rounds toward negative infinity, not toward zero. The quotient is about -2.333, so flooring it produces -3.

Does regular division always return a float in Python?

Yes. The / operator returns a float even when both operands are integers and divide evenly.

Does floor division round toward zero?

No. It rounds down toward negative infinity, which is why -7 // 3 equals -3 rather than -2.

Related Articles