Last Updated : 12 Jul, 2025
Pythonprovides an in-built module
keywordthat allows you to know about the reserved keywords of python. The keyword module allows you the functionality to know about the reserved words or keywords of Python and to check whether the value of a variable is a reserved word or not. In case you are unaware of all the keywords of Python you can use this module to retrieve this information. Also, it helps you to check whether a word is a keyword or not just by using its functions on Python shell mode.
The functions of this module are:
# Program to check whether a given
# word is a Python keyword or not
import keyword
s ="if"
t ="in"
u ="GeeksforGeeks"
# using iskeyword() function to check
print(s, "is a keyword in Python:",
keyword.iskeyword(s))
print("lambda is a keyword in Python:",
keyword.iskeyword("lambda"))
print("print is a keyword in Python:",
keyword.iskeyword("print"))
print(t, "is a keyword in Python:",
keyword.iskeyword(t))
print(u, "is a keyword in Python:",
keyword.iskeyword(u))
Output:
if is a keyword in Python: True lambda is a keyword in Python: True print is a keyword in Python: False in is a keyword in Python: True GeeksforGeeks is a keyword in Python: FalseAs you can see from the above example that the value of variable s and t is a keyword in Python, thus the function returns True. Similarly, the string GeeksforGeeks is not a keyword, thus the function returns False.
# Program to display the list of Python keywords
# importing keyword module
import keyword
# using keyword.kwlist to display the list of keywords
print(keyword.kwlist)
Output:
['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']Note: keyword.kwlist is not a function, hence no parenthesis are used with it. kwlist is a variable defined previously in the keyword module.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4