• Python2.7与3.6的一些区别


    2.7实现了一部分3的功能, 更早版本可能会稍稍涉及一点

    首先是关键字的差别

    python3.6

    import  keyword
    print(keyword.kwlist)
    ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 
    'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 
    'raise', 'return', 'try', 'while', 'with', 'yield']
    

    python2.7

    import keyword
    keyword.kwlist
    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 
    'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 
    'with', 'yield']
    

    python2.4

    ['and', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 
    'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is','lambda', 'not', 
    'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'yield']
    

    这么多看不过来了把, 哈哈

    python36 = {'False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def',
                'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import',
                'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while',
                'with', 'yield'}
    
    python27 = {'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
                'exec', 'finally', 'for','from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or',
                'pass', 'print', 'raise', 'return', 'try', 'while','with', 'yield'}
    
    python24={'and', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
                'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is','lambda', 'not',
                'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'yield'}
    
    
    print(python36-python27) # {'nonlocal', 'False', 'True', 'None'}
    print(python36-python24) # {'False', 'nonlocal', 'None', 'as', 'with', 'True'}
    

    最主要的就是这个nonlocal了, python2中存在False, True, None但是不属于关键字

    语法差异

    异常:

    # python3
    try:
        raise IndexError("傻傻")
    except IndexError as e:
        print(e)
    
    # python2
    try:
        raise IndexError("傻傻")
    except IndexError , e:
        print(e)
    

    其他

    yield from # 好像是3.4
    async和await  # 3.5
    

      

    数据类型

    在python3中str是Unicode编码的字节, bytes是其他字节

    在python2中str默认是ascii编码的字节, Unicode是另一种类型

    # 在pycharm中点击bytes
    bytes = str
    

    bytes: 就是八个二进制的字节, 我们存储和在网络传输中实际上都是字节

    Unicode, GBK, ASCII, UTF8都是将字节翻译成有用的信息, Unicode可以存储所有文字, 可以用他来做一个中间编码来转换其他的编码

    python3

    # 转成字节
    a = "爸爸"
    byte = a.encode("utf8")
    print(byte, type(byte))
    
    byte = bytes(a, encoding="utf8")
    print(byte, type(byte))
    
    # 转成字符串
    st = byte.decode("utf8")
    print(st, type(st))
    
    st = str(byte, encoding="utf8")
    print(st, type(st))
    

    python2

    # _*_ coding:utf8 _*_
    
    # 转成Unicode, 这里用utf8转是因为上面写的
    nui = "我是你爸爸".decode("utf8")
    print nui, type(nui)
    
    # 转成字符串
    st = nui.encode("utf8")
    print st, type(st)
    

    其他

    输入

    # python3
    input() -> str
    
    # python2
    raw_input() -> str
    python2中也有input不过他只能接收数字
    

    输出

    # python3
    print() 
    
    # python2
    print
    

    关于数字

    # <> 运算符在三中被弃用, 比较大多用于数字, 所以就放在这里
    
    # python3中range生成一个数字的生成器, 而在2中直接生成列表. 2中有xrange生成生成器
    
    # 3中弃用了long长整型
    
    # 2中/是整除, 3中则不是, 3中的整除是//
    

      

  • 相关阅读:
    【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell GetCounter
    云边协同架构助力智能工厂视觉 AI 缺陷检测应用构建
    桥接 Mosquitto MQTT 消息至 EMQX
    如何保障物联网平台的安全性与健壮性
    eKuiper Newsletter 202208|多平台插件一键安装,使用更便捷
    使用 Prometheus 监控 eKuiper 规则运行状态
    EMQX Operator 如何快速创建弹性伸缩的 MQTT 集群
    EMQX 5.0 全新网关框架:轻松实现多物联网协议接入
    云原生赋能智能网联汽车消息处理基础框架构建
    MQTT X Newsletter 202208 | v1.8.2 发布、支持使用 Docker
  • 原文地址:https://www.cnblogs.com/wwg945/p/9812498.html
Copyright © 2020-2023  润新知