Python - Division operators reference for Python 3.X and Python 2.X

Introduction

If you are using 3.X, here is the short story on division operators for reference:

>>> (5 / 2), (5 / 2.0), (5 / -2.0), (5 / -2)        # 3.X true division 
(2.5, 2.5, -2.5, -2.5) 

>>> (5 // 2), (5 // 2.0), (5 // -2.0), (5 // -2)    # 3.X floor division 
(2, 2.0, -3.0, -3) 

>>> (9 / 3), (9.0 / 3), (9 // 3), (9 // 3.0)        # Both 
(3.0, 3.0, 3, 3.0) 

For Python 2.X, division works as follows):

>>> (5 / 2), (5 / 2.0), (5 / -2.0), (5 / -2)        # 2.X classic division (differs) 
(2, 2.5, -2.5, -3) 

>>> (5 // 2), (5 // 2.0), (5 // -2.0), (5 // -2)    # 2.X floor division (same) 
(2, 2.0, -3.0, -3) 

>>> (9 / 3), (9.0 / 3), (9 // 3), (9 // 3.0)        # Both 
(3, 3.0, 3, 3.0) 

Related Topic