当前位置:   article > 正文

python2与python3的区别对比_python3 模块相比 python2

python3 模块相比 python2

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

1.输出有差异

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

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

2.输入有差异

raw_input函数被Python3废弃,统一使用input函数

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

3.数据类型有差异

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

  1. mockingbird@MockingbirddeMacBook-Air ~ % python2
  2. >>> type(123)
  3. <type 'int'>
  4. >>> type(10000000000000000000000)
  5. <type 'long'>
  6. >>>
  1. mockingbird@MockingbirddeMacBook-Air ~ % python3
  2. >>> type(123)
  3. <class 'int'>
  4. >>> type(100000000000000000000)
  5. <class 'int'>
  6. >>>

4.整除有差异

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

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

5.不等于操作符有差异

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

  1. mockingbird@MockingbirddeMacBook-Air ~ % python2
  2. >>> 1!=2
  3. True
  4. >>> 1<>2
  5. True
  1. mockingbird@MockingbirddeMacBook-Air ~ % python3
  2. >>> 1!=2
  3. True
  4. >>> 1<>2
  5. File "<stdin>", line 1
  6. 1<>2
  7. ^
  8. SyntaxError: invalid syntax
  9. >>>

6.range有差异

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

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

7.异常机制的差异

  1. python2:
  2. except IndexError , ie:
  1. python3:
  2. 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中类的定义不同:

  1. class Atm():
  2. pass
  3. class Atm:
  4. pass
  5. class Atm(object):
  6. pass

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

python3中类的定义只有新式类

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/45003
推荐阅读
相关标签
  

闽ICP备14008679号