A variable is basically just a storage place to put your numbers and strings. You can
use variables just as if they are regular numbers or strings, and even create new
variables with them. To use them type the name of the variable first, then “=” then
what you are assigning
a = 1 b = 2 c = "My new number is:" d = a + b print "a is", a print "b is", b print c, d b = 1 print c, d
a is 1 b is 2 My new number is: 3 My new number is: 3
What’s going on? We have assigned the number 1 to a, the number 2 to b, and the
string “My new number is:” to c. In d, we assigned the value of a plus the value of b.
Notice that the value of d doesn’t change after we changed the value of b in line 8. d
actually holds the number 3, it doesn’t hold a and b.
a = 3 b = 1 print a, b a = b print a, b
3 1 1 1
Huh? The first three lines should be obvious. Let’s take a closer look at the 4th line.
When we called a = b we assigned the value of b into a, making the output ‘1 1’. The
order is very important. Had we called b = a this would have made the value of each
- Just remember left side is the storage, right side is the assignment.
It is also possible to assign the same value to more than one variable:
a = b = c = 50
which is the same as:
a = 50 b = 50 c = 50
- Make a variable for the string “My favorite number is: “. Make a variable with any
number. print the output.
- Add on to the first program by making another variable with the value of. Make
your output My favorite number squared is: (your # squared). Make sure to use both