What is the purpose of the `//` operator in python?

Possible duplicate:
What is the reason for having "//" in Python?

What is the purpose of the // operator?

  x = 10
 y = 2
 print x / y
 print x // y

Both output 5 as a value.

+4
source share
1 answer

Integer division versus floating division:

 >>> 5.0/3 3: 1.6666666666666667 >>> 5.0//3 4: 1.0 

Or, as they put it in Python docs , // is (on the floor) private from x and y. "The above example was run in Python 2.7.2, which only behaves this way for floating point numbers. If you would use integers in 2.7.2, you would get:

 >>> 5/3 9: 1 >>> 5//3 10: 1 

In Python 3.x, you get different results, so if you really want to use the floor version, get used to using // , as it will matter some day:

 Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> 5/3 1.6666666666666667 >>> 5//3 1 >>> 5.0/3 1.6666666666666667 >>> 5.0//3 1.0 
+17
source

All Articles