Python Identifiers & Reserved Keywords (Detailed Guide)
๐น Identifiers in Python
What is an Identifier?
An identifier is the name used to identify variables, functions, classes, modules, and objects in Python.
โ Examples of Identifiers:
name = "Alice" # Variable identifier
def add_numbers(): # Function identifier
pass
class Student: # Class identifier
pass
Rules for Naming Identifiers in Python
- Can contain letters (A-Z, a-z), digits (0-9), and underscores (_)
- โ
my_variable
,Class1
,num_2
are valid.
- โ
- Must start with a letter (A-Z, a-z) or an underscore (_) but NOT a digit (0-9)
- โ
_temp
,var_name
are valid. - โ
2variable
is invalid.
- โ
- Cannot be a Python reserved keyword (e.g.,
if
,else
,while
)- โ
class = 10
is invalid becauseclass
is a reserved word.
- โ
- Identifiers are case-sensitive
MyVar
,myvar
, andMYVAR
are different.
- Can be of any length but should be meaningful for readability.
- Cannot contain special characters (
@, #, $, %, &
, etc.).- โ
user@name
is invalid.
- โ
โ Valid Identifiers:
age = 25
_name = "Alice"
class1 = "Python" # Not a reserved keyword
โ Invalid Identifiers:
2name = "Bob" # Starts with a digit โ
user@name = "Tom" # Special character โ
class = "Math" # Reserved keyword โ
๐น Python Reserved Keywords
Reserved keywords (also called keywords) are special words in Python that cannot be used as identifiers because they have a predefined meaning.
List of Python Keywords (Python 3.x)
['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']
Using keyword
Module to Get Keywords
You can check all Python keywords using the keyword
module:
import keyword
print(keyword.kwlist) # Prints a list of all reserved keywords
Examples of Reserved Keywords Usage
โ Correct Usage (As Keywords)
if True:
print("This is valid") # 'if' is used correctly as a keyword
def my_function(): # 'def' is correctly used to define a function
pass
โ Incorrect Usage (As Identifiers)
class = "Python" # โ Invalid: 'class' is a keyword
def = 10 # โ Invalid: 'def' is a keyword
โ Corrected Version
class_name = "Python" # โ
Valid identifier
function_name = 10 # โ
Valid identifier
๐น Summary
Feature | Identifiers | Keywords |
---|---|---|
Definition | User-defined names for variables, functions, etc. | Reserved words with predefined meanings |
Rules | Letters, digits, and _ , cannot start with digits or special characters |
Cannot be used as variable or function names |
Examples | my_var , Student , _temp123 |
if , else , def , class , import |
Case-Sensitive? | โ
Yes (MyVar โ myvar ) |
โ
Yes (True is different from true ) |
Customizable? | โ Yes | โ No |
โ Conclusion
- Identifiers are names given to variables, functions, and classes, but they must follow certain rules.
- Reserved Keywords are built-in words in Python that cannot be used as identifiers.
- Use meaningful identifiers to make code readable.
Would you like examples of best practices for naming variables and functions? ๐
Leave a Reply