Moving from Python 2 to Python 3 Introduction This document is aimed at Python 2 programmers wishing to start devel-
oping using Python 3. The document lists those objects and idioms that have changed between the versions, showing how to change from Python 2-style to Python 3.1-style. It also lists some of the most useful new features in Python 3.1. Note that if you want to convert an existing program or module, the place to start is with the 2to3 tool (docs.python.org/library/2to3.html). For a complete introduction to the Python 3 language (covering both 3.0 and 3.1, and vastly more than there’s room for here), see Programming in Python 3 (Second Edition) by Mark Summer eld, ISBN 0321680561 (doc.guandang.net).
Strings and String Formatting
Printing and Executing
Python 3 strings are Unicode; unicode() is gone Python 2 Python 3.1 s= unicode(x) s= u"\u20AC" s= ur"\w" s= str(x) s="\u20AC" s= r"\w""{}{}".format(i, s)"%d%s"% (i, s)"{0}{1}".format(i, s)"{i}{s}".format(i=i, s=s)"%(i)d%(s)s"% ({'i':i, 's':s})"%(i)d%(s)s"% ( locals())"{0[i]}{0[s]}".format({'i':i, 's':s})"{i}{s}".format( **{'i':i, 's':s})
New functions print(), exec(); execfile() is gone Python 2 Python 3.1 print a, b, c print"%03d"% 7 print x, print>>sys.stderr, x exec code exec code in ( globals, locals) execfile(file) print(a, b, c) print("{:03d}".format(7)) print(x, end="") print(x, file=sys.stderr) exec(code) exec(code, globals, locals) with open(file) as fh: exec(fh.read())
String% operator is deprecated; use str.format()
exec code in globals exec(code, globals)
"{i}{s}".format( **locals())"%s-%s"% ("X","X")"{0}-{0}".format("X")"{:.2f}".format(3.142)"%.2f"% 3.142"%.4s"%"Prime""{%d%%}"% 20"%0*d"% (3, 7)"{0:.2f}".format(3.142)"{π:.2f}".format(π=3.142)"{:.4}".format("Prime")"{{{}%}}".format(20)"{:0{}}".format(7, 3)
Numbers
Division doesn’t truncate; long() is gone; octal literals must start with 0o (zero-oh) Python 2 Python 3.1 x= 5/ 2.0# x==2.5 x= 5/ 2# x==2 i= 2147483648L j= long(i * 2) x= 0123# x==83 x= 5/ 2# x==2.5 x= 5// 2# x==2 i= 2147483648 j= int(i * 2) x= 0o123# x==83
Representational Form
Iterators
Backticks are gone; use repr() or str.format() Python 2 Python 3.1 s= repr(x) s=`x` s="{!r}".format(x) s="{0!r}".format(x) s="{z!r}".format(z=x)
New next(); iterators must have __next__() Python 2 Python 3.1 x= iterator.next() x= next(iterator) class Iterator: class Iterator: def __init__(self): def __init__(self): self.i= -1 self.i= -1 def next(self): def __next__(self): self.i+= 1 self.i+= 1 return self.i return self.i
Force ASCII representation with ascii() s=`x` s= ascii(x) s="{!a}".format(x)

Python3.x和Python2.x的区别_计算机软件及应用_IT/计算机_专业资料。Python3.x和Python2.x的区别编码语法使用2to3将代码移植到PYTHON 3 被重命名或者重新组织的...
Python2.X 和 Python3.X 的区别_计算机软件及应用_IT/计算机_专业资料。Python2.X 和 Python3.X 的区别 1.性能 Py3.0 运行 pystone benchmark 的速度比 Py...
Python3.3.2入门教程_计算机软件及应用_IT/计算机_专业资料。Python3.3.2入门教程本文适合有经验的程序员尽快进入 Python 世界.特别地,如果你掌握 Java 和 Javas...
python3.0 对python2.x 升级后重大语法变动_计算机软件及应用_IT/计算机_专业资料。phthon语法文档python3.0 对 python2.x 升级后重大语法变动 python 2.4 与...
Python 3.0与2.0的用法区别_IT/计算机_专业资料。本文介绍了一些python2.5之后版本的一些语法的使用方法。1.性能 Py3.0 运行 pystone benchmark 的速度比 Py...
Python 3.x与Python 2.x的区别_计算机软件及应用_IT/计算机_专业资料。Python 3.0以后的版本与之前版本的区别,作为参考1.性能 Py3.0 运行 pystone benchmark ...
Mod_python 3.2.8中文手... 65页 免费 python函数中文手册 21页 1下载券Mod_python_3.2.8...
Python 3.3 官方入门指南_计算机软件及应用_IT/计算机_专业资料。官方版本的Python...交互模式 2.2.1. 错误处理 2.2.2. 执行 Python 脚本 2.2.3. 源程序...
Python 3 “负层” 1. 0. Python 1. 2. Python 3. Microsoft Windows 4. Mac OS X 5. Ubuntu Linux 6. 7. Python Shell 8. Python 编辑 1. Python...
Python3中文教程_计算机软件及应用_IT/计算机_专业资料。Python已经是3.1版本了.../usr/bin/python a=2 b="test" c=a+b 运行这行程序会出错,提示你字符串...
热门文档