zoukankan      html  css  js  c++  java
  • python3和2的区别

    1.print函数

    python2中的print语句,被python3中的print()函数取代。
    print 'hello world'
    运行结果:

    python2中同时输出多个对象时,会创建一个元组,因为python2中,print是一个语句,而不是函数调用。

    2.整数除法

    python2

    print '3/2=',3/2
    print '3//2=',3//2
    print '3/2.0=',3/2.0
    print '3//2.0=',3//2.0

    运行结果:

    python3

    print('3/2=',3/2)
    print('3//2=',3//2)
    print('3/2.0=',3/2.0)
    print('3//2.0=',3//2.0)

     3.生成器next()

    python3中,g.next()被重新命名为g.__next__(),可以使用next(g)来执行。

    4.闭包__closure__

    闭包函数会比普通函数多一个__closure__属性,里面定义了一个元组用来存放所有的cell对象,每个cell对象保存了这个闭包中对应的外部变量

    python2中使用func_closure,python3中为__closure__

    ```

    def outer():
    x = 1
    def inner():
    print(x)
    return inner

    result = outer()
    print(result.__closure__)

    ```

    运行结果:

    (<cell at 0x0000000002155618: int object at 0x000000005D28C6B0>,)

    5.dict的items替代python2中的iteritems

    6.

  • 相关阅读:
    字符串对比
    时间转换
    fJ字符串
    Codeforces 1526D
    HDU
    树链剖分入门
    AcWing 252. 树(点分治模版题)
    HDU-4487 Maximum Random Walk(概率dp)
    acwing 316 减操作(dp)
    CodeForces
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/7339159.html
Copyright © 2011-2022 走看看