1, 字符编码
python2 默认 ascii 编码
python3 默认 utf-8 编码
2, 除法运算
python2 整数相除的结果是一个整数, 把小数部分完全忽略掉, 浮点数除法会保留小数点的部分得到一个浮点数的结果.
在 python3 中 对于整数之间的相除, 结果也会是浮点数
- Python 2.x:
- >>> 1 / 2
- 0
- >>> 1.0 / 2.0
- 0.5
- Python 3.x:
- >>> 1/2 0.5
3, 不等于运算符
Python 2.x 中不等于有两种写法 != 和 <>
Python 3.x 中去掉了<>, 只有!= 一种写法
4, 数据类型
Py3 去除了 long 类型, 现在只有一种整型 --int, 但它的行为就像 Py2 版本的 long
5, 捕获异常
python2 捕获异常的语法: except Exception, e
python3 except Exception as e
6,unicode 字符串
python2 中有两种字符串类型: Unicode 字符串和非 Unicode 字符串. Python3 中只有一种类型: Unicode 字符串.
python2有两个函数可以把对象强制转换成字符串: unicode()把对象转换成 unicode 字符串, 还有 str()把对象转换为非 Unicode 字符串. Python3 只有一种字符串类型, unicode 字符串, 所以 str()函数即可完成所有的功能.
7,print()和 raw_print()
在 python2.x 中 raw_input()和 input( ), 两个函数都存在, 其中区别为:
python2 中
raw_input()--- 不管输入什么都返回字符串类型
input()--- 只能接收 "数字" 的输入, 它返回所输入的数字的类型(int, float )
在 python3 中 raw_input()和 input( )进行了整合, 去除了 raw_input(), 仅保留了 input()函数, 用法与 py2 中的 raw_input()一致
8,range 和 xrange
py2 中 range()直接生成列表 (量大占内存死机),py3 中只有 range() 和 py2 中还有的 xrang()都只生成一个生成器
9, 经典类与新式类
创建类时, py2 分为经典类和新式类, 新式类就是继承 object 的类, 经典类是没有继承的类, 而 py3 中全部是新式类, 默认继承 object
来源: http://www.bubuko.com/infodetail-3363278.html