Angelica Fox
last year
Angelicacountry asked

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

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hello Angelica, really nice question.

In Python, both / and // do division, but they don’t give you the same kind of result.

  • / is normal division
    It always gives you a float (a number with a decimal), even when the result is a “whole” value.

    8 / 2   # 4.0
    9 / 2   # 4.5
    
  • // is floor division
    It divides, then rounds down to the nearest whole number, dropping anything after the decimal.

    8 // 2  # 4
    9 // 2  # 4
    

One extra detail that can surprise people: “round down” means toward negative infinity, so with negative numbers:

-9 // 2   # -5   (because -4.5 rounds down to -5)

So the short version is:

  • Use / when you want a regular division result (with decimals).

  • Use // when you want an integer-like result, rounded down.

If you have further questions, I'm here to help.

Python
This question was asked as part of the Getting started with Python course.