python2和python3的区别

编码

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

python3默认编码方式unicode(可以识别中文)

print

python2中加不加括号都可以打印

python3中必须加括号

input

  • python2 raw_input()
    raw_input()得到了str
    input()得到了int

  • python3 input()
    input()得到了str

range()

python2 range()/xrange()

python3 range(),是可迭代对象

python3中都是新式类

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

继承了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

持续更新...

原文地址:https://www.cnblogs.com/jie9527-/p/11764504.html