- What are Keywords?
- Example 1: Displaying Python Keywords
- Example 2: Checking Whether a Word is a Keyword
- Example 3: Valid Identifiers
- Example 4: Identifier Names are Case-Sensitive
- Example 5: Function Names are Identifiers
- Example 6: Class Names are Identifiers
- Example 7: Module Names are Identifiers
- Example 8: Using Meaningful Identifiers
- Example 9: Identifiers Can Contain Digits
- Example 10: Different Types of Identifiers
- Common Mistakes
- Best Practices
- Key Points to Remember
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:
ifelseforwhilebreakcontinuereturndefclassimportTrueFalseNone
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
keywordmodule contains information about Python keywords. keyword.kwlistreturns 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 isTrue."student"is not a keyword, so the result isFalse.
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_marksare 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 #
nameandNameare 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 #
greetis 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 #
Studentis 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 #
mathis 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_salaryclearly 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, andareaare 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_casenaming convention for variables and functions. - Use
PascalCasefor 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
keywordmodule can be used to view and check Python keywords.