Introduction
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.
Keyword | Description |
and | a logical operator that returns true if both the operands are true or else returns false |
as | used to create an alias |
assert | used during debugging to check the correctness of code |
break | Control statement used to break from a loop |
class | used to define a class |
continue | control statement used to continue to the next iteration of a loop |
def | used to define a function |
del | used to delete the reference to the object |
elif | condition statement used for the else if condition |
else | conditional statement that is executed if the if condition is false |
except | Used in exceptions |
False | boolean value |
finally | used with exceptions to execute a block of code that will be executed no matter if there is an exception or not |
for | Used in for loop |
from | import specific parts of a module |
global | Used to declare a global variable |
if | a conditional statement that executes if the condition is true |
import | Used to import a module |
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 |
lamda | used to create an anonymous function |
None | used to represent a null value |
nonlocal | used to declare a non-local variable |
not | a logical operator that returns true if the operand is false or else returns false |
or | a logical operator that returns true if any one of the operands is true or else returns false |
pass | a null statement that does not do anything |
raise | Used to raise an exception |
return | used to exit a function and return a value |
True | boolean value |
try | used to make a try-except statement |
while | used in while loop |
with | used to simplify exception handling |
yield | used 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.
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
print(count)
Output:
5
- 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))
Output:
count1 : True
#count_1 : False
_1_count : True
Differences between Python Keywords and Identifiers
Keyword | Identifier |
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 letters | Contains letters, underscore, and digits. |
Example:- or, raise, pass | Example:- maxCount, minNum1, etc |
Frequently Asked Questions
Keywords are reserved words with specific meanings. Identifiers are the user-defined names for variables, functions, etc.
flag, count_3, add_, sum1, etc. are examples for identifiers.
if, not, assert, yield, etc., are examples for keywords.
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
Leave a Reply