Card 2: Variables

Module 1: The Basics

1 The Hook

Think of a Variable like a labeled storage box.

  • The Label is the variable name (e.g., score).
  • The Content is what's inside (e.g., 100).

You can change what's inside the box, but the label stays the same.

2 Worked Example

Read the comments to see how we fill the boxes.

username = "Ninja" # Create box 'username' and put "Ninja" inside
score = 500 # Create box 'score' and put 500 inside
print(score) # Look inside 'score' box and show us (500)

Power Up: Modulo (%)

The % symbol doesn't mean percent. It means Remainder.

10 % 3 = 1 // 3 goes into 10 three times, remainder 1

10 // 3 = 3 // Integer Division (How many times 3 fits in 10)

2 ** 3 = 8 // 2 to the power of 3 (2*2*2)

PREDICT

What will happen when we overwrite a variable?

lives = 3
lives = 0
print(lives)

Hint: A variable can only hold ONE thing at a time. The new value kicks the old one out.

Interactive Editor

> _
BRONZE (MODIFY)

🥉 New High Score

1. Copy the code below.
2. Change the score from 100 to 999.

score = 100
print(score)
> _
SILVER (FIX)

🥈 Backwards Code

Variables work like this: Box = Value.
The code below is backwards. Fix it!

> _

Hint: The Variable Name (Label) always goes on the LEFT.

GOLD (MAKE)

🥇 Game Profile

Create a profile for a game character using 3 variables:

  • hero (store a name in quotes)
  • level (store a number)
  • lives (store a number)
  • Then print all 3 variables.
> _