Python Keywords
Python keywords are reserved words that have special meanings and cannot be used as variable names, function names, or any other identifiers. These keywords help define the syntax and structure of the Python programming language.
01. What are Python Keywords?
- Python keywords are predefined and have specific meanings.
- They cannot be used as variable names, function names, or identifiers.
- Python keywords are case-sensitive.
- As of Python 3.10, there are 35 keywords in Python.
02. List of Python Keywords
Python Keywords | ||||
---|---|---|---|---|
False | await | else | import | pass |
None | break | except | in | raise |
True | class | finally | is | return |
and | continue | for | lambda | try |
as | def | from | nonlocal | while |
assert | del | global | not | with |
async | elif | if | or | yield |
03. Example: Get All Python Keywords
The following Python program retrieves the complete list of keywords using the keyword
module.
Python Code:
import keyword
# Get the list of all Python keywords
keyword_list = keyword.kwlist
# Print the type and the list of keywords
print("Class Type:", type(keyword_list))
print(keyword_list)
# Print the total number of keywords
print("\nTotal Number of Keywords:", len(keyword_list))
Output:
Class Type: <class 'list'>
['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']
Total Number of Keywords: 35
04. Important Information
- Python continuously evolves, and newer versions might introduce additional keywords.
- Keywords like
async
andawait
were introduced in Python 3.5. - To check keywords in your Python version, always use
keyword.kwlist
.
Official Documents:
Comments
Post a Comment