Python Identifiers

Table Of Contents

  1. What Are Python Identifiers?
  2. Rules For Writing Identifiers.
  3. Valid Identifiers Example.
  4. Invalid Identifiers Example.

What Are Python Identifiers ?

  • We need a name and address to identify someone present in this world.
  • Likewise in python, we need a name to identify a class, functions, and variables present inside computer memory.
  • By giving a name to an entity, it will be easier for us to call and use them when needed.

Rules For Writing Python Identifier

  1. The identifier is a combination of character digits and underscore and the character includes letters in lowercase (a-z), letters in uppercase (A-Z), digits (0-9), and an underscore (_).
  2. An identifier cannot begin with a digit. If an identifier starts with a digit, it will give a Syntax error.
  3. In Python, keywords are the reserved names that are built-in to Python, so a keyword cannot be used as an identifier – they have a special meaning and we cannot use them as identifier names.
  4. Special symbols like !, @, #, $, %, etc. are not allowed in identifiers.
  5. Python identifiers cannot only contain digits.
  6. There is no restriction on the length of identifiers.
  7. Identifier names are case-sensitive.

Valid Identifier Examples

  • abc123
  • abc_de
  • _abc
  • ABC
  • abc
  • var1
  • _var1
  • _1_var
  • var_1
  • __varvar

Invalid Identifier Examples

  • 123abc
  • abc@
  • 123
  • for
  • !var1
  • 1var
  • 1_var
  • var#1

Leave a Reply

Your email address will not be published. Required fields are marked *