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.
In [1]:
import keyword
keyword.kwlist
Out[1]:
In [7]:
kw1 = keyword.kwlist
print("Number of Keywords: %d" %len(kw1))
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
Out[5]:
In [4]:
import keyword
keyword.kwlist
Out[4]:
In [5]:
kw1 = keyword.kwlist
print("Number of Keywords: %d" %len(kw1))
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
Out[6]:
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
In [8]:
help("keywords") # note: this list is also 31 words and does not list True/False/None
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)
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)
This solves the discrepencies. Python 3.6 had a keyword count of 33 while Python 2.7 had a count of 31:
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
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")
In [17]:
help("yield")
In [ ]: