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[2] are used to store data[4] 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[2] (and functions) in Python[5] we use an _ to separate different words.

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

We cannot start variables with a number.

In programming[1] 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[6] 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[7] 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. Variables. “Containers” used to label and store data in memory. The data can then be used throughout a program: * Integer[10] variables use only numbers and so can be used for calculations * String[11] variables are sequences of code that may contain numbers, letters, and other characters, and so cannot be used for calculations.
3. variable. A placeholder for a piece of information that can change.
4. data. Raw facts or figures without context[12] or meaning. e.g. 1, “Boris”, False
5. Python. 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. programming. 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. Variables. 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. variable. 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. Python. 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 […]