Keywords and Identifiers in Python

Pallavi Dhotre

8 months ago

Keywords and Identifiers in Python | insideAIML
Table of Contents
  • Introduction
  • Keywords (Reserved words)
  • Identifiers
  • Difference between keyword and identifier in python
  • Summary

Introduction

          Every language has its vocabulary similarly; every programming language comes with its set of keywords. Almost all high-level programming languages contain a set of keywords. Some of the most common keywords used in almost all high-level programming languages are: if, else, while, for, break, etc. These keywords are also known as reserved keywords in python. Keywords have some predefined meaning in the programming language. We cannot assign any value to keywords. For storing the value, we generally use variables. Also, we give names to the entities like class, functions, and variables; these names are known as identifiers. In this article, we will deep dive into the concept of keywords and identifiers in python programming. 

Keywords (Reserved words)

          As python is a case sensitive language, reserved words in python are also case sensitive. Keywords are also called reserved keywords in python because they have a predefined meaning and are strictly used for a specific purpose. We cannot assign a new value to the reserved words. The most important thing about reserved words is that they should be used in the given case. If we change the cases of the reserved words, then it does not follow the predefined meaning. It will not be considered a reserved word anymore. In the diagram given below, we can see the list of reserved words used in Python. Python contains 33 reserved words. Some of them are int, float, import, if, elif, True, False, None, etc. We can notice that all the keywords are in lowercase except None, True, and False.  Let’s have a look at few keywords:
Keywords in Python | insideAIML
  • True, False: these are the Boolean values in python. The result of the logical operation is indicated by any one of these values.
  • and, or, not: these are logical operators in python. And the result of these operators is always a Boolean value.
  • if, elif, else: these keywords are used in the decision control structure
  • while, for: these keywords are used in a loop control structure
  • break, continue: these keywords are used within the loop control structure to terminate the execution of the loop or to terminate the execution of the current iteration of the loop
  • class: this keyword is used to create a user-defined class
  • def: this keyword is used to create a user-defined function
  • try, except, raise, finally: these keywords are used in exception handling to handle the exception that occurred during the execution of the program
  • from, import: these keywords are used to import any in-built module of python programming to your current namespace
  • global: this keyword is used within the function if you want to access the variable defined inside the function from outside the function.
So these are some commonly used reserved keywords in python. Let’s see an example of keywords:
In the given example we have used the following keywords: int, for, in, def, if, or, else, True, False
Now let’s start with the concept of Identifiers.

Identifiers

          Names given to the entities like variables, class, and function are commonly known as identifiers in python. We cannot use keywords as identifiers otherwise program will throw an error. There are some rules for writing identifiers in python. They are as follow:
  • The identifier can have alphanumeric characters only; no special symbol except underscore ( _ ) is allowed. E.g. student_name1 is a valid identifier name
  • Python is a case sensitive language; that’s different cases have different meanings. E.g. name and NAME will be considered as different identifiers.
  • Space between the identifier is not allowed. E.g. student name is not a valid variable name. Instead, you can write it as a student_name
  • An identifier should always start with a letter or an underscore. You cannot start an identifier with a number. E.g. name1 or _name1 is a valid variable name, but 1name is not a valid identifier in python
  • An identifier can be of any length; there is no limitation about it. 
Let’s see examples of identifiers in python:
In the given example, the following are the valid identifier in python: a and b (these are the names given to lists), f1 (this is a name given to a function) 

Difference between keyword and identifier in python

          The given table indicates the difference between keyword and identifier in python:
Difference between keyword and identifier in python | insideAIML

Summary

          In this article, we have learned about keywords and identifiers in python. Also, we have learned about the difference between keyword and identifier in python. Python is a case sensitive language, and keywords are also case-sensitive. Keywords in python have a predefined meaning, and they are created for a specific purpose. Names given to class, variables, and function are Identifiers. We cannot assign a keyword as an identifier. There are some rules to be followed by the programmer when naming or writing valid identifiers in python. So, this is all about keywords and identifiers in python. 
  
We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below.
   
Liked what you read? Then don’t break the spree. Visit our blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube Channel to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing. 
        

Submit Review