Browser lab15 min+60 XP

Variables

Teach Python to remember things. Store a player's name, level and coins.

0% done

Code runs right here in your browser. Nothing to install.

TASK 1

Learn it

Key idea

A variable is a labelled box. name = value puts the value in the box.

Key words
computer memory"neo_42"username7level250coins7level = 7read it right to left: put 7 intothe box called level
Three variables: each name labels a box that holds a value.

A variable is like a labelled box in the computer's memory. You put a value inside, give the box a name, and use that name whenever you need the value again.

You create a variable with the equals sign. Read it right to left: level = 7 means "put 7 into the box called level". This is called assignment. It does not mean "equals" like in maths.

You can change what is in the box later: level = 8 replaces the old value. Naming rules: use letters, numbers and underscores, do not start with a number, and no spaces. Names are case sensitive, so Score and score are different boxes. Python programmers write player_name style names, called snake_case.

Real life linkYour favourite game saves your username, level and coins. Each one is a variable that the game reads and updates as you play.
Example code
username = "neo_42"
level = 7
coins = 250
print("Player:", username)
print("Level:", level)
level = level + 1
print("Level up! Now level", level)
TASK 2

Try it yourself

+10 XP

Create three variables: name (any name in quotes), health set to 100 and coins set to 1000. Print each one with a label.

Create a free account to start this lab

Sign in to write, run and check your code here. Your progress, XP and code are saved to your account. No card needed.

TASK 3

Capture the flag

+10 XP

After running level = 7 and then level = level + 3, what value is stored in level?

Create a free account to start this lab

Sign in to answer the flag question. Your progress, XP and code are saved to your account. No card needed.

HOMEWORK

The great swap

+20 XP

Two variables hold the wrong items: left_hand = "shield" and right_hand = "sword". Swap them using a third variable called temp, then print both. The output must show left_hand: sword and right_hand: shield.

Create a free account to start this lab

Sign in to do this homework and have it checked. Your progress, XP and code are saved to your account. No card needed.