Identifiers and Keywords in Python

Python Identifier is the name we give to identify a variable, function, class, module or any other object. That means whenever we want to give an entity a name, that's called identifier. Think of this as we are identified by our names. Without names it is not possible to reference a person from a group of people in a room. Remember that identifier belongs to a namespace. Namespace is like family in real world. Hence two identifier with same name can be used independently in your program on a condition that they must belong to different namespace. Module introduce namespace in Python. 

Rules for writing Identifiers

1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). Identifiers like myClass, var_1 and print_this_to_screen, are all valid example.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
3. Keywords cannot be used as identifiers.
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.
5. Identifier can be of any length. But keep them as short and as meaningful as possible.


Things to care about

Though you see some hard rules above for writing identifiers, there are some naming conventions which are not mandatory but rather good practices to follow.

1. Python is a case-sensitive language. This means, Variable and variable are not the same. Even 'Python' is not equal to 'python' in python. Always name identifiers that make sense.
2. Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
3. Starting an identifier with a single leading underscore indicates the identifier is private.
4. Starting an identifier with a double leading underscore indicates the identifier is strongly private.
5. If the identifier starts and ends with two underscores, than means the identifier is language-defined special name.
6. While, c = 10 is valid. Writing count = 10 would make more sense and it would be easier to figure out what it does even when you look at your code after a long time.
7. Multiple words can be separated using an underscore, this_is_a_long_variable.
8. We can also use camel case style of writing, i.e., capitalize every first letter of the word except the initial word without any spaces. For example: camelCaseExample



Python keywords are the words that are reserved. That means you can not use them as name of any entities like variables, classes, functions or any other identifier.

So you might be thinking what are these keywords for. They are used for defining the syntax and structures of the Python language.

You should know there are 33 keywords in Python programming language as of writing this article. Although the number can vary in the course of time. Also keywords in Python are case sensitive. So they are to be written as it is.


1. All the keywords contain only alphabets.
2. All the keywords except True, False and None are in lowercase and they must be written as it is.

How can I list the keywords in Python?

Method 1: You can get the list of python keywords through python shell help. 




>>> help()

Welcome to Python 3.4!  This is the interactive help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.4/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> keywords

Here is a list of the Python keywords.  Enter any keyword to get more help.

False               def                 if                  raise
None                del                 import              return
True                elif                in                  try
and                 else                is                  while
as                  except              lambda              with
assert              finally             nonlocal            yield
break               for                 not                 
class               from                or                  
continue            global              pass                

help> [Press Enter to leave help and return to the Python interpreter.]

You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
>>>


Method 2: You can get the list of python keywords through kwlist attribute of keyword module.


>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>>


A complete list of Python keywords with example: List of Keywords in Python

Comments

Back To Top

Popular posts from this blog

How to save video from Internet Explorer

error 18 at 0 depth lookup: self signed certificate

How to check fragmentation in MySQL tables