首先先看單斜桿的用法:舉幾個例子
>>> print(5/3),type(5/3)
1.6666666666666667
(None, <class 'float'>)
>>> print(6/3),type(6/3)
2.0
(None, <class 'float'>)
>>> print 5.0/3,type(5.0/3)
1.66666666667 <type 'float'>
>>> print 5/3.0,type(5/3.0)
1.66666666667 <type 'float'>
>>> print 5.0/3.0,type(5.0/3.0)
1.66666666667 <type 'float'>
可以看出,無論數值是不是能整除,或者說A/B中A和B是不是int型,單斜杠的作用全是float型,結果是保留若干位的小數,是我們正常思維中的除法運算
在看看雙斜桿的例子:
>>> print 5//3,type(5//3)
1 <type 'int'>
>>> print 5.0//3,type(5.0//3)
1.0 <type 'float'>
>>> print 5//3.0,type(5//3.0)
1.0 <type 'float'>
>>> print 5.0//3.0,type(5.0//3.0)
1.0 <type 'float'>
>>>
可以看出,在A//B的返回類型取決與A和B的數據類型,只有A和B都為int型時結果才是int(此時表示兩數正除取商)
//取的是結果的最小整數,而/取得是實際的除法結果,這就是二者的主要區別啦