Vinay Garg Assistant Professor

Lesson 4: Python identifiers and reserved keywords

An identifier is any name you invent: a variable, a function, a class. Thirty-five words are reserved by the language and cannot be used. These are the rules, and the few habits that keep names readable.

A dark code window listing Python keywords and a valid variable name

In short

  • An identifier is a name you give to a variable, function, class or module.
  • It may contain letters, digits and underscores, must not start with a digit, and is case sensitive.
  • 35 keywords are reserved in modern Python; they cannot be used as names.
  • Use `snake_case` for variables and functions, `PascalCase` for classes, and UPPER_CASE for constants.

35

reserved keywords in Python 3.9 and later

Python docs

0

keywords that may be used as a variable name

Python docs

What an identifier is

An identifier is a name you make up for something in your program: a variable, a function, a class, a module. Python uses the name to find the value again, so the rules for forming one are strict even though the choice of words is yours.

The rules

  • Letters (a–z, A–Z), digits and underscore only. No spaces, hyphens or punctuation.
  • The first character must be a letter or an underscore, never a digit: 2marks is invalid, marks2 is fine.
  • Case matters. total, Total and TOTAL are three different names.
  • A keyword cannot be used as a name.
  • There is no length limit, but a name longer than about twenty characters is usually a sign the idea needs splitting.

The 35 reserved keywords

These words already mean something to Python. You cannot use them as names, and you rarely need to memorise the list — recognising them is enough.

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 that True, False and None are capitalised. Writing true gives a NameError, not a syntax error, which is why it confuses beginners.

The program

# names.py — what Python will and will not accept

import keyword

print("Keywords in Python:", len(keyword.kwlist))
print(keyword.kwlist[:8])
print(keyword.iskeyword("for"))     # True
print(keyword.iskeyword("For"))     # False, keywords are case sensitive

student_name = "Asha"
_roll = 21
marks2 = 88

print(student_name, _roll, marks2)

How it works, step by step

  1. keyword is a standard module that knows the reserved words of the running Python.
  2. keyword.kwlist is the list; len() counts it, and [:8] keeps the first eight so the output stays short.
  3. keyword.iskeyword() answers True or False for any word, which is handy in an editor or a quiz.
  4. student_name uses an underscore between words — the Python convention, called snake_case.
  5. _roll starts with an underscore, which is allowed. A leading underscore is a hint that the name is internal to the module.

What it prints

Keywords in Python: 35
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await']
True
False
Asha 21 88

Naming habits worth adopting now

Kind of nameStyleExample
Variablesnake_casetotal_marks
Functionsnake_case, a verbcalculate_total()
ClassPascalCaseStudentRecord
ConstantUPPER_CASEMAX_MARKS = 100
Module or fileshort, lowercasemymath.py

Common mistakes

  • my-var = 5 is read as subtraction. Use an underscore: my_var.
  • class = "MCA" is a syntax error, because class is reserved. Use course = "MCA".
  • Two names that differ only in case are easy to confuse. Keep the habit of lowercase everywhere except classes and constants.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Print the number of keywords in your Python and the first eight of them.
  2. Decide which of these are valid identifiers and check with `keyword.iskeyword` or a quick run: `total_marks`, `2marks`, `my-name`, `class`, `_temp`, `Total`.
  3. Rename an ugly variable such as `x1` to a clear name and print the same value.

Sources

Frequently asked questions

What is an identifier in Python?

Any name you give to a variable, function, class or module. It must start with a letter or underscore, contain only letters, digits and underscores, and must not be one of the reserved keywords.

How many keywords does Python have?

Thirty-five in Python 3.9 and later, including True, False, None, if, for, while, def, class, import, try and lambda. Run len(keyword.kwlist) to see the count for your own version.

Can a variable name start with an underscore?

Yes. A single leading underscore is a convention meaning "internal to this module", and two leading underscores trigger name mangling inside a class. Neither is an error.

Are Python keywords case sensitive?

Yes. for is a keyword, For is an ordinary name, and True is a value while true is an undefined name.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.