Naming Conventions and Restrictions in Python#
š Data Analysis Using Python š Python Fundamentals Lesson 006
ā Previous Ā· Next ā¶ Ā· ā Section Ā· ā Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Naming well and legally#
Variable names are subject to rules (what Python allows) and conventions (what good practice recommends), and both matter ā the rules because breaking them is an error, the conventions because they make code readable. This lesson covers Pythonās naming restrictions and conventions, applying the āgood namesā principle to code.
The restrictions (what Python requires)#
Python enforces rules for valid names:
Allowed characters ā names may contain letters, digits, and underscores (
_), and must start with a letter or underscore, not a digit.sales_2024is valid;2024_salesis not.No spaces ā names cannot contain spaces (use underscores:
tax_rate, nottax rate).Case-sensitive ā
sales,Sales, andSALESare three different names.No reserved keywords ā Pythonās keywords (
if,for,class,def,True, and so on) cannot be used as names, since they have special meaning.
Breaking these rules is a syntax error ā the code will not run ā so the restrictions are non-negotiable.
The conventions (what good practice recommends)#
Beyond legality, Python has widely-followed conventions (codified in the style guide PEP 8) that make code readable:
snake_case for variables and functions ā lowercase words joined by underscores:
total_sales,tax_rate,customer_count. This is the standard Python style.Descriptive names ā
total_salesovertsorx; a name should say what the value is. Clarity over brevity (the foundationsā principle).UPPER_CASE for constants ā values meant not to change (
TAX_RATE = 0.08) by convention use all caps.Avoid confusing names ā not single letters like
l(looks like1), not names that shadow built-ins (list,sum,type), which causes subtle bugs.
Conventions are not enforced by Python ā code violating them still runs ā but following them makes code readable to yourself and others, which is why they are near-universal.
Why naming matters#
Good names are documentation that cannot go out of date. monthly_revenue needs no
comment to explain it; mr or x does. As programs grow, the difference between
well-named and cryptically-named code is the difference between maintainable and
baffling ā the exact parallel to well-named versus cryptic data columns. Naming is a
small, constant discipline with a large cumulative payoff in readability, which is why it
is worth doing deliberately from the start.
The caveat#
The two failure modes are opposite. Under-naming ā cryptic abbreviations, single
letters, meaningless names ā makes code unreadable. But over-naming ā names so long
they clutter (the_total_amount_of_sales_for_the_northern_region_this_year) ā harms
readability too, and can tempt breaking the no-spaces rule. The balance is names
descriptive enough to be clear, concise enough to read ā the same judgement as naming
anything. And a name that shadows a built-in (sum = 5 overwriting the sum
function) is legal but causes confusing bugs, so convention rightly warns against it.
Name for the reader, within the rules. The next lesson covers the types of values names
can hold.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/naming-conventions-and-restrictions-in-python/ (insightful-data-lab.com).