Python Keywords and Identifiers
Introduction to Python Keywords and Identifiers
Keywords and identifiers are fundamental concepts in Python programming. Every Python program uses keywords and identifiers to define instructions, variables, functions, classes, and other programming elements.
Keywords are reserved words that have predefined meanings in Python. They cannot be used as variable names or identifiers.
Identifiers are names given to variables, functions, classes, and other objects in a Python program.
In this tutorial, you will learn about Python keywords, identifiers, naming rules, valid and invalid identifiers, and practical examples.
1. What Are Keywords in Python?
Keywords are reserved words in Python that have special meanings and predefined purposes in the programming language.
These words are part of Python’s syntax and are used to perform specific operations.
For example:
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
In this example:
ifis a Python keyword used for conditional execution.elseis a Python keyword used to define an alternative block.printis a built-in function, not a keyword.
Python does not allow keywords to be used as variable names.
2. List of Python Keywords
Python provides several reserved keywords used for different programming operations.
Some commonly used Python keywords include:
| Keyword | Description |
|---|---|
if | Executes code based on a condition |
else | Defines an alternative condition block |
elif | Checks another condition |
for | Creates a for loop |
while | Creates a while loop |
break | Terminates a loop |
continue | Skips the current loop iteration |
pass | Acts as a placeholder |
def | Defines a function |
return | Returns a value from a function |
class | Defines a class |
import | Imports a module |
from | Imports specific components |
as | Creates an alias |
try | Starts an exception-handling block |
except | Handles an exception |
finally | Defines code that executes after try/except |
raise | Raises an exception |
and | Logical AND operator |
or | Logical OR operator |
not | Logical NOT operator |
in | Checks membership |
is | Checks object identity |
True | Boolean true value |
False | Boolean false value |
None | Represents the absence of a value |
global | Refers to a global variable |
nonlocal | Refers to a variable in an enclosing function |
lambda | Creates an anonymous function |
with | Manages a context |
yield | Produces a value from a generator |
del | Deletes a reference or item |
assert | Checks a condition |
async | Defines asynchronous code |
await | Waits for an asynchronous operation |
Note: The exact keyword list can vary between Python versions.
3. How to Display Python Keywords
Python provides a built-in keyword module that allows you to access the list of reserved keywords.
Example
import keyword
print(keyword.kwlist)
Output:
The program displays the list of Python keywords supported by the installed Python version.
Checking Whether a Word Is a Keyword
You can use the iskeyword() function to check whether a particular word is a reserved keyword.
import keyword
print(keyword.iskeyword("if"))
print(keyword.iskeyword("else"))
print(keyword.iskeyword("name"))
Output:
True
True
False
In this example:
ifis a keyword.elseis a keyword.nameis not a keyword.
4. What Are Identifiers in Python?
Identifiers are names used to identify variables, functions, classes, and other programming elements.
For example:
name = "Nitish"
age = 22
print(name)
print(age)
Output:
Nitish
22
In this example:
nameis an identifier.ageis an identifier.
Identifiers help programmers refer to values and other elements throughout a program.
5. Rules for Naming Identifiers in Python
Python follows specific rules when creating identifiers.
Rule 1: An Identifier Cannot Start with a Digit
An identifier must start with a letter or an underscore.
Correct:
name1 = "Nitish"
age2 = 22
print(name1)
print(age2)
Incorrect:
1name = "Nitish"
This produces a SyntaxError because an identifier cannot begin with a number.
Rule 2: An Identifier Can Start with an Underscore
Python allows identifiers to begin with an underscore (_).
Example:
_ = "Nitish"
print(_)
Output:
Nitish
You can also use underscores within longer identifier names.
_student_name = "Rahul"
print(_student_name)
Rule 3: Special Characters Are Not Allowed
Identifiers can contain letters, digits, and underscores.
Special characters such as @, $, %, and ! cannot be used in ordinary identifier names.
Correct:
student_name = "Rahul"
student_age = 22
Incorrect:
student@name = "Rahul"
student$age = 22
Rule 4: Keywords Cannot Be Used as Identifiers
Python keywords have predefined meanings, so they cannot be used as variable names.
Incorrect:
if = 10
class = "Python"
Correct:
if_value = 10
class_name = "Python"
Rule 5: Spaces Are Not Allowed
An identifier cannot contain spaces.
Incorrect:
student name = "Rahul"
Correct:
student_name = "Rahul"
6. Valid and Invalid Identifiers
The following table shows examples of valid and invalid identifiers.
| Identifier | Valid / Invalid | Reason |
|---|---|---|
name | Valid | Starts with a letter |
name1 | Valid | Contains a digit after a letter |
_name | Valid | Starts with underscore |
student_age | Valid | Contains an underscore |
1name | Invalid | Starts with a digit |
student name | Invalid | Contains a space |
student@name | Invalid | Contains a special character |
class | Invalid | Python keyword |
age2 | Valid | Starts with a letter |
total_marks | Valid | Uses letters and underscore |
7. Python Identifiers Are Case-Sensitive
Python is a case-sensitive programming language.
This means uppercase and lowercase letters are treated differently when creating identifiers.
Example
name = "Nitish"
Name = "Rahul"
NAME = "Amit"
print(name)
print(Name)
print(NAME)
Output:
Nitish
Rahul
Amit
Here, name, Name, and NAME are treated as three different identifiers.
8. Difference Between Keywords and Identifiers
Keywords and identifiers have different purposes in Python.
| Keywords | Identifiers |
|---|---|
| Reserved words in Python | User-defined names |
| Have predefined meanings | Used to identify programming elements |
| Cannot be used as variable names | Can be used as variable names if naming rules are followed |
Examples: if, else, for | Examples: name, age, student_name |
9. Practical Example of Keywords and Identifiers
Consider the following Python program:
# Student information
name = "Nitish"
age = 22
if age >= 18:
print(name, "is eligible")
else:
print(name, "is not eligible")
Output:
Nitish is eligible
In this program:
nameandageare identifiers.ifandelseare keywords.print()is a built-in function.
This example demonstrates how keywords and identifiers work together in a Python program.
10. Practice Questions on Python Keywords and Identifiers
Test your understanding with these multiple-choice questions.
Question 1: What are Python keywords?
A. User-defined variable names
B. Reserved words with predefined meanings
C. Special characters
D. Data types only
Answer: B. Reserved words with predefined meanings.
Question 2: Which of the following is a valid identifier?
A. 1student
B. student name
C. student_name
D. class
Answer: C. student_name
Question 3: Which symbol can be used at the beginning of an identifier?
A. @
B. $
C. _
D. %
Answer: C. _
Question 4: Which Python module provides the list of reserved keywords?
A. math
B. random
C. keyword
D. datetime
Answer: C. keyword
Question 5: Are name and Name treated as different identifiers in Python?
A. Yes
B. No
C. Only in functions
D. Only in classes
Answer: A. Yes
Question 6: Which of the following cannot be used as a variable name?
A. student_age
B. age1
C. _name
D. for
Answer: D. for