Python Keywords

About Python Keywords

In Python, keywords are reserved words that have special meanings and purposes. These keywords cannot be used as variable names or function names or any other identifier.

Keywords of Python Language
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

Note: You will learn the use of these keywords in detail in the upcoming tutorials.

Syntax error for using keyword as variable

If you try to use a keyword as a variable name in Python, you'll encounter a syntax error. Python will raise an error indicating the invalid syntax.

Example of syntax error for using keyword as variable:
Output of the above example
 File "", line 1
    class = "Python"
          ^
SyntaxError: invalid syntax
                                  

The above code returns with a syntax error for using class as a variable.

Using keyword as variable

It's generally not recommended to use Python keywords as variable names for the sake of clarity and avoiding confusion.

If it becomes necessary to use a reserved keyword as variable, you can add underscore (_) at the beginning or end of the keyword as demonstrated in the following examples:

Example of using keyword as variable:
Output of the above example
  Python is great!
  Condition
                                

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python variables and check the syntax error raised by the Python interpreter in the following Python code editor and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output