Back

Activity 1.5: Variable Reassignment

divider

Activity 1.5

Variable Reassignment

Key Concepts

Changing Values Over Time

Reassignment and Self-Referential Updates

Reassigning a Variable

Once a variable exists, you can give it a new value at any time — just write the variable name and a new value. No let or special keyword needed in Python.

Reassigning a Variable

Demo
health = 100
print(health)
health = 50
print(health)
Demo
100
50

Self-Referential Updates

A variable can even appear on both sides of the = sign. Python evaluates the right side first using the old value, then stores the result as the new value.

Self-Referential Updates

Demo
health = 100
print(health)
health = health - 20
print(health)
Demo
100
80

A Common Mistake

Beginners often write the calculation and forget to store it. Python works out the answer, then throws it away — and there is no error message to tell you.

A Common Mistake

Demo
health = 100
print(health)
❌health - 20
print(health)
Demo
100
100

Nothing changed. Without the health = in front of it, the new value has nowhere to go.

Today's Objectives

  • Reassigning a variable's value
  • Updating a variable based on its own current value
  • Tracking a value as it changes over time

Key Terms

Reassignment
Giving an existing variable a new value.
Self-Referential Update
An assignment where a variable's current value is used to compute its new value, like health = health - 20.
State
The current value(s) a program is keeping track of at a given moment.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-5-reassignment.


Task 1: Start With a Value

  • Create a health variable set to 100 and a score variable set to 0.
  • Print each variable with a label.
Variable Reassignment
health = 100
score = 0
print("Health:", health)
print("Score:", score)
#

Task 2: Take Damage

  • Reassign health to itself minus 30.
  • Print the updated value with a label.
Variable Reassignment
health = 100
score = 0
print("Health:", health)
print("Score:", score)
health = health - 30
print("Health:", health)
#

Task 3: Earn Points

  • Reassign score to itself plus 50, then print it.
  • Reassign score to itself plus 25 a second time, then print it again.
Variable Reassignment
health = 100
score = 0
print("Health:", health)
print("Score:", score)
health = health - 30
print("Health:", health)
score = score + 50
print("Score:", score)
score = score + 25
print("Score:", score)
#

Challenge (Optional): Heal Up

  • Reassign health one more time to represent healing (add some points back), then print the updated value.
  • Compare the final value to the value you started with.
divider

Checkpoint

Verify your program works correctly.

Example Output
Health: 100
Score: 0
Health: 70
Score: 50
Score: 75
divider

Reflection

Answer the following questions before submitting your work.

  1. In Python, making a brand new variable and giving an existing one a new value look identical — both are just health = something. What is different about what Python does the second time?
  2. In health = health - 30, which value does Python use to compute the right side of the = — the old value or the new value?
  3. Why might a program need to update the same variable more than once while it's running?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete