Python Identifiers


Python Identifiers

An identifier is a name given to the entities like class, functions, variables, etc. It helps to differentiate one entity from another.

Rules for writing identifiers:

  • Identifiers can be a mixture of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9),
    or an underscore (_). Names like myData, var_1, and print_this_to_screen, are all valid examples.
  • An identifier cannot begin with a digit or a numeric variable.
    EXAMPLE:- 9vari is invalid, but var9 is a valid name.
  • Keywords’ names cannot be used as identifiers.
  • We should not use any special symbols like !, @, #, $, %, etc., in the naming of our identifier.
  • An identifier can be of infinite length as such.

Practices with Identifiers in Python

  • Class names always start with an uppercase letter, and all the identifiers must be started with a lowercase letter.
  • Private identifiers must be written with an underscore(_).
    {NOTE:- This never makes a private variable but warns the programmer from trying to access it.}
  • It's better to use longer names than single characters. {Like for example:- prefer to use index=2; instead of i=2;}
  • It's good to practice underscore to combine words in an identifier. {For example:- It_is_a_code.}
  • While dealing with mangling, adopt leading double underscore only.
  • REMEMBER:- Python is a case-sensitive programming language. So, Code and code are two various identifiers.
  • For naming in python, use the camel case. {For example:- Camelcase is myVarOne and Pascal case is MyVarOne}.

Example:

num = 10 print(num) _x = 100 print(_x) a_b = 99 print(a_b)

OUTPUT:

10
100
99