Python Keywords and Identifiers

Reserved words the interpreter owns, and the naming rules for everything you get to name yourself โ€” with every way to break those rules demonstrated.

Overview

This topic covers two foundational building blocks of Python syntax โ€” Keywords and Identifiers. Keywords are reserved words with special meaning to the Python interpreter, while identifiers are the names we choose for variables, functions, classes, etc. Understanding the rules for identifiers helps avoid common SyntaxError early on.

Key Concepts

Detailed Explanation

1. Keywords

A keyword is a word reserved by the language because it has a special meaning. Keywords can act as commands or parameters, and every programming language has its own set of reserved keywords that cannot be used as variable names.

To view all Python keywords programmatically:

# python has 33 keywords
import keyword
print(keyword.kwlist)

Output:

['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']
๐Ÿ’ก Tip: The keyword module is the easiest way to check the current keyword list for whatever Python version you're using, since this list can change slightly between versions.

2. Identifiers

Definition: A Python identifier is a name used to identify a variable, function, class, module, or other object.

Rules for Setting Identifiers

  1. Can only start with an alphabet (letter) or an underscore (_).
  2. Can be followed by 0 or more letters, underscores (_), and digits.
  3. Keywords cannot be used as identifiers.

3. Valid Identifier Examples (Tested)

CodeResultNotes
name = "Nitish"
print(name)
Nitish โœ… Standard valid identifier โ€” starts with a letter.
_ = "Nitish"
print(name)
Nitish โœ… _ is a valid identifier (single underscore is legal), though here the previously defined name variable is what actually gets printed.

4. Invalid Identifier Examples (Errors Demonstrated)

The instructor deliberately broke each rule to show the resulting errors:

a) Starting with a digit

1name = "Nitish"
print(1name)

Error:

SyntaxError: invalid syntax

Why: Violates Rule 1 โ€” identifiers cannot start with a digit.

b) Using only a digit as the identifier

2 = "Nitish"
print(2)

Error:

SyntaxError: cannot assign to literal

Why: 2 is treated as a numeric literal, not an identifier โ€” Python won't allow assignment to a literal value.

c) Using a special character (#) as identifier

# = "Nitish"
print(#)

Error:

SyntaxError: unexpected EOF while parsing

Why: # is the comment symbol in Python, so the interpreter treats everything after it as a comment โ€” it can never be used as an identifier.

d) Using a hyphen inside the identifier name

first-name = "Nitish"
print(first-name)

Error:

SyntaxError: cannot assign to operator

Why: The hyphen (-) is interpreted as the subtraction operator, not a valid identifier character. Rule 2 only allows letters, digits, and underscores โ€” not hyphens. (Use first_name with an underscore instead.)

e) Using a keyword as an identifier

False = "Nitish"
print(False)

Error:

SyntaxError: cannot assign to False

Why: False is a reserved keyword (part of the 33 keywords list). Rule 3 explicitly forbids using keywords as identifiers.

Commands / Syntax Summary

# View all Python keywords
import keyword
print(keyword.kwlist)
Command/SnippetPurpose
import keywordImports the built-in keyword module
keyword.kwlistReturns the full list of reserved Python keywords

Important Points

Things to Remember

Quick Revision

Python has 33 reserved keywords (checkable via keyword.kwlist) that can never be used as identifiers. An identifier (variable/function/class name) must start with a letter or underscore, and can only contain letters, digits, and underscores afterward โ€” no hyphens, symbols, or leading digits. Breaking any of these rules produces a specific SyntaxError (e.g., invalid syntax, cannot assign to literal, cannot assign to operator, or cannot assign to <keyword>), which directly tells you which rule was violated.