
// in Python: Floor Division Explained with Examples
Python’s floor division operator (//) discards fractional remainders differently than true division (/). The operation uses mathematical floor, which rounds toward negative infinity—a behavior that catches many developers off guard when negative numbers enter the picture. This guide walks through exactly how // behaves, with REPL examples you can test yourself.
Operator Symbol: // · Full Name: Floor Division · 5 // 2 Result: 2 · Result Type: int · -5 // 2 Result: -3
Quick snapshot
- // always floors quotient (toward -inf) (Mimo (Programming glossary))
- Works in all Python 3 versions Mimo (Programming glossary)
- NumPy floor division edge cases
- Behavior with complex numbers
- Python’s design choice documented in 2010 The History of Python (Official Python documentation source)
- Practice with negative number examples
This reference table consolidates the essential floor division facts:
| Label | Value |
|---|---|
| Symbol | // |
| Name | Floor Division |
| 5 // 2 | 2 |
| -5 // 2 | -3 |
| Result Always | Floored to -inf |
How do I use floor division (//) in Python?
The // operator takes two operands and returns the floor of their quotient as an integer. Python uses math.floor() internally to determine the result, which always rounds toward negative infinity regardless of the signs involved Software Development Notes (Technical documentation).
Basic Syntax
quotient = dividend // divisor- Both operands can be integers or floats
- Result type matches input: int + int = int, float + int = float
Integer Examples
15 // 4returns3because 3 is the largest integer less than or equal to 3.75 LearnDataSci (Python educational resource)7.5 // 2returns3.0because the result of dividing 7.5 by 2 is 3.75 GeeksforGeeks (Programming tutorials)
What is 5 // 2?
When you run 5 // 2, Python returns 2, discarding the remainder entirely. This is the most basic behavior of floor division: it finds the largest integer less than or equal to the true quotient.
Positive Integer Case
5 / 2equals2.5(true division)5 // 2equals2(floor division)- The fractional part .5 gets dropped
Floor Explanation
The floor function returns the largest integer less than or equal to x, which means -4 is considered less than -3.333 recurring The Teclado Blog (Educational resource). For positive numbers, this behaves like truncation, but the mechanism is different.
The pattern shows how floor division differs from simple type casting in Python math operations—whereas int(2.5) truncates toward zero, floor division rounds down to the next integer.
What does // mean in Python?
The double slash // is Python’s floor division operator. It yields the floor of the quotient, which is distinct from simply casting to int() because it uses the mathematical floor rather than truncation Mimo (Programming glossary).
Definition
- Operator category: Arithmetic operators
- Returns: floored integer (or float if any operand is float)
- Defined in: Python Language Reference
Operator Precedence
Floor division has the same precedence as regular division and multiplication in Python’s operator hierarchy. It evaluates left-to-right within its precedence level, and lower than exponentiation but higher than addition and subtraction.
In an expression like 2 + 10 // 3 * 2, the floor division executes before the multiplication, which executes before the addition. Understanding precedence prevents subtle bugs.
What is the difference between // and /?
This is the most common point of confusion. The single slash / performs true division and always returns a float in Python 3. The double slash // performs floor division, returning a floored result LearnDataSci (Python educational resource).
True Division /
5 / 2returns2.510 / 4returns2.5- Result is always a floating-point number
Floor vs Float Results
5 // 2returns210 // 4returns2- Result type depends on operand types
For positive numbers, floor division and truncation often look identical, but the difference becomes critical with negative operands.
How does // handle negative numbers?
This is where Python’s floor division diverges from many other languages. Python’s floor division operator rounds results toward negative infinity, even when working with negative numbers Mimo (Programming glossary). Other languages like Swift, C++, and Java truncate the value toward zero for integer division, producing different results Software Development Notes (Technical documentation).
Negative Examples
-5 // 2returns-3, not-25 // -2returns-3regardless of which operand is negative-10 // 3returns-4because -10 divided by 3 equals -3.33, and floor division rounds down CodeGym (Programming education)-15 // 4returns-4since -4 is the largest integer less than or equal to -3.75 LearnDataSci (Python educational resource)
Float Edge Cases
When one operand is negative in floor division, the result of normal division is negative, so the largest integer less than or equal is further from zero LearnDataSci (Python educational resource). For example, -17 // -5 returns 3 because -17 divided by -5 equals 3.4 GeeksforGeeks (Programming tutorials).
Integer division is not a well-defined concept, and each programming language specification can choose truncation toward zero or negative infinity as its defined result Software Development Notes (Technical documentation). Python chose the latter for mathematical consistency.
How to use floor division step by step
Here is a practical sequence for using floor division in your code, from basic to edge cases.
Open a Python REPL or create a new .py file.
Try a basic positive case: type 10 // 3 and press Enter. The output should be 3.
Test with floats: type 7.5 // 2. The output should be 3.0 GeeksforGeeks (Programming tutorials).
Test a negative dividend: type -5 // 2. The output should be -3, not -2 The History of Python (Official Python documentation source).
Verify with math.floor(): type import math then math.floor(-10 / 3). The output should be -4.0 GeeksforGeeks (Programming tutorials).
Compare with regular division: type -5 / 2 (returns -2.5) versus -5 // 2 (returns -3).
Confirmed facts and open questions
Confirmed
- // always floors quotient toward negative infinity Mimo (Programming glossary)
- Works in all Python 3 versions Mimo (Programming glossary)
- Distinct from int() casting Mimo (Programming glossary)
- Linked mathematically to modulo operator The History of Python (Official Python documentation source)
What’s unclear
- NumPy floor division edge cases
- Behavior with complex numbers
- Floor division in Python 2 vs. 3 for floats
What experts say
The integer division operation (//) and its sibling, the modulo operation (%), go together and satisfy a nice mathematical relationship.
The History of Python (Official Python documentation source)
Floor division always rounds away from zero for negative numbers, so -3.5 will round to -4, but towards zero for positive numbers, so 3.5 will round to 3.
Python’s floor division and modulo operation satisfy the mathematical relationship: b*q + r = a where 0 <= r < b The History of Python (Official Python documentation source). This invariant holds for both positive and negative dividends when using floor division. If truncating quotient toward zero were used instead, the remainder would become negative, breaking this elegant property.
Related reading: Floor division in Python · Python math operations
youtube.com, news.ycombinator.com, theknowledgeacademy.com, users.rust-lang.org
As you grasp the floor division operator through examples like 5//2=2, beginners should first establish basics via this Python beginners guide covering installation and roadmap.
Frequently asked questions
What is the result of 10 // 3 in Python?
The result of 10 // 3 is 3. The true division 10 / 3 equals 3.333..., and floor division returns the largest integer less than or equal to that value.
Does // work with floating point numbers?
Yes. When either operand is a float, floor division returns a float result. For example, 7.5 // 2 returns 3.0 GeeksforGeeks (Programming tutorials).
What happens with 0 // 5?
Zero divided by any non-zero number using floor division returns 0. Both 0 // 5 and 0 // -5 return 0.
Is floor division the same as truncation?
No. The floor division operator uses mathematical floor, which always rounds toward negative infinity. Simply casting to int() truncates toward zero, producing different results for negative numbers Mimo (Programming glossary).
How does // behave in Python 2 vs. 3?
In Python 2, the / operator performed floor division for integers and true division for floats. Python 3 unified this: / always returns a float, while // performs floor division.
What is the precedence of //?
Floor division has the same precedence as multiplication and regular division. It evaluates after exponentiation but before addition and subtraction.
How does floor division relate to the modulo operator?
Floor division and modulo are linked by the identity: x = (x // y) * y + (x % y) The Teclado Blog (Educational resource). In Python, the modulo operator result always has the same sign as the divisor, not the dividend.
For developers migrating from C, Java, or JavaScript, the behavior of // with negative numbers requires particular attention. Where those languages truncate toward zero, Python floors toward negative infinity—a choice that maintains mathematical consistency for modular arithmetic across all integers.