Variables
Variables are one of the most basic and essential concepts in programming, used to store values.
What is a Variable?
A variable has a name, and you can store something in it.
The image below shows how we can think of a variable named favFruit
, with the value 'apple'
stored inside it.
Below is how we can create the favFruit
variable, using Python code:
The code above creates a variable named favFruit
, and the equal sign =
is used to store the value 'apple'
inside the variable.
The reason for giving a variable a name is to be able to use it later in the code, and also to know what value it holds.
Creating a Variable
Below is the code for creating the favAnimal
variable in different programming languages.
favAnimal = 'turtles'
Run Example »Variables can hold different types of data, like whole numbers, decimal numbers, or text.
Note: When creating a variable in programming languages like C/C++ and Java, we must tell the computer what type of data the variable holds. To do that we need to write for example int
in front of the variable name, if the variable holds a whole number (integer).
Doing Things with Variables
Like we have just seen in the previous example, a value can be stored in a variable. And if you run the example code above, you see how a variable is printed.
We can do other things with variables as well, like math operations, or put variables with text strings together.
Add a Variable to a String
To use a variable in a string, you can add it to the string, like this:
a = 'Jane'
print('Hello, my name is ' + a)
Run Example »Add Two String Variables Together
You can add two string variables together to form a sentence, using the +
operator, like this:
a = 'Jane'
b = 'My name is '
print(b + a)
Run Example »Add Two Number Variables
If the variables are numeric values, you can perform mathematic operations on them, like adding two numbers:
a = 2
b = 3
print(a + b)
Run Example »or other mathematical operations, like division:
a = 12
b = 3
print(a / b)
Run Example »Another way to add two variables, is to make an extra variable c
to hold the sum, and present the answer with a text string:
a = 2
b = 3
c = a + b
print('The sum a + b is ' + str(c))
Run Example »Note: The +
operator is used to both add numbers, and to put strings together. In Python and C++ we need to convert a number to a string before we can put it together with a string.
Incrementing a Variable
We can create a variable, and update the value by adding 1 to it, like this:
a = 2
a = a + 1
print(a)
Run Example »Incrementing a variable is a common operation in programming, and it's often used in loops. It is so common in fact, that many programming languages have a shorthand for it, like ++
in C/C++ and Java, or +=
in Python.
The code below shows how to increment a variable in different programming languages, using shorthand.
a = 2
a += 1
print(a)
Run Example »Decrementing a Variable
If we want to decrement a variable, we can do that in a similar way as incrementing. And the number we want to decrement by can be any number, not just 1.
The code below shows how to decrement a variable by 3 in different programming languages, using shorthand.
a = 5
a -= 3
print(a)
Run Example »Using a Variable in an if Statement
We can use a variable in an if statement, as part of the condition, like this:
temperature = 25
print('Temperature: ' + str(temperature) + '°C')
if temperature > 20:
print('It is warm')
else:
print('It is not warm')
Run Example »The Variable Name
There are certain rules that applies when naming a variable. Some rules are programming-languange-specific, other applies to all programming languages:
- A variable name cannot contain spaces.
- A variable name cannot start with a number.
- A variable name cannot be a reserved word like if, else, for, function etc.
For readability, it is common to use camelCase or snake_case when naming variables, so instead of myfavanimal
, we can use myFavAnimal
or my_fav_animal
.