Skip to content
Sign upLog in
← Back to Community

Lesson 2: Variables

Profile icon
BartPowers1

Lesson 2: Variables

Now that we know how to make our program say things, let’s talk about variables. A variable is a way to store something. You can store all kinds of things inside variables - words, numbers, pictures, functions, the list goes on! Lets look at an example:

Max is 3 years old, and Hank is 5 years old. Let’s say we want to store their ages in variables. To do that in Python we would do:

maxAge = 3 hankAge = 5 print("Hank is:") print(hankAge)

What do you think this program would say if you ran it?

Hank is: 5

What would you change to make it tell us how old Max is?

print("Max is:") print(maxAge)

When you run this it would say

Max is: 3

Wouldn’t it be cool if we could make our code all print out on the same line like this Max is 3?
To do that and combine the 2 strings simply add them together like this

print("Max is" + maxAge)

This would print out:

Max is 3

Great work! Now let’s do some coding!
Look at the project below… What do you see?

  • We have 3 variables cowSound, pigSound, duckSound
  • We have 3 print commands
  • Press the green play button to see what happens
    Uh-oh! Look what gets printed out when we run our program…
The cow says Moo! The pig says The duck says

Can you figure out how to fix it?

Open the Answers.md file in the project below if you need help

Previous Lesson - Lesson 1: Hello World
Next Lesson - Lesson 3: Lists and Loops

Voters
Profile icon
Juan-DiegoDi332
Profile icon
LuckyOreos
Profile icon
figglediggle
Profile icon
BartPowers1