文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > python2python3
侵权投诉

python2python3

New in Python 3.1

General Notes

Dictionary and set comprehensions; * unpacking; binary literals; bytes and bytearray types; bz2.BZ2File and gzip.GzipFile are context managers; collections.Counter dictionary type; collections.OrderedDict insertion-ordered dictionary type; decimal.Decimals can be created from floats Python 2 Python 3.1 d={} d={x: x**3 for x in range(5): for x in range(5)} d[x]= x**3 S= set( S={x for x in seq}[x for x in seq])

Python 3.1 a, *b= (1, 2, 3)# a==1; b==[2, 3]# a==[1, 2]; b==3 *a, b= (1, 2, 3) a, *b, c= (1, 2, 3, 4)# a==1; b==[2, 3]; c==4 x= 0b1001001 s= bin(97) y= int(s, 2) u="The# x==73# s=='0b1100001'# y==97

Python 3 often returns iterables where Python 2 returned lists. This is usually ne, but if a list is really needed, use the list() factory function. For example, given dictionary, d, list(d.keys()) returns its keys as a list. Aff

ected functions and methods include dict.items(), dict.keys(), dict.values(), filter(), map(), range(), and zip(). Most of the types module’s types (such as types. LongType) have gone. Use the factory function instead. For example, replace if isinstance(x, types.IntType) with if isinstance(x, int). Comparisons are strict—x< y will work for compatible types (e.g., x and y are both numbers or both strings); otherwise raises a TypeError. Some doctests involving oating point numbers might break because Python 3.1 uses David Gay’s algorithm to show the shortest representation that preserves the number’s value. For example, 1.1 in Python 2 and 3.0 is displayed as 1.1000000000000001, and in Python 3.1 as 1.1. String Format Speci cations

"# or: u="The\u20ac"# or: u="The\N{euro sign}" v= u.encode("utf8")# v==b'The\xe2\x82\xac' w= v.decode("utf8")# w=='The ' x= bytes.fromhex("54 68 65 20 E2 82 AC")# x==b'The\xe2\x82\xac' y= x.decode("utf8")# y=='The ' z= bytearray(y) z[-3:]= b"$"# z==bytearray(b'The$') with bz2.BZ2File(filename) as fh: data= fh.read() counts= collections.Counter("alphabetical")# counts.most_common(2)==[('a', 3), ('l', 2)] d= collections.OrderedDict( (("x", 1), ("k", 2), ("q", 3)))# list(d.keys())==['x', 'k', 'q'] dec= decimal.Decimal.from_float(3.75)# dec==Decimal('3.75')

strings have one or more replacement elds of form:{Name!Conv:Spec}. Name identi es the object to format. Optional !Conv is: !a (ASCII repr() format), !r (repr() format), or !s (string format). Optional:Spec is of form: str.format(): Fill Align Sign# 0 Width, .Prec Type

is any character except}. Align is:< (left),> (right),^ (center), or= (pad between sign and number). Sign is:+ (force), - (- if needed), or“” (space or -).# pre xes ints with 0b, 0o, or 0x. 0 means 0-pad numbers. Width is the minimum width. The, means use grouping commas. .Prec is the maximum width for strs and number of decimal places for floats. Type is:% (percent), b (binary), d (decimal), e or E (exponential), f (float) g or G (general float) n (localized) o (octal), x or X (hex). Everything is optional, except that Fill requires Align. Fill"{:*=+10.1f}".format(12345.67)"{:*>+10.1f}".format(12345.67)"{:+010.1f}".format(12345.67)"{:,.2f}".format(12345.678)#### '+**12345.7' '**+12345.7' '+0012345.7' '12,345.68'

Special Methods

The slice methods (__delslice()__, __getslice()__, __setslice__) are gone; instead __delitem()__, __getitem()__, and __setitem__ are called with a slice object. The methods __hex__() and __oct__() are gone; use hex() and oct(). To provide an integer, implement __index__().

An informIT.com publication by Mark Summer eld.#3 Copyright© Qtrac Ltd. 2009.

License: Creative Commons Attribution-Share Alike 3.0 U.S.

python2python3

上一页第4页

你可能喜欢

热门文档

相关文档

  • Python3.x和Python2.x的区别

    Python3.x和Python2.x的区别_计算机软件及应用_IT/计算机_专业资料。Python3.x和Python2.x的区别编码语法使用2to3将代码移植到PYTHON 3 被重命名或者重新组织的...

  • Python2.X 和 Python3.X 的区别

    Python2.X 和 Python3.X 的区别_计算机软件及应用_IT/计算机_专业资料。Python2.X 和 Python3.X 的区别 1.性能 Py3.0 运行 pystone benchmark 的速度比 Py...

  • Python3.3.2入门教程

    Python3.3.2入门教程_计算机软件及应用_IT/计算机_专业资料。Python3.3.2入门教程本文适合有经验的程序员尽快进入 Python 世界.特别地,如果你掌握 Java 和 Javas...

  • python3.0 对python2.x 升级后重大语法变动

    python3.0 对python2.x 升级后重大语法变动_计算机软件及应用_IT/计算机_专业资料。phthon语法文档python3.0 对 python2.x 升级后重大语法变动 python 2.4 与...

  • Python 3.0与2.0的用法区别

    Python 3.0与2.0的用法区别_IT/计算机_专业资料。本文介绍了一些python2.5之后版本的一些语法的使用方法。1.性能 Py3.0 运行 pystone benchmark 的速度比 Py...

  • Python 3.x与Python 2.x的区别

    Python 3.x与Python 2.x的区别_计算机软件及应用_IT/计算机_专业资料。Python 3.0以后的版本与之前版本的区别,作为参考1.性能 Py3.0 运行 pystone benchmark ...

  • Mod_python_3.2.8中文手册

    Mod_python 3.2.8中文手... 65页 免费 python函数中文手册 21页 1下载券Mod_python_3.2.8...

  • Python 3.3 官方入门指南

    Python 3.3 官方入门指南_计算机软件及应用_IT/计算机_专业资料。官方版本的Python...交互模式 2.2.1. 错误处理 2.2.2. 执行 Python 脚本 2.2.3. 源程序...

  • diveintopython3 ---《深入python3》

    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中文教程

    Python3中文教程_计算机软件及应用_IT/计算机_专业资料。Python已经是3.1版本了.../usr/bin/python a=2 b="test" c=a+b 运行这行程序会出错,提示你字符串...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email