Python3的新增功能
Python 3.x引入了一些与Python 2不兼容的关键字和函数,这些关键字和函数可以通过Python 2中的内置__future__模块导入。如果计划为代码提供Python 3.x支持,建议使用__future__导入。
例如,如果我们想在Python 2中使用Python 3.x的整数除法行为,请添加以下import语句。
from __future__ import division
打印函数
Python3最显着,最广为人知的变化是print函数,现在必须在print函数中使用括号(),它在Python 2中是可选的。
print "Hello World" #is acceptable in Python 2 print ("Hello World") # in Python 3, print must be followed by ()
默认情况下,print()函数在末尾插入新行。在Python 2中,可以通过在最后加上','来抑制它。在Python 3中,“ end =''”会附加空格而不是换行符。
print x, # Trailing comma suppresses newline in Python 2 print(x, end=" ") # Appends a space instead of a newline in Python 3
从键盘读取输入
Python2有两个版本的输入函数,input()和raw_input()。如果输入的数据包含在引号“或”中,则input()函数会将其视为字符串,否则将其视为数字。
在Python3中,不推荐使用raw_input()函数。此外,接收到的数据始终被视为字符串。
In Python 2 >>> x = input('something:') something:10 #entered data is treated as number >>> x 10 >>> x = input('something:') something:'10' #entered data is treated as string >>> x '10' >>> x = raw_input("something:") something:10 #entered data is treated as string even without '' >>> x '10' >>> x = raw_input("something:") something:'10' #entered data treated as string including '' >>> x "'10'" In Python 3 >>> x = input("something:") something:10 >>> x '10' >>> x = input("something:") something:'10' #entered data treated as string with or without '' >>> x "'10'" >>> x = raw_input("something:") # will result NameError Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> x = raw_input("something:") NameError: name 'raw_input' is not defined
整除
在Python2中,将两个整数相除的结果四舍五入为最接近的整数,例如3/2结果为1。为了支持浮点除法,必须将分子或分母明确用作浮点数。例如3.0 / 2或3 / 2.0或3.0 / 2.0将得出1.5
Python3默认将3/2评估为1.5,这对于新程序员来说更加直观。
Unicode表示法
如果要存储为Unicode,Python2要求你用u标记字符串。
默认情况下,Python3将字符串存储为Unicode。我们有Unicode(utf-8)字符串和2个字节的类:字节和字节数组。
xrange()函数已删除
在Python 2中,range()返回一个列表,xrange()返回一个仅在需要时才生成范围内项目的对象,从而节省了内存。
在Python 3中,删除了range()函数,并将xrange()重命名为range()。另外,range()对象支持Python 3.2及更高版本中的切片。
提出例外
Python 2接受“旧”和“新”两种语法。如果不将括号中的异常参数括起来,Python 3会引发语法Error。
raise IOError, "file error" #This is accepted in Python 2 raise IOError("file error") #This is also accepted in Python 2 raise IOError, "file error" #syntax error is raised in Python 3 raise IOError("file error") #this is the recommended syntax in Python 3
异常参数
在Python 3中,异常参数应使用'as'关键字声明。
except Myerror, err: # In Python2 except Myerror as err: #In Python 3
next()函数和.next()方法
在Python 2中,允许使用next()作为生成器对象的方法。在Python 2中,还接受了对生成器对象进行迭代的next()函数。但是,在Python 3中,next(0作为生成器方法不再使用并引发AttributeError.
gen = (letter for letter in 'Hello World') # creates generator object next(my_generator) #allowed in Python 2 and Python 3 my_generator.next() #allowed in Python 2. raises AttributeError in Python 3
2to3实用程序
通常将2to3.py脚本与Python 3解释器一起安装在tools / scripts文件夹中。它读取Python 2.x源代码,并应用一系列修复程序将其转换为有效的Python 3.x代码。
Here is a sample Python 2 code (area.py): def area(x,y = 3.14): a = y*x*x print a return a a = area(10) print "area",a To convert into Python 3 version: $2to3 -w area.py Converted code : def area(x,y = 3.14): # formal parameters a = y*x*x print (a) return a a = area(10) print("area",a)