Monday, December 9, 2019

Python Identifier

A Name in python program is called identifier.
It can be class name or function name or module name or variable name.

a = 10

Rules to define identifiers in python :  

The  only allowed characters in python are 

  1. Alphabet symbols ( either lower case or upper case )
  2. Digits(0 to 9)
  3. Underscore symbol(_)

By mistake if we are using any other symbol like $ then we will get syntax error.

cash = 10 ( right )
ca$h = 20 ( wrong )

  1. Identifier should not starts with digit 
           123total ( wrong )
            total123 ( right )

     3. Identifiers are case sensitive. Of course python language is case sensitive language.
         total=10
         TOTAL=999
         print(total) #10
         print(TOTAL) #999

Identifier 

Alphabet symbols ( Either upper case or lower case )
If identifier is start with underscore(_) then it indicates it is private.
Identifier should not start with digits
Identifier are case sensitive 
We cannot use reserve words as identifiers ( Eg: def = 10 ( wrong ) )
There is no length limit for the python identifiers. But not recommended to use too lengthy identifiers.
Dollar ( $ ) symbol is not allowed in python.

Which of the following are valid python identifiers ?

123total ( wrong )
total123 ( right )
java2share ( right )
ca$h ( wrong )
_abc_abc_ ( right )
def ( wrong )
if ( wrong ) 

Note:

If identifier starts with _symbol then it indicates that it is private.
If identifier starts with _(Two under score symbols) indicating that strongly private identifier
If the identifier starts and ends with two underscore symbols then the identifier is language defined special name, which is also known as magic methods.


Eg : _add_ 

No comments:

Post a Comment