zoukankan      html  css  js  c++  java
  • python2和python3中的关键字的区别--keyword模块

    一、python3.6中的:

    共33个关键字:

    通过导入keyword模块,查看python所有的关键字。在ipython中:

    Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1
    900 64 bit (AMD64)]
    Type "copyright", "credits" or "license" for more information.

    IPython 5.1.0 -- An enhanced Interactive Python.
    ? -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help -> Python's own help system.
    object? -> Details about 'object', use 'object??' for extra details.

    In [1]: import keyword

    In [2]: dir(keyword)
    Out[2]:
    ['__all__',
    '__builtins__',
    '__cached__',
    '__doc__',
    '__file__',
    '__loader__',
    '__name__',
    '__package__',
    '__spec__',
    'iskeyword',
    'kwlist',
    'main']

    In [3]: keyword.iskeyword
    Out[3]: <function frozenset.__contains__>

    In [4]: keyword.iskeyword?
    Docstring: x.__contains__(y) <==> y in x.
    Type: builtin_function_or_method

    二、python2.7中的:

    共31个关键字

    [zqf@zengqingfu ~]$ ipython
    Python 2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15)
    Type "copyright", "credits" or "license" for more information.

    IPython 5.1.0 -- An enhanced Interactive Python.
    ? -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help -> Python's own help system.
    object? -> Details about 'object', use 'object??' for extra details.

    In [2]: import keyword

    In [3]: keyword.kwlist
    Out[3]:
    ['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']

    In [4]: dir(keyword)
    Out[4]:
    ['__all__',
    '__builtins__',
    '__doc__',
    '__file__',
    '__name__',
    '__package__',
    'iskeyword',
    'kwlist',
    'main']

    三、区别:

    python3 相比python2的关键字,减 少了print和exec这两个关键字, 但是新增了False,None,True,nonlocal这4个关键字。

  • 相关阅读:
    数学是最好的语言
    人类的语言--自然语言--》逻辑语言
    为什么大脑喜欢看图?
    思考是调用大脑存储的上下文对输入信息进行处理的过程
    Redis的相关问题总结
    TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)
    array_column 函数, 以及在PHP5.5之下的替代方法
    thinkphp在app接口开发过程中的通讯安全认证
    PHP开发APP接口实现--基本篇
    分布式与集群的区别是什么?
  • 原文地址:https://www.cnblogs.com/zengqingfu1442/p/7257645.html
Copyright © 2011-2022 走看看