python3和python2的区别

1、核心类差异

  • Python3 对 Unicode 字符的原生支持。
      Python2 中使用 ASCII 码作为默认编码方式导致 string 有两种类型 str 和 unicode,Python3 只Python2 中使用 ASCII 码作为默认编码方式导致 string 有两种类型 str 和 unicode,Python3 只
    支持 unicode 的 string。Python2 和 Python3 字节和字符对应关系为:
    python2python3表现转换作用
    strbytes字节encode存储
    unicodestr字符decode显示
  • Python3 采用的是绝对路径的方式进行 import。
      Python2 中相对路径的 import 会导致标准库导入变得困难,Python3 中这一点将被修改,如果还需要导入同一目录的文件必须使用绝对路径,否则只能使用相关导入的方式来进行导入。
  • Python2中存在老式类和新式类的区别,Python3统一采用新式类。新式类声明要求继承object,必须用新式类应用多重继承。
  • Python3 使用更加严格的缩进。Python2 的缩进机制中,1 个 tab 和 8 个 space 是等价的,所以在缩进中可以同时允许 tab 和 space 在代码中共存。这种等价机制会导致部分 IDE 使用存在问题。Python3 中 1 个 tab 只能找另外一个 tab 替代,因此 tab 和 space 共存会导致报错:TabError: inconsistent use of tabs and spaces in indentation.

2、废弃类差异

  1. print 语句被 Python3 废弃,统一使用 print 函数
  2. exec 语句被 python3 废弃,统一使用 exec 函数
  3. execfile 语句被 Python3 废弃,推荐使用 exec(open("./filename").read())
  4. 不相等操作符"<>“被 Python3 废弃,统一使用”!="
  5. long 整数类型被 Python3 废弃,统一使用 int
  6. xrange 函数被 Python3 废弃,统一使用 range,Python3 中 range 的机制也进行修改并提高了
    大数据集生成效率
  7. Python3 中这些方法再不再返回 list 对象:dictionary 关联的 keys()、values()、items(),zip(),
    map(),filter(),但是可以通过 list 强行转换:
mydict={"a":1,"b":2,"c":3}
mydict.keys() #<built-in method keys of dict object at 0x000000000040B4C8>
list(mydict.keys()) #['a', 'c', 'b']
  1. 迭代器 iterator 的 next()函数被 Python3 废弃,统一使用 next(iterator)
  2. raw_input 函数被 Python3 废弃,统一使用 input 函数
  3. 字典变量的 has_key 函数被 Python 废弃,统一使用 in 关键词
  4. file 函数被 Python3 废弃,统一使用 open 来处理文件,可以通过 io.IOBase 检查文件类型
  5. apply 函数被 Python3 废弃
  6. 异常 StandardError 被 Python3 废弃,统一使用 Exception

3、修改类差异

  1. 浮点数除法操作符“/”和“//”的区别
    “ / ”:
    Python2:若为两个整形数进行运算,结果为整形,但若两个数中有一个为浮点数,则结果为
    浮点数;
    Python3:为真除法,运算结果不再根据参加运算的数的类型。
    “//”:
    Python2:返回小于除法运算结果的最大整数;从类型上讲,与"/"运算符返回类型逻辑一致。
    Python3:和 Python2 运算结果一样。

  2. 异常抛出和捕捉机制区别
    Python2

    1. raise IOError, “file error” #抛出异常
    2. except NameError, err: #捕捉异常

    Python3

    1. raise IOError(“file error”) #抛出异常
    2. except NameError as err: #捕捉异常
  3. for 循环中变量值区别
    Python2,for 循环会修改外部相同名称变量的值

i = 1
print'comprehension: ', [i for i in range(5)]print'after: i =', i ) #i=4

Python3,for 循环不会修改外部相同名称变量的值

i = 1
print'comprehension: ', [i for i in range(5)]print'after: i =', i ) #i=1
  1. round 函数返回值区别
  • Python2,round 函数返回 float 类型值
    • isinstance(round(15.5),int) #True
  • Python3,round 函数返回 int 类型值
    • isinstance(round(15.5),float) #True
  1. 比较操作符区别
    Python2 中任意两个对象都可以比较
11 < 'test' #True

Python3 中只有同一数据类型的对象可以比较

11 < 'test' # TypeError: unorderable types: int() < str()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值