今天在巩固python基础的路上,发现了一个在运算符上不同的python版本的区别。
在算数运算符/除法上
python2中,俩个整数相除,并且可以整除的情况下,返回的是整数
C:\Users>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 4/2
2
>>> a = 4/2
>>> type(a)
<type 'int'>
python3中,俩个整数相除,并且可以整除的情况下,返回的仍是浮点数
C:\Users>python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 4/2
2.0
>>> a = 4/2
>>> type(a)
<class 'float'>
但是如果是俩个浮点数相除,得到的结果仍是浮点数
C:\Users>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> b = 4.0/2.0
>>> b
2.0
>>> type(b)
<type 'float'>
C:\Users>python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> b = 4.0/2.0
>>> b
2.0
>>> type(b)
<class 'float'>
在比较运算符<>除法上
在python3上,不支持用<>表示不等于,只支持!=这种形式。
C:\Users\40859>python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 5
>>> b = 6
>>> a<>b
File "<stdin>", line 1
a<>b
^
SyntaxError: invalid syntax
C:\Users>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 5
>>> b = 6
>>> a<>b
True
>>>