Table of contents
1.
Introduction
2.
What are Keywords?
3.
What are Identifiers?
3.1.
Rules for naming Identifier
3.2.
Function to check a valid identifier
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

Identifiers and Keywords

Author APURV RATHORE
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Every language contains a set of words that, when combined meaningfully, forms meaningful sentences. Similar to them, there are keywords and identifies present in every programming language that constitutes the vocabulary of the programming language. 

In this blog, we will learn about keywords and identifiers in Python. 

Also see,  Convert String to List Python

What are Keywords?

In Python, keywords are predefined and reserved words with specific meanings. Keywords define the syntax of the code. The keyword can't be used as a variable name, function name, or identifier. Except for True and False, all keywords in Python are written in lower case. There are 36 keywords reserved in Python 3.9. 
 

The major keywords present in Python are

Keyword Description
False boolean value
True boolean value 
from import specific parts of a module
else It is a conditional statement that gets executed if all the if’s evaluate to false
elif It is a conditional statement that represents else if
continue It is a control statement that is used to continue to the next iteration of the loop.
break It is a control statement. It used to break from a loop
assert It is a debugging statement used to check the correctness of the code
and It is a logical operator that returns false if any one of the operands is false or else will return false
not It is a logical operator that returns false if the operand is true or else returns true
or It is a logical operator that will return true if any one of the operands is true or else will return false
pass It is a null statement, i.e., it does nothing
except Used in exceptions
for Used in for loop
while used in while loop
in used to check if a value is present in a list, tuple, etc.
is used to check if the two variables are equal or not
as used to create an alias
lamda used to create an anonymous function
global Used to declare a global variable
nonlocal used to declare a non-local variable
class used to define a class
def used to define a function
del used to delete the reference to the object
yield used to end a function and return a generator
return used to exit a function and return a value
import Used to import a module
try used to make a try-except statement
if  Used to make conditional statement
raise Used to raise an exception
None used to represent a null value
with used to simplify exception handling
finally used with exceptions to execute a block of code that will be executed no matter if there is an exception or not

You can also read about the Multilevel Inheritance in Python, Swapcase in Python 

What are Identifiers?

They are user-defined names that are used to identify entities such as classes, functions, variables, and so on. They're utilised to distinguish one object from another.

An identifier is a combination of digits, characters, and underscores. The first letter of an identifier can not be a character or digit. 

Python is case sensitive; hence identifiers in Python are also case sensitive. For example, Var and var are two different identifiers. 

Also see, Python Operator Precedence

Rules for naming Identifier

  • A combination of digits, underscores, and lowercase or uppercase characters can be used to create identifiers.
  • Identifiers should not contain spaces in between. 
  • A keyword should not be used as an identifier. 
  • Special symbols other than underscore should not be used.
  • Digits must not be present at the beginning of an identifier. 

 

Examples of correct identifier names are var, _var, _var9, _9var, etc. 

Examples of incorrect identifier names are 9var, @var, var@, var1@, etc. 

Function to check a valid identifier

There is an isidentifier() method present in Python, which checks if a string is a valid identifier or not. The function returns True if the string is a valid identifier, else it returns False.

var = "a9_bc"
print(var.isidentifier())
var = "_abc9"
print(var.isidentifier())
var = "9_abc"
print(var.isidentifier())
You can also try this code with Online Python Compiler
Run Code


Output:

True
True
False
You can also try this code with Online Python Compiler
Run Code

 

Example 1:

The below example shows that keywords cannot be used as identifiers. 

#using keyword as identifier
True=1
You can also try this code with Online Python Compiler
Run Code

 

Output:

True=1
    ^
SyntaxError: cannot assign to True
You can also try this code with Online Python Compiler
Run Code

 

Example 2:

The below example shows usage of yield, for, in, else, elif, fun, def keyword. 

def generatorFun():
    for i in range(5):
        yield i

obj = generatorFun()
for i in obj:
    if i==0:
        print("i is zero")
    elif i==1:
        print("i is one")
    else:
        print("i is not zero neither one")
You can also try this code with Online Python Compiler
Run Code

 

Output:

i is zero
i is one
i is not zero neither one
i is not zero neither one
i is not zero neither one
You can also try this code with Online Python Compiler
Run Code

 

Example 3:

The below example shows usage of try, except, def, return keyword. 

def fun():
    var = 0
    try :
        divideRes = 1/var
        return divideRes
    except:
        print("Error")
        return -1

var = fun()
print(var)
You can also try this code with Online Python Compiler
Run Code

 

Output:

Error
-1
You can also try this code with Online Python Compiler
Run Code

 

Example 4:

The below example shows usage of global, def, del keyword. 

l = [1,2,3]
def fun():
    global l
    del(l[1])

print(*l)
fun()
print(*l)
You can also try this code with Online Python Compiler
Run Code

 

Output:

1 2 3
1 3
You can also try this code with Online Python Compiler
Run Code

 

Example 5:

The below example shows the usage of the assert keyword. 

def fun(num1, num2):
    assert (num2!=0), "Divisor is zero"
    num1 = num1 / num2
    return num1

res = fun(1,0)
print(res)
You can also try this code with Online Python Compiler
Run Code

 

Output:

AssertionError: Divisor is zero
You can also try this code with Online Python Compiler
Run Code

 

You can practice by yourself with the help of online python compiler.

Frequently Asked Questions

  1. How to check if an identifier is valid or not?
    Python contains the isidentifier() method, which can be used to check if a string is a valid identifier or not. It returns True if it is valid, else returns False. 
     
  2. What is the difference between keyword and identifier?
    Keywords are a predefined and reserved set of words in Python which holds a special meaning, while identifiers are a name given to a variable, class, or function.
     
  3. Can keywords be used as identifier names?
    No. Keywords are reserved and cannot be used as an identifier name. 

Key Takeaways

Congratulations on making it this far.

In this blog, we understood what keywords and identifiers in Python are.  We also saw different keywords present in Python and learned about their importance. 

If you want to become proficient with Python programming, I suggest you take the Coding Ninjas Python Course, which will teach Python basics with Data Structures and Algorithms. 

Live masterclass