Python Variables: Store, Update, and Reference Values
A variable is a labeled box holding a value. Learn how Python stores and updates values with variable reassignment and the += shorthand operator.
Yesterday's for loop processed all five sales from the week. But where did the total go? The loop ran, the numbers printed out, and then... nothing. It's like the program has amnesia.
That's the core problem we're solving this week. So far every program you've written is a one-time calculation. You call format_sale(), it returns a formatted string, and that's it. In a real Excel spreadsheet, you can keep a running total in a cell. The cell remembers the value even after the formula runs. Python programs need the same thing.
So how do I make Python remember something?
With a variable. A variable is just a labeled box. You put a value inside it, and Python keeps track of where it is. Let me show you:
total = 0
print(total) # Output: 0
You've actually already used variables — remember this from Day 3?
def format_sale(name, amount):
return name + ": $" + str(amount)
Inside the function, name and amount are variables. Someone calls format_sale("Alice Chen", 1250.00) and Python creates two labeled boxes: one called name holding the text "Alice Chen", and one called amount holding the number 1250.00. When the function ends, those boxes disappear.
But variables you create in your main code stick around:
total = 0 # Create a box, put 0 inside
total = total + 100 # Grab what's in the box, add 100, put the result back
print(total) # Output: 100
That second line is the key: total = total + 100. Read it right to left. "Take what's in the total box (which is 0), add 100 to it (getting 100), and put the result back into the total box." Now total holds 100 instead of 0.
So the equals sign doesn't mean "equal" here? It's like... assignment? Put the right side's value into the left side's box?
Exactly. In math, x = 5 means x is 5. In Python, x = 5 means "create a box called x and put 5 in it." When you write x = x + 1, you're not saying "x equals x plus one" — that's impossible mathematically. You're saying "grab the current value from the x box, add 1, and put the new value back."
That's actually less confusing when you explain it that way. I was trying to think about it as math.
Programming borrows = from math and then completely changes what it means. It confuses everyone at first.
Now, updating a variable with total = total + 100 works, but it's verbose. Python has a shortcut called the += operator:
total = 0
total += 100 # Same as: total = total + 100
print(total) # Output: 100
Instead of writing the variable name twice, you just write +=. It means "add the number on the right to what's already in this box."
OK so this is what I'd use if I wanted to track a running total while looping through sales?
Exactly. Here's how you'd use it with a function:
def add_to_total(amount, running_total):
return running_total + amount
total = 0
total = add_to_total(6800.00, total) # total is now 6800.00
total = add_to_total(340.50, total) # total is now 7140.50
total = add_to_total(1250.00, total) # total is now 8390.50
print(f"Running total: ${total:.2f}") # Output: Running total: $8390.50
You can see what's happening: each time you call add_to_total, you pass in the current total, the function adds the sale amount to it, and returns the new total. Then you assign that new total back to the total box. The box gets updated three times.
So my program actually remembers the sum now, even after the function returns?
Your program remembers because total is a variable in the main part of your code, not inside a function. That's the whole difference. Functions are temporary — the boxes they create vanish when the function ends. Variables you create in your main code stay alive until your program finishes.
Wait, but if I create variables inside the function, they disappear? That seems wasteful.
It's not wasteful, it's intentional. Functions are self-contained. If every variable you created lived forever, you'd have thousands of boxes piling up, with no idea which ones are still being used. Python automatically cleans up function variables when the function finishes. It's good housekeeping. But here's the catch: tomorrow we're going to look at why 100 + "50" breaks your code, and why Python is actually being smart about types even when it feels like it's just being difficult.
Practice your skills
Sign up to write and run code in this lesson.
Python Variables: Store, Update, and Reference Values
A variable is a labeled box holding a value. Learn how Python stores and updates values with variable reassignment and the += shorthand operator.
Yesterday's for loop processed all five sales from the week. But where did the total go? The loop ran, the numbers printed out, and then... nothing. It's like the program has amnesia.
That's the core problem we're solving this week. So far every program you've written is a one-time calculation. You call format_sale(), it returns a formatted string, and that's it. In a real Excel spreadsheet, you can keep a running total in a cell. The cell remembers the value even after the formula runs. Python programs need the same thing.
So how do I make Python remember something?
With a variable. A variable is just a labeled box. You put a value inside it, and Python keeps track of where it is. Let me show you:
total = 0
print(total) # Output: 0
You've actually already used variables — remember this from Day 3?
def format_sale(name, amount):
return name + ": $" + str(amount)
Inside the function, name and amount are variables. Someone calls format_sale("Alice Chen", 1250.00) and Python creates two labeled boxes: one called name holding the text "Alice Chen", and one called amount holding the number 1250.00. When the function ends, those boxes disappear.
But variables you create in your main code stick around:
total = 0 # Create a box, put 0 inside
total = total + 100 # Grab what's in the box, add 100, put the result back
print(total) # Output: 100
That second line is the key: total = total + 100. Read it right to left. "Take what's in the total box (which is 0), add 100 to it (getting 100), and put the result back into the total box." Now total holds 100 instead of 0.
So the equals sign doesn't mean "equal" here? It's like... assignment? Put the right side's value into the left side's box?
Exactly. In math, x = 5 means x is 5. In Python, x = 5 means "create a box called x and put 5 in it." When you write x = x + 1, you're not saying "x equals x plus one" — that's impossible mathematically. You're saying "grab the current value from the x box, add 1, and put the new value back."
That's actually less confusing when you explain it that way. I was trying to think about it as math.
Programming borrows = from math and then completely changes what it means. It confuses everyone at first.
Now, updating a variable with total = total + 100 works, but it's verbose. Python has a shortcut called the += operator:
total = 0
total += 100 # Same as: total = total + 100
print(total) # Output: 100
Instead of writing the variable name twice, you just write +=. It means "add the number on the right to what's already in this box."
OK so this is what I'd use if I wanted to track a running total while looping through sales?
Exactly. Here's how you'd use it with a function:
def add_to_total(amount, running_total):
return running_total + amount
total = 0
total = add_to_total(6800.00, total) # total is now 6800.00
total = add_to_total(340.50, total) # total is now 7140.50
total = add_to_total(1250.00, total) # total is now 8390.50
print(f"Running total: ${total:.2f}") # Output: Running total: $8390.50
You can see what's happening: each time you call add_to_total, you pass in the current total, the function adds the sale amount to it, and returns the new total. Then you assign that new total back to the total box. The box gets updated three times.
So my program actually remembers the sum now, even after the function returns?
Your program remembers because total is a variable in the main part of your code, not inside a function. That's the whole difference. Functions are temporary — the boxes they create vanish when the function ends. Variables you create in your main code stay alive until your program finishes.
Wait, but if I create variables inside the function, they disappear? That seems wasteful.
It's not wasteful, it's intentional. Functions are self-contained. If every variable you created lived forever, you'd have thousands of boxes piling up, with no idea which ones are still being used. Python automatically cleans up function variables when the function finishes. It's good housekeeping. But here's the catch: tomorrow we're going to look at why 100 + "50" breaks your code, and why Python is actually being smart about types even when it feels like it's just being difficult.