A variable is used to temporarily store a piece of data.

The '=' symbol is used to assign a value to a variable, in Python this is known as the assignment operator'.

For example:

number1 = 10

In the code above the variable is called number1 and the value it is storing is 10. Variables can hold any type of data. Using variables makes it easier for people to understand what is going on.

For example:

cost = 15
VAT = 3
total_cost = cost + VAT

Adding or subtracting values from variables

To add or subtract a value from a variable, use the following code:

score = score + 1
score = score - 1

Casting Variables

Python will automatically decide what type of data a variable should be, sometimes this isn’t right, therefore you will have to convert/cast variables to a different data type.

Integer

The program below will multiply a number by 5. When data is input from the user it will store it as a string. You will need to convert the variable number to an integer before performing a calculation. An example of how you do this is shown below:

number = input("Enter a whole number ")

#converts the variable number to an integer and multiplies it by 5.
answer = int(number) * 5 
print(answer)

Real / Float

The program below will ask the user to enter their weight in kilograms (this could be a decimal) and convert it to pounds. You will need to convert the variable kg to a float before converting it. An example of how you do this is shown below:

kg = input("Enter the weight in KG ")
pounds = float(kg) * 2.2
print(pounds)

String

Using the program above, if you wanted to improve the print message so that it said Your weight in pounds is X you would need to convert the variable pounds to a string as it is currently a float.

print("Your weight in pounds is " + pounds)

This is what would happen if you ran the code currently:

This is because the variable pounds is a float and to be joined with a string in the print message it needs to be converted to a string, the code is below:

print("Your weight in pounds is " + str(pounds))

Now when run the code it will work correctly:

Example program 1 - Water Tank Capacity Program

The code for the program below will allow the user to enter the height, width and depth of a water tank, then calculate and output the capacity.

#three variables that store the inputs as a decimal (float)
height = float(input("Enter the tank height (cm): "))
width = float(input("Enter the tank width (cm): "))
depth = float(input("Enter the tank depth (cm): "))

#calculation to work out the capacity
capacity = (height * width * depth) / 1000

#outputs the capacity of the water tank
print("The tank holds" + round(capacity,2) + "litres of water")

When run in IDLE:

The code above rounds the variable capacity, to round a variable you use the round() function. You write the name of the variable followed by the number of decimal places e.g. round(capacity,2).

Example program 2 - Cylinder Volume Program

The code for the program below will allow the radius and height of a circle, then calculate and output the volume and surface area.

#three variables that store the two inputs and also the value of pie
radius = float(input("Enter the cylinder radius: "))
height = float(input("Enter the cylinder height: "))
pi = 3.14159

#calculations to work out the volume and surface area
volume = pi * (radius * radius) * height
surface = (2 * (pie * (radius * radius))) + (2 * (pi * radius * height))

#outputs the volume and surface area of the cylinder
print("The volume of your cylinder is", round(volume, 2), "to 2 decimal places")
print("The surface area of your cylinder is", round(surface, 2), "to 2 decimal places")

When run in IDLE: