Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are Python Identifiers?
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.
Rules for Python Identifiers
Python identifiers must follow these rules ?
- Must start with a letter (a-z, A-Z) or underscore (_)
- Can contain letters, digits (0-9), and underscores
- Cannot start with a digit
- Cannot contain spaces or special characters (@, $, %, etc.)
- Case sensitive (Age and age are different)
- Cannot be a Python keyword
Valid and Invalid Examples
Valid Identifiers
# Valid identifier examples name = "John" age_limit = 18 _private_var = 100 user123 = "Alice" MyClass = "Example" print(name, age_limit, _private_var, user123, MyClass)
John 18 100 Alice Example
Invalid Identifiers
# These will cause syntax errors 2name = "Invalid" # Cannot start with digit user-name = "Invalid" # Hyphen not allowed class = "Invalid" # Python keyword user@email = "Invalid" # Special character not allowed
Naming Conventions
Python follows these naming conventions ?
- Variables and functions: Use lowercase with underscores (snake_case)
- Classes: Use CamelCase starting with uppercase
- Constants: Use ALL_UPPERCASE with underscores
- Private variables: Start with single underscore (_variable)
- Name mangling: Start with double underscore (__variable)
- Special methods: Surrounded by double underscores (__init__)
Examples of Naming Conventions
# Variables and functions (snake_case)
user_name = "Alice"
total_count = 50
def calculate_age():
return 25
# Class names (CamelCase)
class StudentRecord:
pass
# Constants (ALL_UPPERCASE)
MAX_SIZE = 100
PI_VALUE = 3.14159
print(f"User: {user_name}, Count: {total_count}")
print(f"Max size: {MAX_SIZE}")
User: Alice, Count: 50 Max size: 100
Reserved Keywords
Python has reserved keywords that cannot be used as identifiers ?
import keyword
# Display all Python keywords
keywords = keyword.kwlist
print(f"Total keywords: {len(keywords)}")
print("First 10 keywords:", keywords[:10])
Total keywords: 35 First 10 keywords: ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def']
Conclusion
Python identifiers must follow specific rules and naming conventions for clean, readable code. Use descriptive names, follow snake_case for variables and CamelCase for classes, and avoid reserved keywords.
