Variables
Teach Python to remember things. Store a player's name, level and coins.
Code runs right here in your browser. Nothing to install.
Learn it
A variable is a labelled box. name = value puts the value in the box.
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.
username = "neo_42" level = 7 coins = 250 print("Player:", username) print("Level:", level) level = level + 1 print("Level up! Now level", level)
Try it yourself
+10 XPCreate 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.
Capture the flag
+10 XPAfter 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.
The great swap
+20 XPTwo 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.