What is meant by Identifiers and keywords in python?

Both of these terms are related to naming conventions in python programming. In python code individual names given to the entities like variable, class, method etc. are known as identifiers. There are few words which are kept reserved by python language, which can’t be used as an identifiers. These reserved words are known as Keywords.

That was the brief definition about identifier and keyword now let’s understand both of these terms thoroghly.

Python Identifiers:

In python program there are many entities present like variables, methods, classes and many other objects. So to identify these entities in python program we have to give some unique names to them. These unique names given to the variables, methods, classes or other object are called as  Identifiers. Using these identifiers interpreter can differentiate between two variables, classes, methods or objects. Identifier gives independent identity to each individual python entity.

Keyword and identifiers in python

In example above words num1, num2, sum are the names given to the individual variable, These names given to each of the variable are nothing but an identifier.

That was just the definition of identifier, but problem here is you just can’t use any word as an identifier. To understand that let’s see one example. See the following two words “Apple” and “apple”. For us these two words are similar in meaning, but for python language these are two completely different words . Since python is a case sensitive language both of these words become a separate identifiers.

So to clear this type of confusion python has set some identifier writing rules. You have to follow these identifier writing rules to avoid errors while execution. So let’s see what are the rules in python to write an identifier.

Rules to define identifiers in python:

  • Identifiers can contain UPPERCASE and lowercase letters (A-Z), Digits (0-9), an underscore “_” or a combination of all of these.
  • Symbols like !,#,@,%,$ can’t be used in identifier.
  • You can write identifier of any length. There is no characters limit for writing an identifier.
  • Identifier can’t be started with digit. Which means 01Apple is not a valid identifier.
  • White spaces between identifier are not supported.

Identifier writing conventions:

Conventions are nothing but the set of standard practices followed in the industry. These are not mandatory things but it is good to follow these practices while writing python code.

To improve the readability of your code and keep it neat and clean you should follow some identifier writing conventions. These conventions will help you and make your life easier in long run. If you keep following these conventions, It will definitely help you to figure out what is written in the code even if you are looking at your code after a long time.

As said earlier these conventions are not mandatory but it is a good practice you should follow. Below are some of the identifier writing conventions followed by most of the python developers.

  • Class names should be written in UPPERCLASS letters. Other identifiers should be written in lowercase letters.
  • Private identifier should be written with leading underscore e.g _variable.
  • While defining any variable use multiple words separated by the underscore e.g It_is_a_variable.
  • Write variable name self explanatory like instead of writing n1 write number1.

So that was all about python identifiers now lets move on to python keywords. Lets try to understand what is meant by keywords in python.

Python Keywords:

In python few words are kept reserved and you are not allowed to use those words as an identifier. Means you can’t use these reserved words to name any entity like variable, class, method etc. These words have predefined meaning in python so if you try use these words as an identifier it will mislead the interpreter and result an error. For example you can not use “True” as a variable name or “else” as a method name.

As said earlier these keywords has some predefined meaning. Like words True/False is the results of Boolean operation python. In same fashion “And/or/not” words are logical operators in python. looking at these examples you must have understood why these words are kept reserved. Now lets see the complete list of keywords in python.

Keywords list is not fixed but it gets updated with each python release. In the list given below few of the words might be added or removed with the newer release of python.

Which are the keywords in python.

Commands to get list of python keyword:

If you want to get the complete list of keyword for your installed python version then below are the steps:

  • Launch python IDE (For me its PyCharm).
  • Type following command lines in PyCharm
  • import keyword
  • print(keyword.kwlist)

how to get the list of keywords in python. List of keywords in python

So guys that was all about identifiers and keywords in python. I hope you must have understood the basic concepts behind these terms. If you are having any doubts then please let me know.

Share this Post

Leave a Comment

Your email address will not be published.