variables

To store data for later use, we use variables:

x = 666
print(x)
666

A variable is like a box. On the first line, we use = to put something inside the box. On the second line, we type the variable name to get the contents of the box.

NOTE


The "variable is like a box" idea is important to understand. When you see `x = 666`, don't think "x equals 666." Think "set x to 666." When you see `print(x)`, think "print the value of x."

When we print the value of a variable, we don’t use quotes. Notice the difference between these two calls to print:

foo = "bar"
print("foo")
print(foo)
foo
bar

You can print text, variables and numbers at the same time by passing them as separate arguments (or in other words, by separating them with commas):

my_age = 23
print("My age is", my_age)
My age is 23
my_name = "jacob"
print("hello", my_name, 123)
hello jacob 123

If you try to print a variable that you haven’t created yet, your program will crash:

print(baz)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'baz' is not defined

You can take the value of a variable and store it in a new one:

abc = 123
hohoho = abc
print(hohoho)
123

previous: print next: input