Sitelet https://www.askpython.com/python/built-in-methods/division-operator-differences-in-python

What Is the Difference Between / and // in Python

When dividing numbers in Python, / and // can give different results. The / operator returns the quotient, while // rounds it down to the greatest whole number no greater than the quotient.

A Python example compares their results and types, then the article explains what happens with negative numbers and division by zero.

TL;DR

In Python 3, / returns the quotient, while // returns the floor of that quotient.

  • Choose / when fractional precision matters.
  • Choose // when the calculation needs a floored quotient.
  • With negative values, floor moves toward negative infinity.

What do / and // mean in Python?

The / operator performs true division. With built-in integer operands, its result is a float, even when the quotient has no fractional part. That matters when a later function accepts only an integer or when a calculation expects a fractional value.

The // operator takes the floor of the quotient. A quotient is the result of division, and floor means the greatest integer less than or equal to that value.

ExpressionResultTypeWhat it shows
15 / 62.5floatUnfloored quotient
15 // 62intFloor of 2.5
15.0 // 62.0floatFloor division keeps a float result
-15 // 6-3intFloor of -2.5

I use the type table before choosing an operator because the returned type can affect the next expression.

With //, integer operands produce an integer result, while a float operand can leave the result as a float. Convert only where an integer is required, since conversion can change the value.

Compare / and // with a Python example

I ran both operators with the same positive operands, then checked a negative quotient and its paired remainder.

a, b = 15, 6
print("15 / 6 =", a / b, type(a / b).__name__)
print("15 // 6 =", a // b, type(a // b).__name__)
print("-15 // 6 =", -15 // b)
print("divmod(-15, 6) =", divmod(-15, b))

Run the source file with this command:

python3 division_demo.py

The output is:

15 / 6 = 2.5 float
15 // 6 = 2 int
-15 // 6 = -3
divmod(-15, 6) = (-3, 3)
Terminal output comparing Python true division and floor division
The terminal shows the quotient values, result types and negative floor-division result.

The first two output lines show the quotient values and their types. The negative case returns -3, and divmod pairs it with remainder 3, satisfying -15 = (-3 × 6) + 3.

The positive example is useful for a first check because 15 divides by 6 into 2 with a remainder of 3. True division keeps that remainder as part of the value, producing 2.5. Floor division discards the fractional part by choosing the floor, which gives 2 for this positive quotient.

These operators are arithmetic operators, so each can appear inside a larger expression. Parentheses make the intended order clear when the quotient is combined with addition or multiplication. This is especially helpful when a floored result is used as an index or count, where an unexpected float may not be accepted.

The choice is about the value the calculation requires, not which spelling is shorter. For example, a ratio used as a rate needs the fractional value, while counting complete groups may call for a floor. Match the operator to the meaning of the quantity before passing it to another expression.

Edge cases: negative numbers, result types and zero

I compared int(-15 / 6), which returned -2, with -15 // 6, which returned -3.

CaseSymptomFix or choice
Negative quotientFloor gives -3 for -2.5, while truncation toward zero gives -2Use // for floor, not int(a / b)
Float operand with //The result can be a float such as 2.0Convert explicitly if a later operation requires an integer
Zero divisorPython raises ZeroDivisionError: division by zeroCheck that the divisor is nonzero first

On a positive quotient, int(15 / 6) and 15 // 6 both return 2, so the two approaches can look interchangeable. With the negative input, conversion returned -2 and floor division returned -3. Test negative values before replacing one with the other.

I checked zero as the divisor and got ZeroDivisionError: division by zero. Guard a value before division when it could be zero, rather than handling a failed calculation later.

For 17 divided into groups of 5, floor division gives 3 complete groups and the remainder is 2. I checked that divmod(17, 5) returned (3, 2), satisfying the quotient-and-remainder identity. This makes the pair useful when code needs both the group count and leftover amount.

PEP 238 documents Python 2’s optional true-division import: with it, / on integers produced true division, while // remained the floor operator.

During a port, inspect each integer division expression. Some callers expect an integer quotient, while others require the fractional result. Change the operator only when the intended value calls for it.

Conclusion

Choose the operator based on whether the calculation needs the quotient or its floor. Check operand types and negative values when the result feeds another calculation.

For the language rules, read the Python arithmetic expression reference and PEP 238. AskPython also covers floor division and Python operators.

FAQ

These answers address common questions about Python division.

What does // mean in Python?

The // operator performs floor division and returns the floor of the quotient.

Why does 5 // 2 return 2?

The floor of 2.5 is 2. For negative quotients, floor moves toward negative infinity.

Does / always return a float in Python?

With built-in integer operands in Python 3, / returns a float, including for whole-number quotients.

Tanishka Dhondge
Tanishka Dhondge
Articles: 59