Challenge 1-4: Variables

Save a Value

Now let's look at another way of working with data in Python - something called a variable.

Suppose you use a math expression to calculate a value. We've done this already. It's pretty simple, right?

Let's try this example. Type 6 times 6 and hit Enter (in the Python Shell).

>>> 6 * 6

36

The value you get back should be 36.

But what if you want to use that value again?

You could type 6 times 6 again, but if you were writing a program you might have to type that a lot.

Luckily Python gives us an easier way to do it. You can give your value a name, then you can use that name over and over again.

Take a look at our next example. Here we're using the name 'puppies' then saying that puppies is equal to 6 times 6.

Go ahead and type the second example exactly as it's written here and hit Enter:

>>> puppies = 6 * 6

We're assigning the value of 6 times 6 to the variable puppies .

You shouldn't get any answer back - we haven't asked for one yet. But type the word puppies again and hit Enter. Do you see your value this time?

>>> puppies

36

Go ahead and type and enter the word puppies a few times. You should always get the same answer back.

>>> puppies

36

>>> puppies

36

>>> puppies

36

Notice that in our example the word puppies does not have any quotes around it.

If it had quotes around it, Python would treat it like a string.

Without quotes, Python knows that it's a variable.