Arithmetic Operations
An arithmetic expression combines values, variables and arithmetic operators, and is evaluated to produce one value. For example, if a swimmer completes 12 lengths of a 25 metre pool, an expression using the variables lengths and pool_length is evaluated to 300. The values and the multiplication are worked out together to give that single result.
Once an expression has produced a value, you can either store it in a variable or display it. Storing uses assignment: the result is placed in a named variable and can be used again later. Displaying uses output, such as print(), which shows the value on screen. Displaying a value does not store it. If you only display an expression, the program works out the value, shows it, then has no variable holding it.
This distinction matters when you need the result more than once. A stored result can be printed, used in another calculation, or changed later. A displayed result disappears once it has been shown, so you cannot refer to it by name. If you need the value again, assign the expression to a variable first.
lengths = 12
pool_length = 25
distance = lengths * pool_length
print(distance) # stored result
print(lengths * 2) # displayed only
print(distance) # unchangedOutput:
300
24
300
The program stores the result of one expression and displays a different expression. The middle printed value is worked out and shown, but no variable holds it. The last line prints the stored result again, which is unchanged because displaying a calculation does not save its value.
OCR GCSE Computer Science · Arithmetic Operations