Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python // operator – Floor Based Division

  • Vijaykrishna RamVijaykrishna Ram
  • December 29, 2019
  • Python Programming
Python // operator – Floor Based Division

The // operator in Python 3 is used to perform floor-based division.

This means that a // b first divides a by b and gets the integer quotient, while discarding the remainder. This means that the result of a//b is always an integer.

Python // Operator Examples

Here are a few examples to illustrate the same:

>>> 2 // 3
0
>>> 1.3 // 2
0.0
>>> 1.3 // 1.0
1.0
>>> 3.4 // 1.1
3.0
>>> 3.4 // 1.2
2.0
>>> -1//2
-1
>>> -6 // 2
-3
>>> -6 // -3
2

This shows how the // operator performs the floor based division, by only considering the integer part of the division, even for floating-point numbers.

Performing this operation on unsupported types (like lists and strings), will result in a TypeError, as is the same for any other arithmetic operator.


Overloading the // Operator

// refers to the __floordiv__() operator by default, so you can perform operator overloading by overriding this method (operator.__floordiv__(a, b))

Here is an example that overloads the // method for integer lists having the same length, by performing individual floor-based division on every pair of elements.

So the two integer lists [3, 4, 5] and [2, 2, 1] will give [3//2, 4//2, 5//1], which is simply the list [1, 2, 5].

import operator

class MyClass():
    def __init__(self, a):
        self.a = a

    def __floordiv__(self, b):
        if isinstance(self.a, list) and isinstance(b.a, list) and len(self.a) == len(b.a):
            result = []
            # Overload // operator for Integer lists
            for i, j in zip(self.a, b.a):
                result.append(i // j)
            return result
        else:
            # Perform Default // operation otherwise
            return operator.__floordiv__(self.a, b.a)

m = MyClass([3, 4, 5])
n = MyClass([2, 2, 1])

print(m // n)

Output

[1, 2, 5]

Conclusion

In this article, we learned about the // floor division operator. We also learned about performing operator overloading on this by implementing operator.__floordiv__(a, b).

References

  • Python Operator Documentation
Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Python ternary Operator Examples
Previous Post Python ternary Operator Examples
Next Post Python arrow module
Python arrow module

Related Posts

What Does %s Mean in Python String Formatting?

What Does %s Mean in a Python Format String?

  • November 30, 2026
Python Convert String to List

Python Convert String to List: Choose the Right Method

  • November 29, 2026
Translate String Python

Translate String Python

  • November 29, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.