In this tutorial, we are going to learn how to get all the keywords that are present in Python. It's straightforward. We have a module called the keyword module, which contains a list of keywords in the kwlist
variable. Hence to get a list of keywords in python we can use the following variable.
Steps to get the List of Keywords
-
Import the keyword module.
-
Print the variable keyword.kwlist
to get a list of keywords.
Let's see the code.
Here's the code:
# importing the keyword module
import keyword
# getting the list of keywords
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']
Conclusion
If you got any doubts while following the tutorial, mention them in the comment section.