Python variables

We are going to start with our first important programming concept: Variables. 

You probably recognize the name from math, where we call \(x\) a variable. Just like in math it's a name that stands for some piece of information. In programming this can be a number, but can also be a piece of text, a name, or a photo.

Creating variables

A variable is a location in the computer's memory where we can store something, perhaps a piece of text, maybe a name, or a number, like 42. We can see it as a box where we save something and bring it out when we need to. 

Every variable has a name and contains something. In Python we can write it like this (right now, this program does nothing visible): 

This code creates three different variables. In Python, we must always specify the variable's name, and what information it contains.

Important: We always use a dot do denote decimals.

Important: If it's a text, we must use quotation marks (' or ") around the text, just like around the name marcus above.

Now you can try running the following two lines:

print("marcus")
print(name)

They print the same thing! When Python tries to execute the second instruction, it looks up the variable name (it opens the box) and finds the text "marcus" inside, and hence that's what gets printed.

We can create as many variables as we want and name them to (almost) whatever we want, as we shall see now.

Naming variables

Variable names may contain letters, numbers and underscores (_). But the name cannot start with a number. The following names are thus allowed:

# Variable 1
a_namne
# Variable 2
names_for_everyone_above_15
# Variable 3
MYVARIABLE

Try to always name variables something that describes what they are used for, and your code will be easier to understand.

Just like we can print the contents of a variable we can also use them as parts of calculations. For example: 

sum = x + y
print(sum)

This gives us 7.7 (remember the variables in the beginning?).

Text plus text 

We can also add text, for example like this: 

firstname = "julius"
lastname = "caesar"
print("My name is " + firstname + " " + lastname)

A slightly nicer way of writing that last row looks like this:

print(f"My name is {firstname} {lastname}")

It's up to you what makes most sense.

Remember - all code is run top to bottom. The code on the first line is executed, then the second, then the third etc. Empty lines are ignored. That's why this would not work:

print("My name is " + firstname + " " + lastname)
firstname = "julius"
lastname = "caesar"

Python starts on the first line and is told to print the contents of the variable firstname, but it does not exist, because it's defined on line two. It is very important that things occur in the right order, just like in a baking recipe. We must make the batter before the cake goes in the oven.

Now that we've created and used a few variables, we are going to talk about the different types of data we can store in variables, and how we can mix them to create smarter programs.

Read page in other languages

In this video, we learn more about the usefulness of variables.

  • Variable: A part of the computer's memory where we can store something, like a piece of text, a name, or a number.