• Home
  • 2.13 Keywords and Identifiers

2.13 Keywords and Identifiers

View Categories

2.13 Keywords and Identifiers

4 min read

Python programs are made up of different kinds of names. These names are called identifiers.

An identifier is the name given to variables, functions, classes, modules, and other objects in a Python program.

Some names, however, are reserved by Python itself. These reserved words are called keywords.

Understanding the difference between keywords and identifiers is essential because keywords cannot be used as identifiers.


What are Keywords? #

Keywords are reserved words that have predefined meanings in Python.

Examples include:

  • if
  • else
  • for
  • while
  • break
  • continue
  • return
  • def
  • class
  • import
  • True
  • False
  • None

Python uses these words to define the language syntax.


Example 1: Displaying Python Keywords #

main.py

import keyword

print(keyword.kwlist)

Sample 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', ...]

Explanation #

  • The keyword module contains information about Python keywords.
  • keyword.kwlist returns a list of all reserved keywords.
  • The exact list may vary slightly depending on the Python version.

Example 2: Checking Whether a Word is a Keyword #

main.py

import keyword

print(keyword.iskeyword("for"))
print(keyword.iskeyword("student"))

Output #

True
False

Explanation #

  • keyword.iskeyword() checks whether a word is a Python keyword.
  • "for" is a reserved keyword, so the result is True.
  • "student" is not a keyword, so the result is False.

Example 3: Valid Identifiers #

main.py

student_name = "Alice"
age = 20
_marks = 95

print(student_name)
print(age)
print(_marks)

Output #

Alice
20
95

Explanation #

  • student_name, age, and _marks are valid identifiers.
  • They follow Python’s naming rules.
  • These names are descriptive and easy to understand.

Example 4: Identifier Names are Case-Sensitive #

main.py

name = "Alice"
Name = "Bob"

print(name)
print(Name)

Output #

Alice
Bob

Explanation #

  • name and Name are different identifiers.
  • Python treats uppercase and lowercase letters as different characters.
  • Identifiers are case-sensitive.

Example 5: Function Names are Identifiers #

main.py

def greet():
    print("Welcome!")

greet()

Output #

Welcome!

Explanation #

  • greet is the name of a function.
  • Function names are identifiers.
  • Python uses the identifier to call the function.

Example 6: Class Names are Identifiers #

main.py

class Student:
    pass

print(Student)

Sample Output #

<class '__main__.Student'>

Explanation #

  • Student is the identifier of the class.
  • Class names are also identifiers.
  • By convention, class names use PascalCase.

Example 7: Module Names are Identifiers #

main.py

import math

print(math.pi)

Output #

3.141592653589793

Explanation #

  • math is the identifier of the imported module.
  • After importing the module, its members can be accessed using the dot (.) operator.

Example 8: Using Meaningful Identifiers #

main.py

employee_salary = 45000

print(employee_salary)

Output #

45000

Explanation #

  • employee_salary clearly describes the stored value.
  • Meaningful identifiers improve readability and maintainability.

Example 9: Identifiers Can Contain Digits #

main.py

student1 = "Alice"
student2 = "Bob"

print(student1)
print(student2)

Output #

Alice
Bob

Explanation #

  • Digits are allowed in identifiers.
  • However, an identifier cannot begin with a digit.

Example 10: Different Types of Identifiers #

main.py

PI = 3.14159

radius = 5

area = PI * radius * radius

print(area)

Output #

78.53975

Explanation #

  • PI, radius, and area are all identifiers.
  • They represent different values in the program.
  • Good identifiers make code easier to understand.

Common Mistakes #

1. Using a Keyword as an Identifier #

Incorrect

for = 10

Output #

SyntaxError: invalid syntax

Reason

for is a reserved keyword and cannot be used as an identifier.

Correct

count = 10

2. Starting an Identifier with a Digit #

Incorrect

2value = 50

Output #

SyntaxError: invalid decimal literal

Reason

Identifiers cannot begin with digits.

Correct

value2 = 50

3. Using Special Characters #

Incorrect

student@name = "Alice"

Output #

SyntaxError: invalid syntax

Reason

Special characters such as @, $, and % are not allowed in identifiers.


4. Using Meaningless Names #

Poor Practice

x = 45000

Better Practice

employee_salary = 45000

Reason

Meaningful identifiers make programs easier to understand and maintain.


Best Practices #

  • Use descriptive names for variables, functions, and classes.
  • Never use Python keywords as identifiers.
  • Follow the snake_case naming convention for variables and functions.
  • Use PascalCase for class names.
  • Keep identifier names clear and meaningful.

Key Points to Remember #

  • An identifier is the name given to a program element such as a variable, function, class, or module.
  • Keywords are reserved words with predefined meanings in Python.
  • Keywords cannot be used as identifiers.
  • Identifiers are case-sensitive.
  • Identifiers must follow Python’s naming rules.
  • Use meaningful names to improve code readability.
  • The keyword module can be used to view and check Python keywords.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *