A variable is one of the basic concepts you'll need in almost any Python program.
We did not need variables yet because the programs we showed you were very simple. But to write larger, more useful programs, understanding variables is crucial.
What is a variable?
Essentially, a variable is a storage place that holds a value.
You can store a number in a variable. You can also store a string in it.
Here is a program that stores a number in a variable called
myNumber = 1
Here is a program that stores a string in a variable called
myString = "test"
Example of using a variable
Take a look at the program in the code box.
The program first stores the number
123 in a variable called
program then prints what is stored in the
What do you think the program will print? Run the program and check your answer.
Variables in mathematics
Variables aren't only found in programming. In fact, you may be familiar with variables from mathematics. In the equation:
y = f(x)
x are variables. In other words, they are names that represent,
or take the place of, actual numbers.
The concrete numbers can vary -- hence the name, variable.
Try storing a number
Now change the program to instead store the number
999 in the variable
before it is printed.
Try storing a string
Change the program to instead store the string
"test" in the variable before
printing it. Don't forget the quotes.
This code stores the number
1 in a variable called
myVar = 1
Such a line is called a variable assignment.
myVar is the name of the
1 is the value of the variable.
Use variables from scratch
Now, write a program that creates a variable named
x and assigns the value
1 to it.
You can give a variable almost any name you want. For example, these lines are all valid Python code:
myVar = 123 x = 42 UPPERCASE = 1 empty_string = ""
Variable names cannot contain spaces
You cannot put spaces in the name of a variable, so
my variable = 1 is
not valid code, but
myVariable = 1 and
my_variable = 1 are valid.
Most Python programmers like to use variable names with underscores and all lowercase letters.
Write a program that creates a variable named
x and assigns the value
123 to it.
Then, with another line of code, create a variable named
y and assigns the
456 to it.
Here's an example you can base your solution on:
x = 1 y = 2
Storing and printing
Write a program with three lines of code.
On the first line, make a variable named
x and store
42 into it.
On the second line, make a variable named
y and store
x+x into it.
On the third line, print the value of the
Why use variables?
For small examples, using variables may seem pointless. But for bigger programs variables are essential.
All Python programs work with values, such as strings and numbers. This could be a password, a birthday, an email address, and so on depending on the particular program.
But these values must be stored somewhere if they don't need to be used immediately. Variables can be used to hold a value, so that we can access it at a later point in our program.
Furthermore, variables allow us to give meaningful names to values. For
than just putting
30 somewhere in your code, you can write
age = 30
age from then on. This makes your code much easier to