Variables and Naming

Concepts covered: Variables

A name is the label you stick on an object. Python lets you spell it with letters and digits plus the _ character, as long as the first character is not a digit. Upper and lower case count as different characters: total is one name, and Total and TOTAL are 2 more. A space ends a name, as does a hyphen or any other punctuation, which is why order total reads as 2 names and order-total reads as a subtraction.

Which spellings Python accepts

NameAccepted?Why
row_countyesletters and an underscore
row2yesa digit may appear after the first character
_cacheyesan underscore may start a name
2nd_rownoa name cannot begin with a digit
row-countnothe hyphen is read as subtraction
row countnothe space splits it into 2 names
classnoa keyword Python reserves for itself
Classyeskeywords are case sensitive, so this spelling is free

The last 2 rows point at a short list of words with fixed meaning in the language. True, False, None, and, or, not, if, for, while, def, class, import, return are on it, and the list has a few more. Assigning to one of them stops the program before it starts, with a SyntaxError on that line.

membership_class = "gold"
class_ = "gold"

Case matters, and so does spelling

Because Total and total are unrelated names, a typo in one place produces a name that was never assigned. Python reports that the moment the line runs, after any earlier lines have already produced their output.

Total = 40
print(Total)
print(total)

Names that already mean something

print is a name too, attached at startup to the built-in object that writes to the screen. Nothing stops you from reattaching it to an integer. The assignment succeeds, and the very next call fails, because an integer can't be called.

print = 5
print("hello")

Conventions that make names readable

Python only enforces the spelling rules above. The rest is convention, followed almost universally so that code reads the same across teams. Variable names are lowercase words joined by the _ character, which is called snake case. A value meant to stay fixed for the whole program is spelled in capitals, such as MAX_RETRIES = 3, a signal to readers rather than a lock. And a name says what the value is, so that a line can be read without scrolling up to the assignment.

Names that hide the meaning
  • x = 1200
  • tmp = x * 12
  • d = 30
  • flag = tmp == 14400
Names that carry it
  • monthly_rent = 1200
  • yearly_rent = monthly_rent * 12
  • days_in_month = 30
  • is_full_year = yearly_rent == 14400

In pipeline code the strongest names say where a value came from and what unit it is in. orders_raw_count and orders_clean_count can sit side by side without confusion, and timeout_seconds can't be mistaken for milliseconds. A name that took 5 extra characters to type saves the next reader a trip through the whole file.

Do
  • Use snake_case for variables: rows_loaded, not rowsLoaded
  • Spell constants in capitals: BATCH_SIZE = 500
  • Name the unit when it matters: timeout_seconds, size_mb
  • Make booleans read as a question: is_ready, has_header
Avoid
  • Reuse one letter for several things: x for a price and later for a count
  • Shadow a built-in: list, sum, max, min, print
  • Encode the type in the name: str_name, int_count
  • Rely on case to tell 2 names apart: Total and total

Which line will Python refuse to run?

A NameError quotes the exact spelling Python looked up and failed to find. Compare it character by character with the assignment that was meant to create it: a single capital letter makes a separate name. The misspelled assignment ran without complaint, so the error appears later, at the first line that reads the name.

About this lesson

This section is part of the Python Expressions: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.