python2与python3的区别
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