zoukankan      html  css  js  c++  java
  • python2与python3的不同

    一:输入的不同

    python2:

    raw_input("请输入你的名字")

    python3:

    input("请输入你的名字")

    python2中的input功能:将用户的输入的东西进行了计算或进行了其他的处理
    python3中的input功能:将用户输入的内容当做一个字符串进行处理
    所以python2中要用raw_input(),python3中用input()

    二:输出的不同

    python2中print是一条语句,把print后面的内容整体输出,python3中print是一个函数存在,只输出括号里面的内容

    python2:

    print("hello", "world")
    
    ('hello', 'world')

    python3:

    print("hello", "world")
    
    
    'hello', 'world'

    三:编码不同

    python2默认编码是asscii编码,python3使用utf-8编码

    python2:

    sys.getdefaultencoding()

    'ascii'

    python3:

    sys.getdefaultencoding()

    'utf-8'

    四:True和false

    python2中True和False分别是两个全局变量,分别对应1和0,不过既然是变量,就可以指向其他的值,容易造成混乱,像下面这样。

    >>> True = False
    >>> True
    False
    >>> True is False
    True
    >>> False = "x"
    >>> False
    'x'

    python3中,True和False是两个关键字,永远指向1和0,不允许在被重新赋值

    >>> False = 1
      File "<stdin>", line 1
    SyntaxError: can't assign to keyword
    >>> True =  0
      File "<stdin>", line 1
    SyntaxError: can't assign to keyword

    五:整除运算

    在python 2.x中/除法就跟我们熟悉的大多数语言,比如Java啊C啊差不多,整数相除的结果是一个整数,把小数部分完全忽略掉,浮点数除法会保留小数点的部分得到一个浮点数的结果。

    在python 3.x中/除法不再这么做了,对于整数之间的相除,结果也会是浮点数。

    python2:

    >>> 1 / 2
    0
    >>> 1.0 / 2.0
    0.5

    python3:

    >>> 1/2
    0.5

    六:range与xrange

    在python2中,有range和xrange之分, range返回的是一个列表,xrange返回的是一个生成器。所以xrange做循环的性能比range好,因为不用一次开辟很大的空间,尤其是返回很大的时候。

    在python3中,range()这种实现被移除了,保留了xrange()的实现,且将xrange()重新命名成range()。所以Python3不能使用xrange。

    python2:

    >>> range(5)
    [0, 1, 2, 3, 4]
    >>> xrange(5)
    xrange(5)
    >>>

    python3:

    >>> range(6)
    range(0, 6)

    :不等运算符

    Python 2.x中不等于有两种写法 != 和 <>

    Python 3.x中去掉了<>, 只有!=一种写法。

    八:字典

    字典的 dict.keys()、dict.values() 方法在python2中返回的是一个列表,python3中返回的是一个类似迭代器的"view" 对象。

    python2:

    >>> d = {"name":"zhangsan","age":88}
    >>> d.keys()
    ['age', 'name']
    >>> d.values()
    [88, 'zhangsan']
    >>>

    python3:

    >>> d = {"name":"zhangsan","age":88}
    >>> type(d.keys())
    <class 'dict_keys'>
    >>> d.keys()
    dict_keys(['name', 'age'])
    >>> type(d.values())
    <class 'dict_values'>
    >>> d.values()
    dict_values(['zhangsan', 88])
    >>>

    九:nonlocal

    在函数里面可以用关键字 global 声明某个变量为全局变量,

    但python2中,在嵌套函数中,想要给一个变量声明为非局部变量是没法实现的,在Pyhon3,新增了关键字 nonlcoal,使得非局部变量成为可能。

    python2:

    def func():
        c = 1
        def foo():
            c = 12
        foo()
        print(c)
    func()

    输出:1

    python3:

    def func():
        c = 1
        def foo():
            nonlocal c
            c = 12
        foo()
        print(c)
    func()

    输出:12

    十:打开文件

    python2可以使用file和open,python3只能使用open

    python2:

    >>> file(r"E:Flow classificationTransformImageResult
    esult44.txt")
    <open file 'E:\Flow classification\TransformImage\Result\result44.txt', mode 'r' at 0x0000000002AE41E0>
    >>> open(r"E:Flow classificationTransformImageResult
    esult44.txt")
    <open file 'E:\Flow classification\TransformImage\Result\result44.txt', mode 'r' at 0x0000000002AE4150>

    python3:

    >>> open(r"E:Flow classificationTransformImageResult
    esult44.txt")
    <_io.TextIOWrapper name='E:\Flow classification\TransformImage\Result\result44.txt' mode='r' encoding='cp936'>
    >>> file(r"E:Flow classificationTransformImageResult
    esult44.txt")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'file' is not defined

    十一:map、filter 和 reduce

    这三个函数号称是函数式编程的代表。在 Python3.x 和 Python2.x 中也有了很大的差异。

    首先我们先简单的在 Python2.x 的交互下输入 map 和 filter、reduce,看到它们两者的类型是 built-in function(内置函数):

    >>> map
    <built-in function map>
    >>> filter
    <built-in function filter>
    >>> reduce
    <built-in function reduce>
    >>>

    并且他们的返回类型都是列表。

    >>> map(lambda x:x *2, [1,2,3])
    [2, 4, 6]
    >>> filter(lambda x:x %2 ==0,range(10))
    [0, 2, 4, 6, 8]
    >>>

    但是在Python 3.x中它们却不是这个样子了,对于比较高端的 reduce 函数,它在 Python 3.x 中已经不属于 built-in 了,被挪到 functools 模块当中。

    >>> map
    <class 'map'>
    >>> filter
    <class 'filter'>
    >>> reduce
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'reduce' is not defined
    >>>

    首先它们从函数变成了类,其次,它们的返回结果也从当初的列表成了一个可迭代的对象, 我们尝试用 next 函数来进行手工迭代:

    >>> f =filter(lambda x:x %2 ==0, range(10))
    >>> next(f)
    0
    >>> next(f)
    2
    >>> next(f)
    4
    >>> next(f)
    6
    >>>

    ***************不积跬步无以至千里***************

  • 相关阅读:
    [leetcode]5最长回文子串
    [leetcode]4寻找两个有序数组的中位数
    [leetcode]3无重复字符的最长字串
    [leetcode]2两数相加
    [leetcode]1两数之和
    [学习记录]堆
    [学习记录]平衡树
    [学习记录]二叉树删除
    [学习记录]排序算法总结
    创建mysql数据库
  • 原文地址:https://www.cnblogs.com/liangshian/p/11794512.html
Copyright © 2011-2022 走看看