import keyword
keyword.kwlist

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

print(keyword.iskeyword("yield"))  # testing with a word that is a keyword
keyword.iskeyword("queue")  # testing with a word that is not a keyword

"""
Explanation: <div align="right">Python 3.6 [PY36 Env]<br/>
Python 2.7 [PY27_Test Env]</div>

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.
Related Links

Keyword Lists online:
Python org - Python 2 Keywords
Python org - Python 3 Keywords
programmiz.com Python3 Keyword List - w hyperlinks to explanations
PY2: import keyword library and commands
PY36
Built-in Functions on Python.org
Reference Index for Python 3

Tests in Python 3.6
End of explanation
"""


import keyword
keyword.kwlist

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

print(keyword.iskeyword("yield"))  # testing with a word that is a keyword
keyword.iskeyword("queue")  # testing with a word that is not a keyword

# 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

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

# 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)

"""
Explanation: Tests in Python 2.7
Same tests performed in Python 2.7
End of explanation
"""


# 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)

"""
Explanation: 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.
End of explanation
"""


help("assert")

help("yield")

"""
Explanation: 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:
End of explanation
"""


