What Are Python Keywords And Identifiers?

What Are Python Keywords And Identifiers?
What Are Python Keywords And Identifiers?


In any language, consider English, there would be a set of words that would give us a meaningful sentence when used together. Similarly, there are some words called Keywords and Identifiers used to write the code in programming languages. Keywords and identifiers together build the vocabulary for the programming language.

Let’s learn about the Python Keywords and Identifiers in detail.

Python Keywords

Keywords are reserved words with a predefined meaning. These words cannot be used as a function name, variable name, class name, etc. They are written in lower case except for True, False, and None and are case-sensitive. 

There are 33 keywords in Python. Let’s go through them one by one.

anda logical operator that returns true if both the operands are true or else returns false
asused to create an alias
assertused during debugging to check the correctness of code
breakControl statement used to break from a loop
classused to define a class
continuecontrol statement used to continue to the next iteration of a loop
defused to define a function
delused to delete the reference to the object
elifcondition statement used for the else if condition
elseconditional statement that is executed if the if condition is false
exceptUsed in exceptions
Falseboolean value
finallyused with exceptions to execute a block of code that will be executed no matter if there is an exception or not
forUsed in for loop
fromimport specific parts of a module
globalUsed to declare a global variable
if a conditional statement that executes if the condition is true
importUsed to import a module
inused to check if a value is present in a list, tuple, etc.
isused to check if the two variables are equal or not
lamdaused to create an anonymous function
Noneused to represent a null value
nonlocalused to declare a non-local variable
nota logical operator that returns true if the operand is false or else returns false
ora logical operator that returns true if any one of the operands is true or else returns false
passa null statement that does not do anything
raiseUsed to raise an exception
returnused to exit a function and return a value
Trueboolean value 
tryused to make a try-except statement
whileused in while loop
withused to simplify exception handling
yieldused to end a function and return a generator

Python Identifiers

An identifier is a user-defined name used to identify entities like class, functions, variables, etc. They are used to differentiate one entity from another.

The identifier is a combination of characters, digits, and a special symbol underscores. Underscore can be used for multi-word variables like count_one, container_2, etc.

Python is a case-sensitive programming language that makes identifiers also case-sensitive. For example, Count and count are two different identifiers in Python.  

It is advised to give a meaningful name to the identifier to make the code understandable. For example, a variable to store the count of numbers can be named “count” instead of “c.”

Rules to name an identifier 

While naming an identifier, a set of rules needs to be followed to have a valid identifier. The rules are:-

  • An identifier can be a combination of digits, underscore, and letters in lowercase or uppercase.
  • Keywords are not allowed to be used as identifiers.
  • An identifier cannot have any spaces.
  • Digits cannot be used in the starting position of identifiers.
  • Special symbols like !, @, #, $, %, etc are not allowed.

If any one of the above rules is violated, the identifier would be invalid, and the following error would occur.

blog banner 1

Example of Valid Identifiers: count1, count1, count_1, _1_count, Count101, etc.
Example of Invalid Identifiers: count_1, 1_count, 1Count, count1, etc.

Attempt the MCQ questions on CodeStudio based on Identifier naming.

Example for Python Keywords and Identifiers

Let’s see an example to count the number of even numbers from 1 to 11 and identify the Python Keywords and Identifiers used in it.

# variable count
count = 0
# for loop
for i in range (1, 11):
 # check if event
 if(i % 2 == 0):
   count = count + 1
# print the number of even numbers



  • In the above example, the identifiers are: count, i
  • In the above example, the keywords are: for, if, in

isidentifier() method

The isidentifier() method in Python is used to check whether a string is a valid identifier or not. Python language has its identifier definition, which is used by this method. It returns True if the string is a valid identifier, otherwise False.

Let’s see an example of the isidentifier() method to understand its use.

# variables declared
variable1 = "count1"
variable2 = "#count_1"
variable3 = "_1_count"
# isidentifer() function called
identifier1 = variable1.isidentifier()
identifier2 = variable2.isidentifier()
identifier3 = variable3.isidentifier()
# print result
print(variable1+" : "+str(identifier1))
print(variable2+" : "+str(identifier2))
print(variable3+" : "+str(identifier3))


count1 : True
#count_1 : False
_1_count : True

Differences between Python Keywords and Identifiers

Keywords are the reserved words with a special meaning.Identifiers are the user-defined names of variables, functions, etc.
They are written in lower case except for True, False, and None.Need not be written in lowercase.
It helps to identify a specific property that exists within Python.It identifies the name of the particular entity.
Contains only lettersContains letters, underscore, and digits.
Example:- or, raise, passExample:- maxCount, minNum1, etc

Frequently Asked Questions

What are Python keywords and identifiers?

Keywords are reserved words with specific meanings. Identifiers are the user-defined names for variables, functions, etc.

Give some examples of identifiers?

flag, count_3, add_, sum1, etc. are examples for identifiers.

Give some examples for keywords?

if, not, assert, yield, etc., are examples for keywords.

What is the difference between an identifier and a variable?

An identifier is a user-defined name used to identify an entity uniquely in a program at execution time. A Variable is a type of identifier. It is the name given to a memory location that holds a value.

Key Takeaways

In this blog, Python keywords and identifiers were covered in detail, along with examples. This blog also included the differences between Python keywords and identifiers, rules for naming identifiers, use of isidentifier() method, and FAQs related to Python keywords and identifiers.

Don’t stop here. Check out our Python guided path to learn Python from Scratch. We hope you found this blog useful. Feel free to let us know your thoughts in the comments section. 

By Hari Sapna Nair