Variables and arithmetic

2.2. Variables and arithmetic

2.2.1. Variables

Variables are well known in mathematics. When we write a = 2, a is the variable name and 2 is the assigned value. This is also the case in programming; a variable is a quantity that can store a value in a program. The variable can be changed throughout the program.

Variables are especially useful when we need to use the same value multiple times, or when we need to update a value during the program. A simple example is if we want to find ten numbers in a certain series of numbers. We can describe this as follows with a pseudocode:

number = 0
repeat 10 times:
    number = number + 3
    show number on screen

The algorithm above finds the first ten numbers in the order 3, 6, 9 … and displays the numbers on the screen as they are calculated. To calculate a new numeric value, we need the previous value of the variable number. We can store the different values in a variable that we can update and change several times in the program.

If we need to use a value in several places, variables are also an advantage. Below is an example of a program code in Python where we calculate the average speed in m/s for various bodies that have moved 3, 4.5, 7 and 14 meters respectively in 3 seconds.

t = 3

v1 = 3/t
v2 = 4.5/t
v3 = 7/t
v4 = 14/t

If we want to calculate the velocity for other values of time, we just need to change the t above. Then t changes each time this variable is used, and we do not have to replace the value in each of the expressions. This is both time-saving and neat.

There are several types of data we can store in variables. In the video below you can see more about different types of data and how we use them in programming.

2.2.2. Arithmetics

We can use Python as a calculator. Some of the operators we can use are \(+\), \(-\), \(*\), \(/\) and \(**\) (\(a**b\) = \(a\) to the power of \(b\)). There are also two special operators: \(//\) and %. Find out what these operators do by trying out the code box below. We have added four lines that you can use as a starting point.

Most mathematical operators and function, like the square root, logarithms and trigonometric functions, are not included in standard Python, but we can import them from libraries, which are packages with useful Python functions. Two useful packages are numpy (numerical Python) and matplotlib.pyplot (plotting), which we will use a lot. We can import functions from these packages in several ways. Study the example below and try to understand how they work:

# First method: Importing only the functions you need
from numpy import sqrt, pi

A = 2 
radius = sqrt(A/pi)

# Second method: Importing the whole library
import numpy

A = 2
radius = numpy.sqrt(A/numpy.pi)

# Third method: Importing the whole library with an alias. This is the most used import method.
import numpy as np

A = 2
radius = np.sqrt(A/np.pi)

# Fourth method: Importing everything from the library without specifying where the functions come from.
# This can lead to trouble when importing several functions, but can be used as a pedagogical tool.
from numpy import *

angle = arccos(0.5)

Discussion

  • In the last excercise you tried to find out what the two operators did by trying and failing. How can programming facilitate the use of trying and failing in your teaching.

  • In programming we can write a = a + 1. How can this use of the equal sign stimulate to dicussion? How can it be a source to confusion?

  • Most of your students does probably have a good deal of experience with programming before they start at higher education. How will you map their competence to be able to do teaching at the appropriate level?