Variables

Just like any other programming language, Python also has variables.

>>> x = 4
>>> x * 3
12

Variables can contain any of the types we’ve covered so far: strings, floats, integers…just about anything can be shoved in a variable. When we assign a value to a variable we can do other things with that variable without having to explicitly state their values.

So, variables can be used just like any other value:

>>> socks = 2
>>> shirts = 1
>>> shorts = 1
>>> clothing = socks + shirts + shorts
>>> clothing
4

Your Turn: Variables 🏁

Variable Exercises

Square a Number

Make a variable x that contains your favorite number. Make a variable y that contains the square of your favorite number (x multiplied by itself). Print the values of x and y to make sure they’re correct.

Name

Make a variable name that contains your name.

String Methods with Variables

See if you can figure out how to perform string methods (like .upper()) on strings contained in a variable.

The Life of a Variable

Type this code at the REPL and note what happens. Is the result what you expected?

>>> x = 7
>>> y = 12
>>> z = x * y
>>> z = 10
>>> z