Learnitweb

Python identifiers and keywords

An identifier is a name given to a variable, function, class or module. Only following are allowed in identifiers:

  • Lowercase (a-z) characters
  • Uppercase (A-Z) characters
  • Digits (0-9)
  • Underscore(_)

An identifier can be a combination of lowercase characters, uppercase characters, digits and underscore.

  • An identifier can not start with a digit. 1y is invalid identifier whereas y1 is a valid identifier.
  • Keywords can not be used as identifier. Keywords are later mentioned in this tutorial.
  • Spaces and special symbols like !,@,#,$,%,^,& etc. are not allowed in identifier.
  • Identifier can be of any length.
  • Identifiers are case sensitive. Sum, SUM and sum all are different identifiers.

Following are some valid identifiers:

sum, first_sum, sum1, sum__1, _sum, sum_1_

Following are some invalid identifiers:

1sum, %sum, 1_sum, 1sum%

Keywords

Keywords are a list of reserved words that have predefined meaning. Keywords can not be used as an identifier in Python. Attempting to use a keyword as an identifier name will cause an error.

List of Keywords in Python

andfinallynonlocal
asforNone
assertfromor
breakFalsepass
classglobalraise
continueifreturn
defimporttry
delinTrue
elifiswhile
elselambdawith
exceptnotyield