Nearby lessons
12 of 108Python - Identifiers
Introduction
An Identifier is a name used to identify different elements in a Python program.
Identifiers are used to name:
- Variables
- Functions
- Classes
- Modules
- Objects
Choosing meaningful identifiers makes programs easier to read and understand.
Example of an Identifier
In the following example, a is an identifier.
main.py
a is an identifier.
Rules for Naming Identifiers
Python has some rules for creating valid identifiers.
Rule 1: Allowed Characters
An identifier can contain only:
- Alphabets (A-Z, a-z)
- Digits (0-9)
- Underscore (_)
Valid Examples:
main.py
All identifiers are valid.
Invalid Characters
Special characters such as $, @, #, and % are not allowed.
main.py
SyntaxError
Rule 2: Identifier Cannot Start with a Digit
An identifier cannot begin with a number.
Invalid Example:
main.py
SyntaxError
Valid Example
main.py
Valid Identifier
Rule 3: Identifiers are Case Sensitive
Python is a case-sensitive language.
This means identifiers with different letter cases are treated as different names.
main.py
10 999
Rule 4: Reserved Words Cannot Be Used
Python keywords (reserved words) cannot be used as identifiers.
Invalid Example:
main.py
SyntaxError
Rule 5: No Length Limit
Python does not limit the length of an identifier.
However, very long names should be avoided because they reduce readability.
Rule 6: Dollar Symbol is Not Allowed
The dollar symbol ($) is not allowed in Python identifiers.
main.py
SyntaxError
Valid and Invalid Identifier Examples
| Identifier | Status |
|---|---|
| total123 | Valid |
| java2share | Valid |
| abc_abc | Valid |
| 123total | Invalid |
| ca$h | Invalid |
| def | Invalid |
| if | Invalid |
Special Meaning of Underscore (_)
Identifiers with underscores have special meanings in Python.
Single Underscore (_name)
An identifier starting with a single underscore indicates that it is intended for internal use.
main.py
Single underscore identifier
Double Underscore (__name)
An identifier starting with two underscores is used for stronger name protection inside classes.
main.py
Double underscore identifier
Double Underscore on Both Sides (__name__)
Identifiers that start and end with double underscores are special Python names, often called magic methods.
main.py
Special Python identifiers
Examples of Identifiers
Variable Identifier
main.py
Variable identifier
Function Identifier
main.py
Hello
Class Identifier
main.py
Student class created
Best Practices
Always use meaningful names for identifiers.
Good Examples:
main.py
Meaningful identifiers
Poor Naming Examples
main.py
Avoid using unclear names.
Benefits of Meaningful Identifiers
- Improves code readability.
- Makes programs easier to maintain.
- Follows clean coding practices.
- Helps other developers understand the code.
Key Points
- Identifiers are names used for variables, functions, classes, modules, and objects.
- Only alphabets, digits, and underscores are allowed.
- Identifiers cannot start with a digit.
- Python is case-sensitive.
- Reserved words cannot be used as identifiers.
- The dollar symbol ($) is not allowed.
- Use meaningful names for better readability.
Quick Summary
| Rule | Description |
|---|---|
| Allowed Characters | Letters, digits, and underscore (_) |
| Starting Character | Cannot start with a digit |
| Case Sensitive | Yes |
| Reserved Words | Cannot be used |
| Dollar Symbol | Not allowed |
| Length Limit | No limit |