Identifiers and Keywords

Learn about the identifiers and keywords used in Python.

We'll cover the following

Identifier

Python is a case-sensitive language. In Python, an identifier is a name used to identify a variable, function, class, module, or any other object.

The rules for creating identifiers are:

  • Start with an alphabet or an underscore.
  • Follow by zero or more letters, an underscore ( _ ), and digits.
  • Keywords cannot be used as an identifier.

Some examples of creating valid identifiers are shown below:

Press + to interact
# Valid identifiers examples
valid = 2
Valid02 = "abc"
_valid_3 = 8.0

We can’t start an identifier with a digit, as shown below:

Press + to interact
# Invalid identifier example
1notValid = 4

We face an error by using the keyword as an identifier, as shown below:

Press + to interact
True = "abc"

Python keywords

Python keywords are reserved words with specific meanings and purposes in the language. They cannot be used as identifiers and are essential for defining code structure, controlling execution flow, and performing operations. Keywords have predefined functionality and cannot be altered.

We can print a list of Python keywords through the following statements:

Press + to interact
import keyword # makes the module 'keyword' available
print(keyword.kwlist) # syntax modulename.object/function