Identifiers and Keywords in Python

Programming is majorly about manipulation about data. These data are normally either assigned to a variable or inserting into a collection(atleast temporary data). Assigning of data, under the hood means binding an object reference so that it refers to the object in memory that holds that data.
Thus if we talk about identifiers, all we mean is the custom names that we give to these object references. In short they are our variable names.

Factors to Consider in Naming identifiers

Naming variables or assigning identifiers should follow some basic rules:

  1. The identifier must a non-empty sequence of characters of any length.
  2. The first character in the identifier must be anything that Unicode considers a letter. This includes the :
    • ASCII letters : a-z,A-Z
    • Underscore :_
  3. Each continuation character(charatecter after start character) can be any non-whitespace character, including digits(0-9).
  4. Identifiers are case-sensitive.
  5. No identifier can have the same name as one of Python's keywords.

Python provides a built in function called dir(). This function returns a list of of an object's attributes.This is by calling it with no parameters.

>>> dir()
['__builtins__', 'sys']

We can then call it passing in the __builtins__ attribute as a parameter to our dir() function.

>>> dir(__builtins__)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

As you can see the __builtins__ attribute is a module that holds all op Python's built-in attributes.

You should not use names that begin and end with two underscores e.g __getitem__. This a convention as python defines various special methods that use such names.
Furthermore, names that begin with one or two leading underscores(and don't end with two underscores) are treated specially in some contexts.

A single underscore on its own can also be used as an identifier. If used inside an interactive python interpreter or shell, _ will hold the result of the last expression that was evaluated.

EXAMPLE

for _ in (1,2,3,4,5):
    print(_)

RESULT

1
2
3
4
5