Python 2 to 3
来源:互联网 发布:机器人单片机 编辑:程序博客网 时间:2024/06/05 14:59
这篇博客主要讲讲Python2与Python3的部分异同
使用2to3将代码移植到Python 3
print
首先最简单的区别的必然是print语句,简单来说就是有无括号
unicode字符串
Python2中必须使用 u’unicode_str’来表示这是unicode字符串
Python3默认是unicode,所以可以直接时候输入’unicode_str’
<>
Python3不支持<>这个运算符,需使用!=
Python2则两者都支持
字典部分区别
has_key()
Python2使用dict.has_key(“xx”),来检测字典中是否包含xx
Python3不支持此方法,使用 “xx” in dict 即可
keys()、items()
Python2使用此方法默认返回值为列表
Python3必须使用list方法进行转换
调用部分模块的区别
urllib、urllib2
Python2 直接调用两者即可
Python3 合并了urllib2,综合到了urllib中
commands –> subprocess
filter()
Python2 采用了filter(xxx) 返回列表
Python3 要使用list方法,才能使filter返回列表
map()
Python2 返回列表
Python3 返回迭代器
reduce()
Python2 直接使用
Python3 需要引入from functools import reduce
try…except
Python2 使用except someError, e
Python3 使用except someError as e:
raise
Python2 使用raise xxx, ‘some msg’
Python3 使用raise xxx(‘some msg’)
xrange()
Python2 使用xrange(10)
Python3 使用range(10)
raw_input()
Python2 使用raw_input()
Python3 使用input(),Python2的input()转为eval(input())
lambda()
Python2 使用lambda (x, (y, z)): x + y + z
Python3 使用lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]
zip()
Python2 使用zip(a, b, c)即可返回一个元组组成的列表
Python3 要使用list(zip(a, b, c))才能返回列表,否则返回迭代器
对元组列表解析
Python2 [i for i in 1, 2]
Python3 [i for i in (1, 2)]
- Python 2 to 3
- link to python 3
- Path to Python(2)
- Pygame_How to Install Pygame to Python 3 on Ubuntu
- How to Install Pygame to Python 3 on Ubuntu
- Topic 3:An Informal Introduction to Python
- Python 3 : Convert string to bytes
- [Coursera][Rice] An introduction to interactive Programming in Python Week 2 3
- introduce to python
- Begin to learn python
- Python How to Program
- Python dataframe to array
- use python to sendmail
- Use Python to plot
- use python to sendmail
- Welcome to Python!
- python list to string
- python link to Orcal
- Linux
- 最短路径问题的概述
- 2017.8.19结构体整理(对照类理解异同)
- 欢迎使用CSDN-markdown编辑器
- 面团点评笔试:整数加法
- Python 2 to 3
- 设计模式之静态代理模式
- 读《大道至简》有感
- iOS 一一 自定义cell按钮的点击事件(通知机制)
- 第三天:css选择器的优先级、Adobe photoshop CS6软件、html初步布局
- 采用lombok简化代码
- node.js遇到的错误
- Spring Boot 视频
- spring schema 扩展(NamespaceHandler,BeanDefinitionParser)