Reserved words in Python
Python has a list of keywords which it uses for its syntax and internal processing. We can’t use these reserved words in our program as variable names or function names or as identifiers . Here is the list . ( this may change with different versions of Python )
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
Python reserved words list using keyword module with methods to check words
VIDEO
How to know if the name is included in the reserved key word list or not ?
We are checking the key word ( variable name ) nonlocal
import keyword
print(keyword.iskeyword('nonlocal')) # True
Display the full list of reserved keywords
We can display full list of reserved keywords in Python ( current version )
import keyword
print(keyword.kwlist)
To know the number of words in the reserved keyword list
print(len(keyword.kwlist)) # 35 ( may change )
If we use any of the above listed words as variable then we will get the error message SyntaxError: invalid syntax
def is a reserved word ( for defining functions ) This line will generate error
def=45
SyntaxError: invalid syntax
« variables & identifiers in Python
Built in Functions in Python»
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com
Python programming Basics ⇩