python2和python3的区别

编码

python2默认编码凡是ASCII码(不能识别中文,要在文件头部加上 #-- encoding:utf-8 -- 指定编码方式)python

python3默认编码方式unicode(能够识别中文)python2.7

print

python2中加不加括号均可以打印编码

python3中必须加括号code

input

  • python2 raw_input()
    raw_input()获得了str
    input()获得了int对象

  • python3 input()
    input()获得了str继承

range()

python2 range()/xrange()utf-8

python3 range(),是可迭代对象unicode

python3中都是新式类input

python2.7中经典类和新式类混合class

继承了object的类是新式类

新式类查找广度优先,经典类查找深度优先

python3能够使用super

python2不能使用super

浮点数

python2的除法不是浮点数只返回商,整数同样

python3的除法返回小数,整除只返回商

# python2除法
5 / 2 = 2
5.0 / 2 = 2.5

# 整除
5 // 2 = 2
5.0 // 2 = 2.0

#####################
# python3除法
5 / 2 = 2.5
5.0 / 2 = 2.5

# 整除
5 // 2 = 2
5.0 // 2 = 2.0

持续更新...

相关文章
相关标签/搜索