python2和python3在运算符上的区别

文章讲述了Python2和Python3在处理整数除法(整数结果不同)、浮点数除法以及比较运算符语法上的区别。Python3中整数除法会得到浮点数,而在比较运算符使用上,Python3不支持`<><>`,只接受`!=`。
摘要由CSDN通过智能技术生成

今天在巩固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
>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值