Keywords and Identifiers in Python

Keywords and Identifiers in Python

Hello Techies,

In this blog, we are learning about Keywords and Identifiers in Python. So basically I am covering keywords (reserved words in Python) and identifiers topics in Python.

Let’s check the use of Keywords and Identifiers in Python with the help of some examples.

Keywords and Identifiers in Python with Examples

What is a Python Keyword?

  • Keywords are reserved words in Python.
  • There are 35 keywords in Python 3.8. This number may change slightly over time.
  • You cannot use keywords as the variable name, function, class, etc. These keywords have a special meaning and are used for special purposes in the Python programming language. For example – the Python keyword “with” is used in Python when working with unmanaged resources so you can’t name a variable named “with” otherwise it may cause an error.

Type keywords to get a list of keywords running Python version on your operating system.

>>> help()
Welcome to Python 3.8's help utility!

...

help> keywords
Here is a list of the Python keywords.
...
False               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not

Also, using the code below you will get a list of Python keywords.

import keyword
print(keyword.kwlist)

Output:

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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'] 

How many keywords in Python?

There are 35 keywords in Python 3.8. This number may change slightly over time.

Examples of Python Keywords

Let’s look at some examples of Python keywords.

True, False

True and false keywords are used comparison operations or logical (boolean) operations in Python. For example:

>>> 0==0
True
>>> True == False
False
>>> 8>7
True

and, or and not

These are logical operators in Python. If both operands are True, the AND operator will return the True. OR operator returns True if one of the operands is true. If the operand is False, NOT operator returns True. let’s check some examples:

>>> True and False
False
>>> True or False
True
>>> not True
False

break, continue

We use break and continue in for and while loops to change their normal behavior. let’s check the example:

#break keyword
for i in range(1,20):
    if i == 8:
        break
    print(i)

#continue keyword
for i in range(1,20):
    if i == 8:
        continue
    print(i)

Output:

#break keyword output
1
2
3
4
5
6
7

#continue keyword output
1
2
3
4
5
6
7
9
10
11
12
13
14
15
16
17
18
19

These are some examples of Python keywords. For the rest of the keywords, here is a complete list of all keywords with examples.


What is a Python Identifier?

  • The identifier is the name given to elements such as classes, functions, variables, etc. that help to distinguish one element from another.
  • An Identifier is a name having a few letters, numbers, and underscore [A to Z, a to z, 0-9 or _] not special characters like %, $, #.
  • It should always start with a non-numeric character.
  • It is used to identify a variable, function, class, etc.

Python identifier example

Below are 4 different variables known as identifiers.

num = 78
print(num)

_x = 89
print(_x)

x_y = 98
print(x_y)

NUM = 99
print(NUM)

Output:

78
89
98
99
Note: Python is case sensitive programming language, so as per the above 
           example the num and NUM are two different variables in python.
Interview Question

Q:  Difference between keyword and identifier in Python?
Ans: A keyword is a predefined term reserved for working programs that 
         have a special meaning and cannot be used anywhere. Identifiers are    
         values  used to define different programming items, such as variables,   
         integers, structures, unions, and others.

I hope you understand the concept of keywords and identifiers in Python. Still, you have some doubts regarding keywords and identifiers in Python comment down below.

Check out the official site for more information about keywords and identifiers in Python.

Leave a Comment