python2与python3的区别对比

目前使用比较多的是python3(python的主流版本是python3.6之后的版本),但是有一些程序由于未改动,还是使用python2,因此我们需要简单了解一下python2与python3中存在哪些差异,以下是我在学习过程中总结的差异,希望对你有帮助!

1.输出有差异

print语句被python3废弃,统一使用print()函数

[root@scchen1 ~]# python2
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "xieshan"
xieshan
>>> print("xieshan")
xieshan
>>> 
[root@scchen1 ~]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print "xieshan"
  File "<stdin>", line 1
    print "xieshan"
                  ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("xieshan")?
>>> print("xieshan")
xieshan
>>> 

2.输入有差异

在Python2中,input()函数会尝试执行输入的内容作为Python代码,raw_input()函数会将输入作为字符串处理

在Python3中,raw_input()函数被废弃,统一使用input函数,input()函数会将所有输入作为字符串处理

[root@scchen1 ~]# python2
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> suda = input("please input a word: ")
please input a word: "a"
>>> type(suda)
<type 'str'>
>>> suda = input("please input a word: ")
please input a word: 12
>>> type(suda)
<type 'int'>
>>> suda = raw_input("please input a word: ")
please input a word: 12
>>> type(suda)
<type 'str'>
[root@scchen1 ~]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> suda = input("please input a word: ")
please input a word: a
>>> type(suda)
<class 'str'>
>>> suda =raw_input("please input a word: ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>> 

3.数据类型有差异

Python2中整型分为整型和长整型,Python3中统称为整型,长整型被Python3废弃,统一使用int

mockingbird@MockingbirddeMacBook-Air ~ % python2


>>> type(123)
<type 'int'>
>>> type(10000000000000000000000)
<type 'long'>
>>> 
mockingbird@MockingbirddeMacBook-Air ~ % python3

>>> type(123)
<class 'int'>
>>> type(100000000000000000000)
<class 'int'>
>>> 

4.整除有差异

python2中的“/”是地板除(向下取最接近的整数),python2要想真除,就要转换为浮点数。python3中的“/”是真除,要想得到地板除结果,使用“//”

mockingbird@MockingbirddeMacBook-Air ~ % python2
>>> 6/4
1
>>> 6.0/4.0
1.5
>>> 
mockingbird@MockingbirddeMacBook-Air ~ % python3
>>> 6/4
1.5
>>> 6//4
1

5.不等于操作符有差异

Python2中有"<>"和"!="两种方式表示不等于,Python3中只能用"!="表示。不相等操作符"<>"被Python3废弃,统一使用"!="

mockingbird@MockingbirddeMacBook-Air ~ % python2
>>> 1!=2
True
>>> 1<>2
True
mockingbird@MockingbirddeMacBook-Air ~ % python3
>>> 1!=2
True
>>> 1<>2
  File "<stdin>", line 1
    1<>2
     ^
SyntaxError: invalid syntax
>>> 

6.range有差异

xrange函数被python3废弃,统一使用range。python2中range返回的是一个列表,xrange返回的是一个可迭代对象。python3中range返回的是一个可迭代对象

mockingbird@MockingbirddeMacBook-Air ~ % python2
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> xrange(10)
xrange(10)
>>> 
mockingbird@MockingbirddeMacBook-Air ~ % python3
>>> range(10)
range(0, 10)
>>> xrange(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'xrange' is not defined
>>> 

7.异常机制的差异

python2:
except IndexError , ie:
python3:
except IndexError as ie:

8.默认编码有差异

python2的默认编码是ASCLL码,python3的默认编码是utf-8

9.布尔类型的差异

python3中的True和False属于关键字,而在python2中不属于

10.格式化标识符有差异

python2中没有f标识符表示格式化的用法,python3中有

11.包目录结构有差异

包结构的目录里面可以有一个 __init__.py 模块,python2:__init__.py是必须的,python3:__init__.py是可选的  

12.大小写转化函数有差异

python3中string.letters和相关的string.lowercase和string.uppercase被去除,请改用string.ascii_letters 、string.ascii_lowercase、string.ascii_uppercase等 

13.python中类的定义有差异

class Atm():
    pass
class Atm:
    pass
class Atm(object):
    pass

python2中类的定义包括经典类和新式类

python3中类的定义只有新式类

python2中显示的继承object的类称为新式类,否则称为经典类(在python2中,上面的前两种为经典类,最后一种为新式类)

python3种默认都是继承object,所以都是新式类(以上三种在python3中都属于新式类)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值