Python 2.x和Python 3.x版本之间有什么区别?
随着科技的发展,Python 2.x 逐渐向 Python 3.x 过渡。这个过程也就意味着写 Python 程序的时候,我们可能会涉及到 Python 2.x 和 Python 3.x 之间的差别。在此,我们将详细解析它们之间的差异。
阅读更多:Python 教程
Python3中的Print语句与Python2中的print语句
在 Python2 中,我们可以使用如下代码进行输出
#Python 2.x 输出Hello World
print "Hello World!"
但是在 Python3 中这种用法就会编译错误,换而言之,Python2.x 中的 print
与 Python 3.x 中的 print()
是不同的。
可以看出,Python3 是要求在语句后添加括弧的,因为print 就被定义成了一个函数。因此,在 Python 3.x 中,您必须像下面这样编写
#Python 3.x 输出Hello World
print("Hello World!")
Python3中的文本编码
Python 默认使用 ASCII 编码来进行文本处理。在 Python 2.x 中,当你要处理unicode字符时,你需要在代码中加入一下在代码中加入以下代码来告诉 Python,你的代码是Unicode类型的:
# -*- coding:utf-8 -*-
这一行代码告诉解释器文本是utf-8类型的。在Python 2.x 中,这样做很常见,而在 Python 3.x 中,解释器默认使用 UTF-8 编码,所以这个代码已经不是必须的,不过还是可以加上的。
Python 2.x中的Unicode类型与Python 3.x中的Unicode类型
Python 2.x中的Unicode字符串在Python 3.x中的的字符串中的表示方式是相同的,但在Python 2.x中的字符串等同于在Python 3.x中的bytes类型,这在 Python 3.x 中引发了许多向后不兼容问题。
#Python 2.x 上下方向箭头Unicode类型
unicode_arrow_down = u"\u21B3"
print type(unicode_arrow_down) #<type 'unicode'>
#Python 3.x 上下方向箭头Unicode类型
unicode_arrow_down = "\u21B3"
print type(unicode_arrow_down) #<class 'str'>
在 Python 3.x 版本中,以上两个代码输出结果是一样的,值均为 <class 'str'>
。
Python2.x中的/操作符(非浮点数运算)与Python3.x中的//操作符
在 Python 2.x 中,/ 操作符总会返回一个整型结果:
# Python 2.x 中的 / 操作符
result = 9 / 2
print result # 4
而在 Python 3.x 中,/ 操作符总会得到一个高精度小数(浮点型),// 则会返回一个整型数。
# Python 3.x 中的 / 和 // 操作符
result = 9 / 2
print(result) #4.5
result = 9 // 2
print(result) #4
字符串的ASCII和字节不等效
Python 3.x对字符串和字节的分离更彻底。
举个例子,你会发现在Python 2.x中这两个操作返回的是同样的结果:
# Python 2.x 中的字符串比较
print "hello" == b"hello" # True
但在 Python 3.x 中,这种类型的比较将不再起效。Python 3.x 中唯一可能的方法是将整个字符串转换为字节。
# Python 3.x 中的字符串比较
print "hello" == b"hello" #False
print "hello".encode() == b"hello" #True
Python 3.x的range()函数和Python 2.x的range()函数
在 Python 2.x 中,range()
函数返回的是一个列表。这种方法被广泛使用,也是创建列表的一种最常见方法:
# Python 2.x 中的 range() 函数
result = range(5)
print result #[0, 1, 2, 3, 4]
而在 Python 3.x 中,range()
函数会返回一个可迭代的对象。如果需要返回列表,则需要手动转换为列表:
# Python 3.x 中的 range() 函数
result = range(5)
print(result) # range(0, 5)
result = list(result)
print(result) #[0, 1, 2, 3, 4]
异常的处理
在 Python 3.x 中,异常处理有一些微妙的变化。Python 3.x 引入了一个新的语言特性,允许你在 except 子句中提供 as
关键字。
举个例子,在 Python 2.x 中,处理异常的正确方法如下:
# Python 2.x 中的异常处理
try:
result = 5 / 0
except Exception, e:
print e
而在 Python 3.x 中,你需要这样写:
# Python 3.x 中的异常处理
try:
result = 5 / 0
except Exception as e:
print(e)
需要注意,as
子句后面的变量将指向 Exception
对象。
print函数
在 Python 2.x 中,print 函数是个语句:
# Python 2.x 中的 print 语句
print "hello world"
而在 Python 3.x 中,print() 是个函数:
# Python 3.x 中的 print() 函数
print("hello world")
尽管 Python 3.x 移除了 print 语句,并引入了 print() 函数,但是你可以通过导入 print_function
来使用 Python 3.x 的 print() 函数:
# 使用 Python 3.x 的 print() 函数
from __future__ import print_function
print("hello world")
input函数
在 Python 2.x 中,input() 函数会尝试将用户输入作为 Python 代码进行求值,这可能造成潜在的安全问题。而在 Python 3.x 中,input()
函数等价于 Python 2.x 中的 raw_input()
函数。
# Python 2.x 中的 input() 函数
user_input = input("Enter your name: ")
print user_input
# Python 3.x 中的 input() 函数
user_input = input("Enter your name: ")
print(user_input)
总结
Python 2.x 和 Python 3.x 有很多不同点,包括了语法、语句、关键字和内置函数的变化等等。我们必须注意这些变化,并确保我们的代码可以兼容两个版本。同时,对于新手来说,采用学习 Python 3.x 是个不错的选择。