Python Identifiers and Keywords


Table of contents

Python Identifiers

Like other programming languages, identifiers in python are those names that identify a variable, function, class, module, package, or another object. Identifiers in python can be started by any uppercase or lowercase letters, or an underscore and followed by zero or more letters (A-Z, a-z), underscores or numbers (0-9). However, punctuation characters like @, #, %, $, *, are not allowed within an identifier. The identifiers in python are case sensitive, that is ‘Yonep’ and ‘yonep’ are two different identifiers.

Python identifier writing rules

  • Only lowercase or uppercase letters (a -z, A-Z) or digits (0-9) or an underscore can be combined to form an identifier. For example, myvar, studentClass, employee0, _var1, var_100 are valid identifiers.
  • An identifier cannot be started with a digit or number. For example, myvar9 is valid identifier while 9myvar is not.
  • Python keywords or reserved words are not allowed to be used as identifiers.
  • All the punctuation characters like !, %, &, $, ., cannot be used in identifiers.
  • According to Python docs, a python identifier can be of any length, however, PEP-8 standard limits the length to a maximum of 79 characters.

Python identifier writing conventions

  • Start class names with an uppercase letter and other identifiers with a lowercase letter.
  • Start private identifiers with an underscore (_).
  • Make an identifier such that it is easy to understand the context for a person reading the code. Avoid using single letters as an identifier.
  • Use underscore to combine multiple words to form a more descriptive identifier. For example student_name, employee_salary

Python Keywords or Reserved Words

There are numbers of keywords in python that are reserved and cannot be used as an identifier. Keep in mind that all the keywords are lowercase except True, False and None. As mentioned above in python identifier writing rules, you cannot use these keywords or reserved words as identifiers.

List of Python Keywords

and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield
True False None