How to Create a List of All Keywords in Python?

Estimated read time 1 min read

In Python, you can create a list of all keywords by using the keyword module, which provides a list of all Python keywords. Here’s an example:

import keyword

# Create a list of all keywords
all_keywords = keyword.kwlist
print(all_keywords)

In this example, we first import the keyword module. We then use the kwlist attribute provided by the keyword module to get a list of all Python keywords. We assign this list to the variable all_keywords and print it using the print() function.

The output of this program would be:

['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']

These are all the keywords in Python, including the built-in constants False, None, and True.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply