Skip to content

Python variable naming conventions

  • by

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:

  1. Use lowercase letters: Variable names should be in lowercase, separated by underscores (_). For example: my_variable, count.
  2. 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.
  3. 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.
  4. 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.
  5. Avoid single-letter names (unless in specific contexts): While single-letter variable names like x, y, and i are sometimes used in mathematical or loop iterations, it’s generally recommended to use more descriptive names.
  6. 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.
  7. 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.
  8. 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:

ConventionExample
Use lowercase lettersvariable_name
Separate words with underscoressnake_case_variable_name
Be descriptivedescriptive_variable_name
Avoid reserved keywordsavoided_keyword
Use uppercase for constantsSCREAMING_SNAKE_CASE_CONSTANT
Avoid single-letter namesavoided_single_letter_name
Use self-explanatory names for objects/classesobject_name, ClassName
Follow module/package naming conventionsmodule_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:

Python variable naming conventions

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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading