一、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个关键字
[[email protected] ~]$ 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个关键字。