Reference to Python Keywords
Python includes a set of reserved words called keywords that are essential to writing programs. These words have special meanings and cannot be used as names for variables, classes, functions, or other identifiers. The Python interpreter relies on these keywords to understand and structure the code.
This guide provides a complete and up-to-date list of all Python reserved and soft keywords, serving as a definitive reference for developers.
Alphabetical Reference
The following table lists all 35 Python keywords in alphabetical order.
Keyword | Description |
---|---|
and | A logical operator |
as | Creates an alias |
assert | For debugging |
async | Define asynchronous functions |
await | Suspend or pause the execution of asynchronous functions to perform the awaited operation |
break | To break out of a loop |
class | To define a class |
continue | Unlike break, continues to the next iteration in the loop |
def | Define a function with this keyword |
del | Delete an object with this keyword |
elif | Short for else if, used in conditional statements |
else | Used in conditional statements |
except | Used with exceptions, what to do when an exception occurs |
False | Boolean value, used for comparison operations |
finally | Used with exceptions, executes a block even with exception |
for | To create a for loop |
from | To import specific parts of a module |
global | Declares a global variable |
if | Makes a conditional statement |
import | Imports a module |
in | It checks if a value is present in a list, tuple, etc. |
is | To test if two variables refer to the same object |
lambda | Creates an anonymous function |
None | Represents a null value |
nonlocal | Declares a non-local variable |
not | A logical operator |
or | A logical operator |
pass | A null statement, a statement that will do nothing |
raise | Raises an exception |
return | To exit a function and return a value |
True | Boolean value, used for comparison operations |
try | Used with except statement |
while | Creates a while loop |
with | Used to simplify exception handling |
yield | Produces a value from a generator, suspending function state |
Dynamically Get All Python Keywords
In Python programming, you can dynamically retrieve the complete list of keywords using simple scripts. Below are two methods to obtain all the reserved keywords available in the Python language.
Use the following code snippet to get the up-to-date collection of Python reserved words.
Method 1
import keyword # Get the list of all keywords python_keywords = keyword.kwlist print(python_keywords)
Method 2
# Get a complete keyword list help() # Run this command # Upon Prompt help> keywords
Python Soft Keywords List
Certain Python reserved words, called soft keywords, are reserved only in specific contexts and available from Python 3.10 onward. Below is the list of these context-sensitive soft keywords.
Keyword | Description |
---|---|
_ | A placeholder variable in Python |
match | A pattern matching expression that allows handling of complex conditional statements |
case | Used within the match to define specific patterns |
type | Used for type hinting to get data types of function parameters |
Dynamically Get All Python Soft Keywords
Use the following code snippet to get the up-to-date collection of soft keywords.
Get Soft-Keywords
# Get soft keywords list with null safety import keyword soft_keywords = getattr(keyword, 'softkwlist', None) if soft_keywords is not None: print(soft_keywords) else: print("Soft keywords list not available in this Python version.")