Python 3.6 [PY36 Env]
Python 2.7 [PY27_Test Env]

Python Keyword List

Content in this notebook uses the keywords library and commands accessible within Python to show how to list and get help on keywords. It also uses code to analyze differences between Python 2 and Python 3. The related links at the top is concerning related help on keywords and related topics.

Tests in Python 3.6


In [1]:
import keyword
keyword.kwlist


Out[1]:
['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']

In [7]:
kw1 = keyword.kwlist
print("Number of Keywords: %d" %len(kw1))


Number of Keywords: 33

In [5]:
print(keyword.iskeyword("yield"))  # testing with a word that is a keyword
keyword.iskeyword("queue")  # testing with a word that is not a keyword


True
Out[5]:
False

Tests in Python 2.7

Same tests performed in Python 2.7


In [4]:
import keyword
keyword.kwlist


Out[4]:
['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 [5]:
kw1 = keyword.kwlist
print("Number of Keywords: %d" %len(kw1))


Number of Keywords: 31

In [6]:
print(keyword.iskeyword("yield"))  # testing with a word that is a keyword
keyword.iskeyword("queue")  # testing with a word that is not a keyword


True
Out[6]:
False

In [7]:
# False / True are listed as keywords in Python 3.6 but not in 2.7
# at least accodring to keyword.kwlist
# checking them:

True  # highlights like keyword
False # highlights like keyword
None  # highlights like keyword

print(keyword.iskeyword("True"))  # does not know it
print(keyword.iskeyword("False")) # does not know it
print(keyword.iskeyword("None"))  # does not know it
                                  # all were added to keywords list for Python 3.x


False
False
False

In [8]:
help("keywords") # note: this list is also 31 words and does not list True/False/None


Here is a list of the Python keywords.  Enter any keyword to get more help.

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


In [14]:
# final observation:  Keyword counts different b/w Python 2.7 and Python 3.6
#                     This cell looks for PY27 words that are depricated in PY36

py3_list = ['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']   # list copied from earlier run in PY36

common_kw = []
temp = []

for word in kw1:
    temp = [(common_kw.append(word2), word2) for word2 in py3_list if word==word2]
    if len(temp) == 0:
        print("word not found in Python 3.6: %s" %word)

print("Common Keywords between Both Versions of Python:")
print(common_kw)


word not found in Python 3.6: exec
word not found in Python 3.6: print
Common Keywords between Both Versions of Python:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

exec and print were not depricated. They were converted from key words to functions in Python 3.6. This explains the syntax change in print statements that breaks old Python 2.7 code.


In [15]:
# now let's look for the discrepencies the other way.  We already know about True/False/None
# but taken together with exec, print, the counts don't add up yet for the differences

common_kw = []
temp = []

for word in py3_list:
    temp = [(common_kw.append(word2), word2) for word2 in kw1 if word==word2]
    if len(temp) == 0:
        print("word not found in Python 2.7: %s" %word)

print("Common Keywords between Both Versions of Python:")
print(common_kw)


word not found in Python 2.7: False
word not found in Python 2.7: None
word not found in Python 2.7: True
word not found in Python 2.7: nonlocal
Common Keywords between Both Versions of Python:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

This solves the discrepencies. Python 3.6 had a keyword count of 33 while Python 2.7 had a count of 31:

  • Python 2.7 has 31 keywords
  • 2 were converted to functions in Python 3.6 so that gives 3.6 a base of 29
  • 29 + 4 new keywords (added to the list for PY36) and we get 33

However, it should be noted that True, False, None are reserved words in Python 2.7 that simply don't show in its keywords list. This is more of a correction than an addition in Python 3.6

Getting Help on a Keyword

In other languages, documentation often includes a convenient page with hyperlinks for each keyword. Python has this for builtin functions but I could not find it for keywords. Using above code, we can generate the list, and then we can ask for help on any given keyword like this:


In [16]:
help("assert")


The "assert" statement
**********************

Assert statements are a convenient way to insert debugging assertions
into a program:

   assert_stmt ::= "assert" expression ["," expression]

The simple form, "assert expression", is equivalent to

   if __debug__:
       if not expression: raise AssertionError

The extended form, "assert expression1, expression2", is equivalent to

   if __debug__:
       if not expression1: raise AssertionError(expression2)

These equivalences assume that "__debug__" and "AssertionError" refer
to the built-in variables with those names.  In the current
implementation, the built-in variable "__debug__" is "True" under
normal circumstances, "False" when optimization is requested (command
line option -O).  The current code generator emits no code for an
assert statement when optimization is requested at compile time.  Note
that it is unnecessary to include the source code for the expression
that failed in the error message; it will be displayed as part of the
stack trace.

Assignments to "__debug__" are illegal.  The value for the built-in
variable is determined when the interpreter starts.


In [17]:
help("yield")


The "yield" statement
*********************

   yield_stmt ::= yield_expression

The "yield" statement is only used when defining a generator function,
and is only used in the body of the generator function. Using a
"yield" statement in a function definition is sufficient to cause that
definition to create a generator function instead of a normal
function.

When a generator function is called, it returns an iterator known as a
generator iterator, or more commonly, a generator.  The body of the
generator function is executed by calling the generator's "next()"
method repeatedly until it raises an exception.

When a "yield" statement is executed, the state of the generator is
frozen and the value of "expression_list" is returned to "next()"'s
caller.  By "frozen" we mean that all local state is retained,
including the current bindings of local variables, the instruction
pointer, and the internal evaluation stack: enough information is
saved so that the next time "next()" is invoked, the function can
proceed exactly as if the "yield" statement were just another external
call.

As of Python version 2.5, the "yield" statement is now allowed in the
"try" clause of a "try" ...  "finally" construct.  If the generator is
not resumed before it is finalized (by reaching a zero reference count
or by being garbage collected), the generator-iterator's "close()"
method will be called, allowing any pending "finally" clauses to
execute.

For full details of "yield" semantics, refer to the Yield expressions
section.

Note: In Python 2.2, the "yield" statement was only allowed when the
  "generators" feature has been enabled.  This "__future__" import
  statement was used to enable the feature:

     from __future__ import generators

See also:

  **PEP 255** - Simple Generators
     The proposal for adding generators and the "yield" statement to
     Python.

  **PEP 342** - Coroutines via Enhanced Generators
     The proposal that, among other generator enhancements, proposed
     allowing "yield" to appear inside a "try" ... "finally" block.


In [ ]: