4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

What Does // Mean in Python?

By 4Geeks

Floor division divides and rounds down, also with negative numbers and floats. See it work, then use it in real code.

6 min readLast updated
Python floor division icon

What does // mean in Python?

Short answer: // is the floor division operator. It divides two numbers and rounds the result down to the nearest whole number: 19 // 5 returns 3, while 19 / 5 returns 3.8. "Down" means toward negative infinity, so -19 // 5 returns -4, not -3. With two integers the result is an int; if either number is a float, the result is a float, such as 10 // 2.5, which returns 4.0.

// is the partner of %: // gives you how many whole times one number fits into another, and % gives you what is left over. Python guarantees that (a // b) * b + a % b == a for any numbers.

/ keeps the decimals. // moves to the whole number on the left of the result on the number line, also with negative numbers.

Floor division at a glance

ExpressionResultWhy
19 / 53.8Regular division always returns a float
19 // 533.8 rounded down
19 % 54The remainder: 3 × 5 + 4 = 19
-19 // 5-4-3.8 rounded down is -4
-19 % 51-4 × 5 + 1 = -19
10 // 2.54.0A float in the operation gives a float
divmod(19, 5)(3, 4)// and % in one call
x //= 5x becomes x // 5Augmented assignment
7 // 0ZeroDivisionErrorYou cannot divide by zero

// vs / in Python

In Python 3, / is true division: it always returns a float, even when the division is exact. // throws the decimals away by rounding down.

python
print(19 / 5)   # 3.8
print(19 // 5)  # 3
print(20 / 5)   # 4.0  (a float, even if it is exact)
print(20 // 5)  # 4    (an int)

If you are reading old Python 2 code, be careful: there, / between two integers already did floor division. Python 3 separated the two operations, so // is the one to use whenever you want a whole number.

Floor division with negative numbers

This is the part that surprises people. Rounding "down" means moving toward negative infinity, not toward zero:

python
print(19 // 5)    # 3   (3.8 rounded down)
print(-19 // 5)   # -4  (-3.8 rounded down)
print(19 // -5)   # -4
print(-19 // -5)  # 3

If you want to cut the decimals toward zero instead, use int() or math.trunc():

python
import math
 
print(int(-19 / 5))         # -3
print(math.trunc(-19 / 5))  # -3
print(math.floor(-19 / 5))  # -4, the same as -19 // 5

Floor division with floats

When either number is a float, // still rounds down, but the result is a float:

python
print(10 // 2.5)    # 4.0
print(17.5 // 3.3)  # 5.0
print(7.5 // 2)     # 3.0
print(-7.5 // 2)    # -4.0

Wrap it in int() when you need an integer type, for example to use the result as a list index: int(17.5 // 3.3) returns 5.

// and % together: divmod()

divmod(a, b) returns both results as a tuple, which is handy for unit conversions:

python
total_seconds = 125
minutes, seconds = divmod(total_seconds, 60)
print(minutes, seconds)  # 2 5

With negative numbers, % takes the sign of the divisor, so -19 % 5 is 1 and divmod(-19, 5) is (-4, 1). That is what keeps (a // b) * b + a % b == a true.

Career · AI Engineering

You know what // does, now build something with it

From zero to junior AI Engineer: Python, AI agents and RAG with mentors and real projects.

Use floor division with AI

An AI assistant such as ChatGPT, Claude or GitHub Copilot can explain an unexpected result or adapt a formula to your case. Give it the expression, the values and the result you expected. These three prompts are ready to copy.

1. Explain an unexpected result

In Python, [-19 // 5] returns [-4] but I expected [-3].
Explain step by step why, what "rounding toward negative infinity" means,
and show me how to get the result I expected.

2. Write a formula with // and %

I need a Python function that [splits 3,725 seconds into hours, minutes and seconds].
Use // and % or divmod(), explain each step,
and add three asserts that test normal values, zero and a large number.

3. Review my code for division bugs

Review this Python code and find any place where / should be //, or the other way round,
and any division that can fail with zero or negative numbers:
[paste your code]

Run the code the AI gives you with negative numbers and zero before you use it.

When to use // in real code

Pages, batches and groups. How many full groups fit, and how many pages you need in total:

python
items, per_page = 47, 10
full_pages = items // per_page              # 4
pages = (items + per_page - 1) // per_page  # 5, rounds up

-(-items // per_page) is another common way to round up with integers.

The middle of a list. Indexes must be integers, so binary search uses //:

python
low, high = 0, 9
middle = (low + high) // 2  # 4

Digits of a number. % 10 takes the last digit and //= 10 removes it:

python
n = 1234
digits = []
while n:
    digits.append(n % 10)
    n //= 10
print(digits)  # [4, 3, 2, 1]

Very large integers. // between integers is exact at any size, while / goes through a float and loses precision:

python
print(10**20 // 3)      # 33333333333333333333
print(int(10**20 / 3))  # 33333333333333331968

// with Decimal and Fraction

Fraction follows the same rule as integers, but Decimal cuts toward zero:

python
from decimal import Decimal
from fractions import Fraction
 
print(Fraction(-19, 1) // 5)       # -4
print(Decimal(-19) // Decimal(5))  # -3

If you mix both types in a program, test the negative cases.

Learn with a mentor

Master Python operators, then build AI with them

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common errors with // in Python

What you seeWhy it happensFix
-7 // 2 returns -4, not -3// rounds toward negative infinityUse int(-7 / 2) or math.trunc() to cut toward zero
You get 4.0 instead of 4One of the numbers is a floatWrap the result in int()
TypeError: list indices must be integers or slices, not floatYou used / to calculate an indexUse //
ZeroDivisionErrorThe divisor is 0Check the divisor before dividing
SyntaxError: invalid syntax on a line that starts with //// is not a comment in PythonUse # for comments
NameError after x = 10 // notePython reads it as 10 divided by a variable called noteUse # for comments
Free download

Floor division explained in a 2-page cheat sheet

What // returns with integers, floats and negative numbers, how it works with % and divmod(), recipes for real code, the most common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

Python floor division (//) cheat sheet

2 pages · PDF · 4Geeks

  • 12 expressions with their results, negatives included
  • Recipes for pages, indexes, digits and time
  • The 5 most common errors and prompts to fix them with AI

Get it free

Your PDF right away

Frequently asked questions about // in Python

Want to go from Python basics to building AI agents, with real projects and mentors?

See the AI Engineering program