Nearby lessons
12 of 159Python - Identifiers
- Understand what identifiers are in Python
- Learn the rules for naming identifiers
- Know which names are allowed and which are not
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.
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:
Invalid Characters
Special characters such as $, @, #, and % are not allowed.
Rule 2: Identifier Cannot Start with a Digit
An identifier cannot begin with a number.
Invalid Example:
Valid Example
Rule 3: Identifiers are Case Sensitive
Python is a case-sensitive language.
This means identifiers with different letter cases are treated as different names.
Rule 4: Reserved Words Cannot Be Used
Python keywords (reserved words) like if, def, class, and for cannot be used as identifiers.
For the full list of reserved words, visit Python Reserved Words.
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.
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.
Double Underscore (__name)
An identifier starting with two underscores is used for stronger name protection inside classes.
Double Underscore on Both Sides (__name__)
Identifiers that start and end with double underscores are special Python names, often called magic methods.
Examples of Identifiers
Variable Identifier
Function Identifier
Class Identifier
Best Practices
Always use meaningful names for identifiers.
Good Examples:
Poor Naming Examples
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 |
- Identifiers are names used for variables, functions, classes, etc.
- Can contain letters, digits, and underscores — cannot start with a digit
- Cannot use Python reserved words as identifiers
- Python is case-sensitive — Name and name are different
- Use meaningful names like student_name, not x or y