Python identifiers and keywords

Here we will discuss the terms Python identifiers and keywords.


Pyhton identifiers

Identifiers refers to the name given to the variables or object in Python. Identifier is used to identify different objects and also use them in our programs. Wihtout identifier we cannot use any object.

>>> var=90 #var is int type identifier
>>> st='text' #st is string type identifier
>>> from decimal import Decimal as D
>>> d=D(90.4647) #d is decimal type identifier
>>> ls=[23 , 'New'] #ls is list type identifier
>>> dic={1:'One' , 2:'Two' , 3:'three'} #dic is dictionary type identifier

Some rules to know when giving a name/identifier to an object

I) The valid characters for identifiers are the uppercase and lowercase letters ‘A’ through ‘Z’. The underscore ‘_’ and, except for the first character, the digits ‘0’ through ‘9’. This means the nubmer cannot be at the beginning of the names.

Also note, upper case and lower case letter are treated as different character.

In the code below all the identifeirs are different.

>>> new=90
>>> New=901
>>> nEw=789
>>> neW=9000

More examplebelow.

>>> _new=901
>>> n23u=900 #Work fine
>>> 
>>> 34new='text' #error!!
SyntaxError: invalid syntax
>>> +2md=901
SyntaxError: invalid syntax

ii)Identifiers can be unlimited in length.

iii)We can also use characters from outside the ASCII for indetifiers naming.

In the code below Russian,Japanese and Latin is used as an identifiers.

>>> бaвг=901
>>> 平仮名=9101
>>> bēcēdēē=100
>>> бaвг
901
>>> 平仮名
9101
>>> bēcēdēē
100


Python keywords

The Python keywords are identifiers which have a special meaning. Their meanings cannot be changed and hence they cannot be used as a normal identifier. Some of the Python keywords are: False, import, except, in, True, raise, class, global, yield, async,etc.

If you try to use any of the keywords as a normal identifier you will get and error.

>>> False=90 #Error!
SyntaxError: can't assign to keyword
>>> yield=(90 , 89) #Error!
SyntaxError: invalid syntax
>>> raise=900
SyntaxError: invalid syntax

But below identifiers are allowed.

>>> false=90
>>> Yield=(90 , 89)
>>> Raise=900
>>> false
90
>>> Yield
(90, 89)
>>> Raise
900

Pyhton reserved identifiers

There are some reserved identifiers in Python like the keywords. These identifiers have special meaning. They are characterized by the leading and trailing underscore(_) characters.

*Note The ‘*’ represent some names.

_* : It is used in the interactive mode to store the result of the last evaluation; it is stored in the builtins module.

__*__ : Such names are defined by the interpreter and its implementation.

__* : They are use is class definition to prevent intermingling of private attributes between the base class and the derived classes.




Leave a Reply

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