python2与python3的区别

发布时间:2019-09-06 08:51:59编辑:auto阅读(1894)

    说明:标注?????是暂时没遇到且看不懂的,做个标记。常见的区别有print,range,open,模块改名,input,整除/,异常 except A as B

    为了不带入过多的累赘,Python 3.0在设计的时候没有考虑向下相容。过渡版本Python2.6基本使用了Python 2.x的语法和库,同时考虑了向Python 3.0的迁移,允许使用部分Python 3.0的语法与函数。

    目录

    新增nonlocal在闭包中改变临时变量 

    Unicode编码存储字符串

    数据类型新增bytes

    除法/不需要转float

    异常捕获 加as

    range

    八进制表示   只能0o1000

    不等运算符 只能!=

    去掉了repr表达式``

    模块改名

     字典的关键字 用属性代替函数

    从键盘键入字符串input

    map、filter、reduce

    打开文件 open

    chr( K ) 与 ord( c )的范围

    字节数组对象bytearry


    • 新增nonlocal在闭包中改变临时变量 

    python2没有nonlocal关键字,要修改临时变量只能将其改成可变数据类型,如数组。b=[a]

    • print加()

    print()函数代替print语句

    • Unicode编码存储字符串

     Python 3加入 Unicode 字符串,用以编码存储字符串。比如用 utf-8可以用来输入中文

    • 数据类型新增bytes

     Python 3去掉long类型,新增了bytes。可以看成是“字节数组”对象,每个元素是 8-bit 的字节,取值范围 0~255。

    在 python 3中字符串以 unicode 编码存储,当写入二进制文件时,字符串无法直接写入writr(或读取),必须以某种方式的编码(encode编码/decode解码)为字节序列后,方可写入。换句话说str类型的字符串无法write进文件,要将str字符串encode为bytes才能write

    a='asdfg'
    print(type(a))
    b=a.encode()
    print(type(b))
    c=b'qwe'
    print(type(c))
    d=c.decode()
    print(type(d))

    运行结果:

    <class 'str'>
    <class 'bytes'>
    <class 'bytes'>
    <class 'str'>

    注意:type类型的数据不能当成普通的int型去修改

    s="ABCD"
    b=s.encode("gbk")
    print (b[0])       # 输出65
    b[0] = 66 #TypeError: 'bytes' object does not support item assignment

     

    • 除法/不需要转float

     Python 3整数之间做除法可以得到浮点数的结果,不需要进行数据格式转换1/2=0.5

     Python 2整数int间除法结果为把运算结果去尾的整数1/2=0,3/2.0=1.5

    • 异常捕获 加as

     Python 3 中   except exc as var

     Python 2中     except exc , var

    ?????????????

    • range

     Python 3 中 range()

    Python 2 中 xrange()

    ?????????????

     

    • 八进制表示   只能0o1000

     Python 2 中 0o1000 或者01000

     Python 3 中 只能0o1000

    • 不等运算符 只能!=

     Python 2 中 != 或者<>

     Python 3 中 只能!=

    • 去掉了repr表达式``

    ?????????????

    • 模块改名

    ????????????????

    线程模块:Python 2 中 thread,Python 3 中_thread

    •  字典的关键字 用属性代替函数

    Python 3去掉iterkeys()、 dict.has_key(),用.keys()、.items 和.values()方法返回迭代器

    dict1={'a':1,"b":2,"c":3}
    print(dict1.keys())
    print(dict1.values())
    print(dict1.items())

    运行结果:

    dict_keys(['a', 'b', 'c'])
    dict_values([1, 2, 3])
    dict_items([('a', 1), ('b', 2), ('c', 3)])

    • 从键盘键入字符串input

    Python 2 中 raw_input("提示信息")用以输入字符串     ; input()用以输入数字

     Python 3 中input("提示信息")将所有输入默认为字符串

    • map、filter、reduce

     Python 2 中 map、filter是内置函数,输出为列表

     Python 3 中 map、filter是类,返回可迭代的对象,可用next()进行迭代

    “对于比较高端的 reduce 函数,它在 Python 3.x 中已经不属于 built-in 了,被挪到 functools 模块当中。”

    • 打开文件 open

     Python 2 中  file(。。。)或oen(。。。)

     Python 3 中  只能open(。。。)

    • chr( K ) 与 ord( c )的范围

    ???????????????

    python 2.4.2以前

       chr( K )   将编码K 转为字符,K的范围是 0 ~ 255

       ord( c )   取单个字符的编码, 返回值的范围: 0 ~ 255

    python 3.0

       chr( K )   将编码K 转为字符,K的范围是 0 ~ 65535

       ord( c )   取单个字符的编码, 返回值的范围: 0 ~ 65535

    • 字节数组对象bytearry

    python3新增,将多个bytes字节类型数据组成数组。

    (1) 初始化

        a = bytearray(   10 )

         # a 是一个由十个字节组成的数组,其每个元素是一个字节,类型借用 int

         # 此时,每个元素初始值为 0

    (2) 字节数组 是可变的

        a = bytearray(   10 )

         a[0] = 25

         # 可以用赋值语句更改其元素,但所赋的值必须在 0 ~ 255 之间

    (3)   字节数组的切片仍是字节数组

    (4)   字符串转化为字节数组

         #coding=gbk

         s ="你好"

         b = s.encode( "gbk")     # 先将字符串按某种“GBK”编码方式转化为 bytes

         c = bytearray( b )          #再将 bytes 转化为 字节数组

         也可以写作

         c = bytearray( "你好", "gbk")

    (5)   字节数组转化为字符串

          c = bytearray( 4 )

           c[0] = 65 ; c[1]=66; c[2]= 67; c[3]= 68

          s = c.decode( "gbk" )

           print ( s )

          # 应显示: ABCD           

    (6) 字节数组可用于写入文本文件

    #coding=gbk

    f = open("c:\\1234.txt", "wb")
    s = "张三李四abcd1234"
    # -------------------------------
    # 在 python2.4 中我们可以这样写:
    # f.write( s )
    # 但在 python 3.0中会引发异常
    # -------------------------------
    b = s.encode("gbk")

    f.write( b )
    c=bytearray( "王五","gbk")
    f.write( c )
    f.close()

    input("?")

     

     

     

    参考网址:

    http://www.runoob.com/python/python-2x-3x.html

    https://www.cnblogs.com/hanggegege/p/5840005.html

     

     

     

     

     

关键字