Keywords are special words which comes built in a programming language. They are used to perform basic operations which the programmer will need frequently such as looping, or for performing special and important actions in the language. Together with identifiers, constants and language symbols , keywords forms the building blocks of statements in a language.
Python like many other languages comes with it's inbuilt set of keywords. we can use these keywords directly without any special actions like importing them. It is worth noting that Keywords in Python are different from builtin functions, for example print()
is not a keyword it is a builtin function.
The types in Python are also not categorized as keywords, they may share much in common like coming inbuilt into the language, but keywords have some special features which sets them apart. For example it is illegal to assign keywords to variables or pass them to functions and doing this will raise a SyntaxError
, but it is perfectly legal with types though it is still not a very good idea. The reason why you cannot assign keywords to variables or pass them to functions is because they have no value, they are simply tools to aid in program development. Types on the other hand, are literally Python classes and they, therefore, have value.
Keywords available in Python
As of now, Python keywords totals to 35 even though more keywords might be added and some removed in future versions of Python. For example the async
and the await
keywords are relatively new and they didn't exist until Python 3.7 also print
and exec
are keywords in Python 2 but were removed from keywords in python 3.
The 35 keywords available in the current versions of Python as of now are:
False |
await |
else |
import |
None |
break |
except |
in |
True |
class |
finally |
is |
and |
continue |
for |
lambda |
as |
def |
from |
nonlocal |
assert |
del |
global |
not |
async |
elif |
if |
yield |
pass |
raise |
return |
or |
try |
while |
with |
IDEs/Editors usually highlights keywords differently.This can help a programmer to identify them easily and avoid using them incorrectly.
You can use the builtin
help()
function to get more details about the keywords, for example to view all the keywords available in your version of Python you can pass "keywords" as the argument to the help()
function as shown below.
You can also pass the name of a specific keyword to get more information about it. For example:
The keyword module
The Python standard library also comes with a builtin module called keyword , you can use this module to work with the keywords programmatically. This module provides four utilities to work with keywords, these are
- kwlist
- iskeyword()
- softkwlist
- issoftkeyword()
The kwlist
as the name tries to suggest is a list containing all the keywords present in the Python version.
To check whether a given word is a keyword , you can use the iskeyword()
function in the module like:
Soft keywords.
From Python 3.10 onward, two keywords were introduced, match
and case
. These keywords are not yet supported as full keywords and the language refers to them as soft
Keywords. They can be used as normal words , like being assigned to variables or as any valid identifier.
The softkwlist
in the keyword module is a list containing the soft keywords while the issoftkyword()
function can be used to check whether a given word is a soft keyword.
You can check on Python's Implementation of Switch-Case , to see a detailed exploration of match
and case
keywords in python.
Categorizing Python keywords
We can group the 35 keywords according to their purpose and usage as follows:
Value Keywords | |||||
True |
False |
None |
|||
Operator Keywords | |||||
and |
or |
not |
in |
is |
|
Control Flow Keywords | |||||
if |
elif |
else |
|||
Iteration Keywords | |||||
for |
while |
break |
continue |
else |
|
Structure Keywords | |||||
def |
class |
with |
as |
pass |
lambda |
Returning Keywords | |||||
return |
yield |
||||
Import Keywords | |||||
import |
from |
as |
|||
Exception-Handling Keywords | |||||
try |
except |
raise |
finally |
else |
assert |
Asynchronous Keywords | |||||
async |
await |
||||
Variable Handling Keywords | |||||
del |
global |
nonlocal |