1. 程式人生 > >Python程式設計:檢視python語法中的關鍵字keyword

Python程式設計:檢視python語法中的關鍵字keyword

python2

$ workon py2

(py2)$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import keyword

>>> dir(keyword)
['__all__', '__builtins__', '__doc__'
, '__file__', '__name__', '__package__', 'iskeyword', 'kwlist', 'main'] >>> 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'] >>> len(keyword.kwlist) 31 >>> keyword.iskeyword("yield") True

python3

$ workon py3

(py3)$ python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help"
, "copyright", "credits" or "license" for more information. >>> import keyword >>> 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'] >>> len(keyword.kwlist) 33

so: python2中的關鍵字:31個 python3中的關鍵字:33個