Kids Coding
Boxes (Variables)
A variable is a named box. Put a name or a number in it. Use it later.
A box with a name
A variable is a named box. You put a value in it. Later you use the name instead of typing the value again.
Store a name
name = "Sam"
print(name)
print("Hi, " + name)The name goes on the left of =, the value on the right. 5 = age is backwards and Python will complain. Always age = 5.
How to name a box
- Start with a letter.
- Use lowercase and underscores:
high_score. - Do not use spaces.
high scorewill fail. - Do not use a name Python already needs, like
print.
You can change what is in the box
A new = puts a new value in. The old value is gone.
Change a score
score = 0
print("Start:", score)
score = 10
print("Now:", score)Real life
A nickname on a game scoreboard.
You store the name once. Every message can reuse the box.
Scoreboard name
player = "FoxKid"
score = 12
print(player + " has " + str(score) + " points")
score = score + 3
print(player + " now has " + str(score))Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Make a variable pet with an animal name. Print My pet is and the name.
Show solution
pet = "fox"
print("My pet is " + pet)