首先先看单斜杆的用法:举几个栗子:
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的时一样的,但//取的是结果的最小整数,而/取得是实际的除法结果,这就是二者的主要区别