How to print all keywords in python


Print all keywords in python


Method 1
We need to import a module named as keyword, present in Python library. We will get a LIST of  keywords.


#Program python_3.7

>>> import keyword
>>> keyword.kwlist

#output
['False', 'None', 'True', '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']



#program python_3.8

>>> import keyword
>>> print(keyword.kwlist)

#Output
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']

There are 2 newly added keywords in python 3.8, async and await, so Python 3.8 supports total 35 keywords. Above example proves the same.

Above code will be helpful to understand, and check the keywords supported in the version used by you.


Method 2:
we can use a python built in function "help()"

>>> help('keywords')

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

False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not




Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India