-
What is Code?
-
Data
-
Input
-
Functions
-
Boolean logic and decisions
-
Loops
-
Modules
-
Examples
-
Exercises
Variables
We can store data using a variable. A variable has a name and a value. A name must be unique.
We can create a variable and assign it a value with an instruction.
Here are some examples:
age = 42
name = "bob"
numberOfChickens = 11
The equals character =
is known as the assignment instruction.
It assigns the value on the right of the equals character to a variable on the left. Like this:
name = value
So, in the following example code, the =
assigns the value 42
to a variable called age
.
age = 42
If age already has a value, the old value is replaced with the new value.
So in this code
count = 1
count = 2
count = 3
Count will have the value 3 at the end of these instructions.
name = "bob"
name = "alice"
Name will have the value "alice"
at the end of these instructions.
There are strict rules to follow when naming variables or the computer won't be able to understand them.
We can't use any space characters in the variable name.
We can't use special characters either.
See what errors you get when you run this code and have a go at fixing them.