Python 2.x vs Python 3(三)
来源:互联网 发布:linux dhcp配置步骤 编辑:程序博客网 时间:2024/05/16 03:36
Python2.x vs Python3
Python2.x vs Python3——从 raw_input() 到 input()
int/long
在 Python 2.x 视 int/long 为不同的整型类型,在 Python 3中不再进行区分,Python 2.x 还是 Python 3 都对浮点数的单精度(float)和双精度(double)进行区分,均视为双精度类型,也即在 Python 3 中实现了整型/长整型,单精度/双精度的统一。
# Python 2.x>>> a = 10>>> type(a)<type 'int'>>>> a = 10l>>> type(a)<type 'long'>
cmp 参数
在 Python 3.x 的世界里,一些函数(sorted、min、max)不再支持 cmp 参数(用于对大小的定义)。
next() ⇒ __next__()
In Python 3, to make syntax more consistent, the next() method was renamed to __next__().
dict.keys()
TypeError: ‘dict_keys’ object does not support indexing
>>> d = {'video':0, 'music':23}>>> k = b.keys()>>> k[0]TypeError: 'dict_keys' object does not support indexing>>> kdict_keys(['music', 'video'])
Python 3 改变了 dict.keys() 的行为,在 Python 3 中它返回一个 dict_keys 类型对象,可迭代但不可索引(iterable but not indexable,就像 Python 2. 中的 dict.iterkeys()),可通过显式地调用 list 实现类型的转换。
如下代码所示:
>>> b = { 'video':0, 'music':23 }>>> k = list(b.keys())>>> k['music', 'video']
或者:
>>> list(b)['music', 'video']
(1)xrange,range(Python 2) ⇒ range(Python 3)
Python 2:**xrange:可迭代,不可索引;**range:可迭代,可索引
Python 3:range:可迭代,不可索引(2)dict.keys(),dict.getkeys()(Python 2) ⇒ dict.keys()(Python 3)
Python 2:dict.keys():可迭代,可索引;dict.getkeys():可迭代,不可索引
Python 3:dict.keys():可迭代,不可索引
References
[1] Python dictionary.keys() error
- Python 2.x vs Python 3(三)
- Python 2.x vs 3.x
- Python 面向对象 —— super 的使用(Python 2.x vs Python 3.x)
- Python 2.x vs Python 3.x(四)—— TypeError: unhashable type
- Python 3.X 基础(三)
- Python学习(3): Python 3.x 和 Python 2.x的区别
- cocos2d-x使用python创建vs模板
- Python-3 python变量VS字符串
- Python中字符编码的总结和对比:Python 2.x的str和unicode vs Python 3.x的bytes和str
- 简明Python教程笔记三 python3.x
- common lisp vs python 3
- VS2010 + cocos2dx 2.X版本 + python 3
- 自学OPENCV+PYTHON+MACOS-X(3)
- Python 进阶 —— x = x+1 vs x += 1
- Python有趣的现象——x+=y VS x=x+y
- python集锦(三)
- python理论学习(三)
- python笔记(三)
- Android自定义控件(三)——打造闪闪发光的字体
- mingw的安装
- Qt右击菜单栏实现
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- 无法连接远程桌面--必须为远程桌面启用Windows防火墙例外
- Python 2.x vs Python 3(三)
- svn Changing directory 'xxxxxxx' is forbidden by the server 解决方案
- docker镜像操作
- iOS经典讲解之UIAlertView的使用技巧
- 网站根目录
- Notepad++ Emmet/Zen coding插件的安装
- TOEFL 学习笔记(writing)
- display的属性值
- docker容器启动