1. Home
  2. Docs
  3. Python 101 – Python Programming for Beginners
  4. Keywords

Keywords

Keywords in Python

Like in any other programming language, Python also has set of keywords which are reserved.

You cannot use the keywords as variable name, function name, class name or any other identifier.

List of keywords

False      class      finally    is         return
None       continue   for        lambda     try
True       def        from       nonlocal   while
and        del        global     not        with
as         elif       if         or         yield
assert     else       import     pass
break      except     in         raise

 

The list may vary from version to version.

But you can check, list of keywords for the installed Python version with
the below code snippet.

 

How to check version of python ?

C:\Users\PC-USER>python3 --version
Python 3.6.3

 

How to get list of keywords of installed Python version ?

>>>import keyword

>>>print(keyword.kwlist)

Output:

['False', 'None', 'True', 'and', 'as', 'assert', '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']
>>>

 

What happens if you use a keyword as a variable ?

you will get a syntax error.

Example:

>>> nonlocal = 20  # nonlocal is keyword used as variable  File "<stdin>", line 1  nonlocal = 20  ^ SyntaxError: invalid syntax >>>

 

>>> True = 0   # True is keyword and trying to change its behavior  File "<stdin>", line 1 SyntaxError: can't assign to keyword >>>