Python運算符中/和//的區別 Python運算符中/和//的區別

Python運算符中/和//的區別

首先先看單斜杆的用法:舉幾個栗子:

>>> print 5/3,type(5/3)

1 <type 'int'>
>>> 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的數據類型,只有A和B都爲int型時結果纔是int(此時表示兩數正除取商),其他情況全是float型,在看看數值,當結果爲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的時一樣的,但//取的是結果的最小整數,而/取得是實際的除法結果,這就是二者的主要區別啦

原文地址:https://blog.csdn.net/Zackary_/article/details/78597959

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章