Lesson 3 of 7
Variables
Store, update, and manage information in your programs using Python variables and data types.
Learn it
Imagine a variable as a storage box with a label on the front. You can place a piece of information inside the box, look at it whenever you need, and replace it with something new later.
In Python, creating a variable is as easy as choosing a name and using the equals sign to store a value. You can store words, whole numbers, decimals, or true and false values.
Key terms
- Variable
- A named container in computer memory used to store and retrieve data.
- Assignment
- The act of storing a value into a variable using the equals sign (=).
- String
- A data type representing a sequence of text characters wrapped in quotes.
- Integer
- A data type representing whole numbers without any decimal point.
Tracking Player Stats in Memory
See how variables allow a game engine to keep track of a player's evolving status.
- 1Declaration: The game initialises a variable named score with a starting value of 0.
- 2Reading Value: The UI system reads the current value of score to draw it on the game heads-up display.
- 3Update via Action: When the player collects a gold coin, the program calculates score + 50.
- 4Reassignment: The new total of 50 is saved back into the score variable box, overwriting the old 0.
Creating and Updating Game Variables
pythonplayer_name = "Aria"
health = 100
gold_coins = 25
print(player_name)
print("Health remaining:", health)
gold_coins = gold_coins + 10
print("Total gold:", gold_coins)We assign initial values to text and numerical variables, then increase the gold_coins variable by 10 and print the updated value.
Try it
Select the correct code token to assign the value 500 to the variable player_score.
player_score______500
print(player_score)Challenge
Create three variables: hero_name (text), health_points (integer), and shield_active (Boolean set to True). Print all three.
Pick whichever way suits you — every mode earns the same bonus XP.
Write at least 40 more characters to submit.
Mark your own work
Guided walkthrough — 0/5 clues revealed
- Clue 1 locked — reveal it only if you get stuck.
- Clue 2 locked — reveal it only if you get stuck.
- Clue 3 locked — reveal it only if you get stuck.
- Clue 4 locked — reveal it only if you get stuck.
- Clue 5 locked — reveal it only if you get stuck.
Each clue costs 4 XP (never below 18 XP). You'd earn 35 XP right now.
Extension: Simulate taking damage by subtracting 25 from health_points and printing the updated total.