Python3.x和Python2.x的区别

来源:互联网 发布:优易ip代理软件 编辑:程序博客网 时间:2024/05/18 12:44

1.性能
Py3.0运行 pystone benchmark的速度比Py2.5慢30%。Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可
以取得很好的优化结果。
Py3.1性能比Py2.5慢15%,还有很大的提升空间。
2.编码
Py3.X源码文件默认使用utf-8编码,这就使得以下代码是合法的:

中国 = ‘china’
print(中国)
china
3. 语法
1)去除了<>,全部改用!=
2)去除“,全部改用repr()
3)关键词加入as 和with,还有True,False,None
4)整型除法返回浮点数,要得到整型结果,请使用//
5)加入nonlocal语句。使用noclocal x可以直接指派外围(非全局)变量
6)去除print语句,加入print()函数实现相同的功能。同样的还有 exec语句,已经改为exec()函数
例如:
2.X: print “The answer is”, 2*2
3.X: print(“The answer is”, 2*2)
2.X: print x, # 使用逗号结尾禁止换行
3.X: print(x, end=” “) # 使用空格代替换行
2.X: print # 输出新行
3.X: print() # 输出新行
2.X: print >>sys.stderr, “fatal error”
3.X: print(“fatal error”, file=sys.stderr)
2.X: print (x, y) # 输出repr((x, y))
3.X: print((x, y)) # 不同于print(x, y)!
7)改变了顺序操作符的行为,例如x

原创粉丝点击