Variables & assignment
Bind names to values with =, reassign them, and use them in expressions.
Names for values, and why they matter
Every nontrivial program builds a result in steps: read an input, transform it, combine it, return it. A variable pins an intermediate value to a name so you can reuse it without recomputing, and so the next person (often you, a week later) can read what the code means. total_price tells a reviewer what a number is. x makes them guess. A good name is the cheapest documentation you will ever write.
Assignment binds a name to a value
Read = as "gets", not "equals":
score = 10 # the name score now refers to 10
name = "Ada" # name refers to the string "Ada"
The right-hand side is evaluated first, then the name is pointed at the result. That is why rebuilding a value from its old self works:
score = 10
score = score + 5 # RHS 10 + 5 runs first, then score is re-pointed to 15
= is an instruction ("make this name refer to that value"), not a claim that two things are already equal.
A worked example
width = 4
height = 3
area = width * height
print(area) # 12
width = 10 # reassign width only
print(width * height) # 30
Notice that area is computed once and stays 12. Rebinding width to 10 does not reach back and update area, because area holds the number that width * height produced at that instant, not a live formula.
Name things clearly
Use lowercase words joined by underscores (snake_case) and pick names that say what the value holds:
total_price = 4.99
items_in_cart = 3
You can compute into a well-named variable and then return it, or return the expression directly when it is a one-liner. Both are clear:
def rectangle_area(width, height):
area = width * height # store, then return
return area
def rectangle_area(width, height):
return width * height # return the expression directly
Pitfalls
- Reassignment does not recompute earlier results. As above,
areastays12afterwidthchanges. If you need the updated area, recompute it:area = width * height. - Using a name before it is assigned raises
NameError. The name must be bound on some line that actually runs before you read it, soscore = score + 5fails ifscorewas never given a starting value.
Interview nuance: in Python a variable is a name bound to an object, not a box that stores the value. Assignment never copies the object; it just points a name at it. For numbers and strings this is invisible, but the same rule means two names can refer to the same list, so mutating through one name is visible through the other. Remembering that "assignment rebinds, it does not copy" is what saves you from aliasing bugs later.
width = 4
height = 3
area = width * height
print(area) # 12
width = 10 # reassign
print(width * height) # 30Apply
Your turn
The task this lesson builds to.
Implement rectangle_area(width, height): return the area of a rectangle.
The area is width * height. You may store it in a variable first or return the expression
directly.
3 hints and 4 automated checks are waiting in the workspace.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Implement seconds_total(hours, minutes): convert a duration to total seconds.
One hour is 3600 seconds and one minute is 60 seconds. Combine both parts.
2 hints and 4 automated checks are waiting in the workspace.