This is the last boring lesson before we start something fun. In this lesson, we are going to learn about variables in python. As you may know, a variable is like a box, it stores a value. For example, if is x is our variable than x is the name of the box.
We can set x equal to a value by using the code:
x = some_value
If we want to set x to the string “Hello World” we can type:
x = “Hello World”
We are putting “Hello World” into the box x. If we want to change that to “Goodbye World”, we can take “Hello World” out of the box and put “Goodbye World” in using:
x = “Goodbye World”
Variables don’t just have to be strings, they can be any data type. If we wanted to make x a integer, we can do:
x = 4
If we do this, we can add variables. Try typing the line of code above followed by:
Try it yourself
Try setting a variable a to 5 and a variable b to 10. Now set a variable c equal to a + b. Print c and see it’s value. It should be equal to 5 + 10 (15).
b = 10
c = a + b
Did it work? Here is one last Challenge. Try to set name to “No name” and then print it. Than change name to whatever your name is and print that.
Now that we can save information in a “box” and store it for later, we can get started with interactive code in the next lesson.