0 of 6 lessons complete (0%)
Getting Started in Python

# Variables

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 10 minutes

Variables are used to store data and the contents of them can be changed. This could be strings (text), numbers (integers, floats / decimals), or booleans (true or false).

When naming variables (and functions) in Python we use an _ to separate different words.

For example a variable to store a person’s first name could be first_name

We cannot start variables with a number.

In programming the = symbol is known as the assignment operator. It is used to assign (store) data in a variable.

We assign what is on the right of the = to the variable on the left of the =.

To the right of the = we can carry out the processing to modify the data to store in the variable.

Below are five examples of variables that have data stored in them.

```first_name = "Lenny"
surname = "Learner"
age = 15
account_balance = 3.50
is_gamer = True```

We can change the data stored in a variable as shown below.

Lines 2 and 4 are to show the output that has changed.

```price = 3.5
print("Price before: ", price)
price = 4
print("Price after: ", price)```

Another thing that you might want to do with variables is switch the values between two variables.

For example if a = 5 and b = 10 we might want to switch them to a = 10 and b = 5

To achieve this we need to create a placeholder variable. If we don’t do this and use the code below we will lose the contents of one vairable.

```a = 5
b = 10
print("A: ", a, "B: ", b)
a = b
b = a
print("A: ", a, "B: ", b)```

The code above will print

``````A: 5 B: 10
A: 10 B: 10``````

This isn’t what we want.

The code below uses a placeholder value to temporarily hold the value of the variable that we are overwriting first.

```a = 5
b = 10
print("A: ", a, "B: ", b)

temp = a
a = b
b = temp
print("A: ", a, "B: ", b)```

Variables are really important and useful and will be used thoroughly throughout your programming journey.

Terms definitions
2. “Containers” used to label and store data in memory. The data can then be used throughout a program: * Integer variables use only numbers and so can be used for calculations * String variables are sequences of code that may contain numbers, letters, and other characters, and so cannot be used for calculations.
3. A placeholder for a piece of information that can change.
4. data. Raw facts or figures without context or meaning. e.g. 1, “Boris”, False
5. A type of programming language. It is one of many high-level, general-purpose programming languages. It was first released in 1991
Terms definitions
1. Length: 10 minutes Variables are used to store data and the contents of them can be changed. This could be strings (text), numbers (integers, floats / decimals), or booleans (true or false). Variables must start with a letter. When naming variables (and functions) in Python we use an _ to separate different words. For example […]
2. Length: 10 minutes Variables are used to store data and the contents of them can be changed. This could be strings (text), numbers (integers, floats / decimals), or booleans (true or false). Variables must start with a letter. When naming variables (and functions) in Python we use an _ to separate different words. For example […]
3. Length: 10 minutes Variables are used to store data and the contents of them can be changed. This could be strings (text), numbers (integers, floats / decimals), or booleans (true or false). Variables must start with a letter. When naming variables (and functions) in Python we use an _ to separate different words. For example […]
4. data. Length: 10 minutes Variables are used to store data and the contents of them can be changed. This could be strings (text), numbers (integers, floats / decimals), or booleans (true or false). Variables must start with a letter. When naming variables (and functions) in Python we use an _ to separate different words. For example […]
5. Length: 10 minutes Variables are used to store data and the contents of them can be changed. This could be strings (text), numbers (integers, floats / decimals), or booleans (true or false). Variables must start with a letter. When naming variables (and functions) in Python we use an _ to separate different words. For example […]