Python variable naming conventions provide guidelines for naming variables in Python code, promoting code readability and maintainability.
Following conventions such as using lowercase letters, separating words with underscores, and using descriptive names helps make code more understandable.
Avoiding reserved keywords, choosing meaningful names, and following module/package naming conventions further, enhance consistency and clarity. Adhering to these conventions leads to well-structured Python code that is easier to read, comprehend, and maintain.
Here are the commonly accepted naming conventions in Python:
- Use lowercase letters: Variable names should be in lowercase, separated by underscores (_). For example:
my_variable
,count
. - Be descriptive: Use meaningful names that convey the purpose or content of the variable. This helps in understanding the code’s logic and makes it easier to maintain. For example:
student_name
,total_count
. - Avoid reserved keywords: Do not use reserved keywords as variable names. For example,
if
,while
,for
,class
, etc., are reserved and cannot be used as variable names. - Use snake_case: Multiple words in a variable name should be separated by underscores (_). This convention is called snake_case. For example:
first_name
,total_count
. - Avoid single-letter names (unless in specific contexts): While single-letter variable names like
x
,y
, andi
are sometimes used in mathematical or loop iterations, it’s generally recommended to use more descriptive names. - Use lowercase for constants: If you want to define constants, use uppercase letters with underscores. This convention is called SCREAMING_SNAKE_CASE. For example:
MAX_VALUE
,PI
. - Use self-explanatory names for objects and classes: When defining objects or classes, use nouns or noun phrases to make their purpose clear. For example:
Car
,Employee
. - Follow module/package naming conventions: For modules or packages, use lowercase letters, without underscores, to represent the name. For example:
math
,os
.
Note: these conventions are not enforced by the Python language itself, but they are widely followed by the Python community.
Here’s a summary of Python variable naming conventions presented in a tabular format:
Convention | Example |
---|---|
Use lowercase letters | variable_name |
Separate words with underscores | snake_case_variable_name |
Be descriptive | descriptive_variable_name |
Avoid reserved keywords | avoided_keyword |
Use uppercase for constants | SCREAMING_SNAKE_CASE_CONSTANT |
Avoid single-letter names | avoided_single_letter_name |
Use self-explanatory names for objects/classes | object_name , ClassName |
Follow module/package naming conventions | module_name , package_name |
Python variable naming conventions example
Here’s an example of Python variable names following the naming conventions in a real-world scenario:
# Variable names following Python naming conventions
# Lowercase letters
first_name = "John"
last_name = "Doe"
age = 25
# Separate words with underscores (snake case)
total_count = 100
average_score = 85.5
is_active = True
# Be descriptive
student_name = "Alice"
num_of_classes = 5
max_attempts = 3
# Avoid reserved keywords
my_value = 10
class_name = "Physics"
element_count = 20
# Use uppercase for constants (screaming snake case)
MAX_VALUE = 100
PI = 3.14159
DEFAULT_TIMEOUT = 10
# Avoid single-letter names
x = 10
y = 20
index = 0
temp = 25
# Self-explanatory names for objects/classes
car_brand = "Toyota"
employee_name = "John Smith"
person_age = 30
# Follow module/package naming conventions
import math
import os
import datetime
Output:
Do comment if you have any doubts or suggestions on this Python basic topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.