Reserved words the interpreter owns, and the naming rules for everything you get to name yourself โ with every way to break those rules demonstrated.
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.
Name and name are different identifiers).SyntaxError.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.
Definition: A Python identifier is a name used to identify a variable, function, class, module, or other object.
_)._), and digits.| Code | Result | Notes |
|---|---|---|
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. |
The instructor deliberately broke each rule to show the resulting errors:
1name = "Nitish"
print(1name)
Error:
SyntaxError: invalid syntax
Why: Violates Rule 1 โ identifiers cannot start with a digit.
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.
#) 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.
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.)
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.
# View all Python keywords
import keyword
print(keyword.kwlist)
| Command/Snippet | Purpose |
|---|---|
import keyword | Imports the built-in keyword module |
keyword.kwlist | Returns the full list of reserved Python keywords |
Name, NAME, and name are three different identifiers.name1 is valid).#.False, True, None, import, class, etc.) can be used as variable names.SyntaxError: cannot assign to literal, cannot assign to operator, and cannot assign to <keyword> are Python's way of telling you which specific rule was broken โ reading the exact error message helps pinpoint the mistake quickly.name, _name, name_1, _1name (starts with digit), first-name (hyphen), #comment (special char), False (keyword), 2 (pure literal)keyword.kwlist anytime you're unsure if a word is reserved.first_name, not first-name).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.