python教程:「/」運算符和「//」運算符的區別

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

>>> 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型,結果是保留若干位的小數,是咱們正常思惟中的除法運算學習

在看看雙斜杆的例子:code

#Python學習交流QQ羣:778463939

>>> 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(此時表示兩數正除取商)class

//取的是結果的最小整數,而/取得是實際的除法結果,這就是兩者的主要區別啦數據類型

相關文章
相關標籤/搜索