Variables and Assignment
Concepts covered: Variables
A Python variable is a name attached to a value. The line quantity = 3 builds the integer object 3 and binds the name quantity to it, with no declaration beforehand and no type written next to the name. The type belongs to the value, so the same name can point at an integer on one line and a string on the next, and type(quantity) reports whatever the name currently points at.
Assignment reads right to left
Python evaluates everything to the right of the = first, producing a single value, and only then binds the name on the left to it. That order is what makes quantity = quantity + 1 legal: the right side computes the current 3 plus 1, and the name is then rebound to the new 4. The 3 itself is untouched, because rebinding moves the name rather than changing the object. The 4 lines below show what that means for a second name that shared the old value.
quantity = 3
reserved = quantity
quantity = quantity + 1
print(quantity, reserved)4 3
The second line binds reserved to the same 3, so for a moment both names sit on one object. The third line builds a new 4 and moves only quantity. reserved still reads 3, so the last line prints 4 3. An int cannot be changed in place, so rebinding is the only way a counter changes, and 2 names on one int never interfere. The picture of names as labels on objects matters again for lists, which can change in place under every name bound to them.
Values carry the type, names carry nothing
user_id = 8814
event = "checkout"
quantity = 3
unit_price_cents = 4999
is_member = True
print(user_id, event, quantity, unit_price_cents, is_member)
print(type(user_id).__name__, type(event).__name__, type(is_member).__name__)
quantity = "3"
print(type(quantity).__name__)Output:
8814 checkout 3 4999 True int str bool str
The first printed line shows 5 values of 3 different kinds, each under a name that says what it holds. type(...).__name__ asks for the type's bare name: int for 8814, str for the event text, bool for is_member. After the rebinding on the last 2 lines, quantity reports str, because it now points at the text "3", and nothing warned that a count had turned into text. The loader that stored 33 went wrong at exactly this point.
Legal names and good names
A name is letters, digits and underscores, and it cannot begin with a digit. Names are case-sensitive, so quantity, Quantity and QUANTITY are 3 unrelated variables. A capitalisation typo on the left of an = silently creates a new name and leaves the intended one unchanged. The same typo on a name being read raises NameError on exactly that line. Python's style guide, PEP 8, asks for lowercase snake_case, unit_price_cents rather than unitPriceCents, and the libraries a data engineer reads follow it.
- unit_price_cents: lowercase words joined by underscores, with the unit in the name
- 2nd_order: refused with a SyntaxError, a name cannot start with a digit
- unit-price: parsed as unit minus price, 2 names and a subtraction
- sum = 0: legal, and it shadows the built-in sum() for the rest of the file
- unitPriceCents: legal camelCase, which reads as another language's habit
Python refuses its 35 reserved keywords as names with a SyntaxError, so class = "gold" never runs. Built-in names such as sum, max and list are ordinary names. sum = 0 runs, and the next sum(prices) in that file fails with TypeError: 'int' object is not callable, possibly 200 lines from the assignment that caused it.
A name that was never bound
The most common error in a new programmer's first week is a NameError: the code reads a name that no assignment has bound yet, because of a typo, a capital letter, or an assignment that sits below the line that uses it. Python reports the exact name it could not find, and the fix is to match the spelling of the name that was bound. The program below computes a subtotal and stops on its last line.
The error names Subtotal_cents, with a capital S, and that spelling appears nowhere on the left of an =. Reading the name in the message and searching the file for it is the whole technique, and it works the same way when the missing name is a variable assigned further down the file than the line that reads it.
About this lesson
This section is part of the Python Foundations: 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.