1.3. Variables#

1.3.1. Introduction#

Python stores data in variables. To assign data to a variable we use the = sign.

For example,

x = "Alison"

One way to think of variables is to treat them like a box. In the above example, we have a box named x, and the box is storing the value 10.

../../_images/variable_in.png

When we want to use the value x, we look for the box labelled x and extract the contents.

../../_images/variable_out.png

Here is an example:

x = "Alison"
print(x)

Note that the variable x does not have quotes but the string ‘Alison’ does!

1.3.2. Exercises#

Question 1

What do you think the output of the following code will be?

name = 'Steve'
print('Hello')
print(name)
Hello
Steve
Hello
name
Hello
Steve
Question 2

What do you think the output of the following code will be?

x = '3'
print('My lucky number is')
print('x')
My lucky number is
3
My lucky number is
x
My lucky number is
3
lock This solution is locked.
Question 3

Which of the following are valid? Select all that apply.

print(x)
'message' = Happy Birthday
print(message)
day = 'Monday'
print('Today is')
print(day)
print('I do not like')
print(dislike)
dislike = 'eggs and ham'