Python keywords are reserved words that have a special meaning associated with them and can’t be used for anything but those specific purposes.
What is keyword in Python?
Python keywords are reserved words that have a special meaning associated with them and can’t be used for anything but those specific purposes. Each keyword is designed to achieve specific functionality.
Python keywords are case-sensitive.
- All keywords contain only letters (no special symbols)
- Except for three keywords (
True,False,None), all keywords have lower case letters
Get the List of Keywords
As of Python 3.9.6, there are 36 keywords available. This number can vary slightly over time.
We can use the following two ways to get the list of keywords in Python
- keyword module: The keyword is the buil-in module to get the list of keywords. Also, this module allows a Python program to determine if a string is a keyword.
help()function: Apart from a keyword module, we can use thehelp()function to get the list of keywords
Example: keyword module
import keyword
print(keyword.kwlist)All the keywords except, True, False, and None, must be written in a lowercase alphabet symbol.
Example 2: The help() function
help("keywords")Understand Any keyword
The python help() function is used to display the documentation of modules, functions, classes, keywords.
Pass the keyword name to the help() function to get to know how to use it. The help()function returns the description of a keyword along with an example.
Let’s understand how to use the if keyword.
print(help('if'))Usually, IDE should identify the keywords in a specific colour, and you should take note of that. In the example above, note how the keyword (if, elif, and else) were all identified with the same colour.
Keyword Module
Python keyword module allows a Python program to determine if a string is a keyword.
iskeyword(x): Returns True if x is a keyword
Example if is a keyword…
import keyword
print(keyword.iskeyword('if'))
print(keyword.iskeyword('range'))As you can see in the output, it returned True because ‘if’ is the keyword, and it returned False because the range is not a keyword (it is a built-in function).
Also, keyword module provides following functions to identify keywords.
keyword.kwlist:It return a sequence containing all the keywords defined for the interpreter.keyword.issoftkeyword(s): ReturnTrueif s is a Python soft keyword. New in version 3.9keyword.softkwlist: Sequence containing all the soft keywords defined for the interpreter. New in version 3.9
Types of Keywords
Python 3.10+ has 35 official keywords, but the categorization of keywords can indeed be expanded into specific usage contexts for clarity:
- Value Keywords: These represent built-in values.
- Keywords:
True,False,None - Logical Operator Keywords: Keywords used for logical operations.
- Keywords:
and,or,not,is,in - Conditional Keywords: Keywords for conditionals and conditional structures.
- Keywords:
if,elif,else - Loop and Flow Control Keywords: For defining and controlling loops and flow within loops.
- Keywords:
for,while,break,continue,else - Function and Class Definition Keywords: For defining functions, classes, context managers, and lightweight functions.
- Keywords:
def,class,lambda - Context Management Keywords: For managing resources (often in I/O operations).
- Keywords:
with,as,pass - Import Keywords: Keywords for module importing and aliasing.
- Keywords:
import,from,as - Return Keywords: For returning values or generator functionality.
- Keywords:
return,yield - Exception Handling Keywords: Keywords for handling exceptions and enforcing conditions.
- Keywords:
try,except,raise,finally,else,assert - Variable Scope and Memory Management Keywords: Keywords for managing variable scope and memory.
- Keywords:
del,global,nonlocal - Asynchronous Programming Keywords: Keywords for asynchronous execution and managing coroutines.
- Keywords:
async,await