DEFAULT OR RESERVED KEYWORDS IN PYTHON

Keywords in python are the reserved words which cannot be customized for individual variables.

Python Interpreter throws “SyntaxError: invalid syntax” error during parse phase if a variable is declared with a reserved keyword. Each keyword has its own purpose in python which are system related internal code.

help(keyword.kwlist)

Code:

#import keywords
import keyword

#Print them
print("Reserved keywords are: ")
for resrvd_words in keyword.kwlist:
    print(resrvd_words)

Output:

False
None
True
__peg_parser__
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

A simple function to check reserved keywords. keyword has a function iskeyword() which takes one argument for word to check if the entered string is keyword or not.

#Library for keyword 
import keyword
#w = ['abc','efg','True']

#Function which checks if the entered string is keyword or not
def _CRKW_(w):
    if keyword.iskeyword(w):
        return "Reserved keywords"
    else:
        return "Non reserved words"
            
_CRKW_('T')

Leave a Reply