Clear variable names are part of teaching your future self how a program works. They reduce the need for comments explaining obvious calculations.
Understanding the Concept
Use lowercase words separated by underscores, called snake_case, for ordinary Python variable names. Avoid single-letter abbreviations outside very small local contexts, avoid names that look like built-ins such as list or input, and choose units when they matter. Names are case-sensitive, so customerTotal and customer_total are different identifiers. Consistent naming makes debugging and collaboration easier.
When and Why You Would Use This
A conversion calculation is clearer when both the original and converted units appear in the names. The code communicates intent before the reader knows the exact numbers.
Practical Example
distance_kilometers = 8
kilometers_to_miles = 0.621371
distance_miles = distance_kilometers * kilometers_to_miles
print(round(distance_miles, 2))
Understanding the Example
The result is 4.97. The names explain why the multiplication occurs. A one-letter name for the distance would be less helpful in a real project where several measurements appear.
Example Output
4.97
Try It Yourself
Rename three vague variables in one of your earlier calculator projects so their purpose is obvious without comments.
Common Beginner Mistakes
- Using names that shadow built-in functions such as str, list, or input.
- Ignoring units and confusing meters with kilometers or dollars with cents.
- Mixing naming conventions unpredictably within the same project.
Key Takeaways
- Prefer descriptive snake_case names for variables.
- Include units in names when they are important.
- Avoid shadowing common built-in functions.