Variables and basic calculations


A computer program is essentially an implementation of one or more algorithms. Algorithms, in turn, consist of descriptions of the concrete steps that need to be followed to arrive at a specific result. Computer programs translate these steps or activities into concrete instructions or commands. These instructions can be referred to, more generally, as statements. In the Python language, the individual statements are delineated by hard returns. The end of the statement (i.e. the hard return) is a cue which triggers the Python parser to interpret and to execute the statement.

Printing output

Programs are generally written to solve specific problems, and they usually produce some output. This output can be the result of a calculation, for instance. The print() function can be used to communicate the output of the program to the user. As will be explained in more detail later, a function is a named collection of statements. The print() function is always used with parentheses. Inside the parentheses, you can provide the text that you would like the computer to display. This text must be provided either in single or in double quotes.

In [ ]:
print('This program works!')

You can also format the text, to some extent. The character '\t' prints a tab, and '\n' creates a hard return. Note that the print() function also adds a line breaks itself.

In [ ]:
print('This sentence is followed by two line breaks. \n')
print('This line contains \ta tab. ')


Virtually all programming languages make use of variables of some sort. Variables can be thought of as containers or as boxes in which information can be stored temporarily. Variables are always given a name. This name can be used to retrieve the 'contents' of the variable. These contents (the data referred to by the variable) is generally referred to as the variable's value.

Variables names can consist of any combination of alphanumerical characters that you can think of. Underscores are also allowed in variable names. One important rule, however, is that variable names cannot begin with a number and they cannot contain spaces. Although it is possible to work with any sequence of characters, your script will evidently be most understandable when you make use of variable names which are meaningful. Note that there are a number of reserved words which have a particular function in the Python language, and which cannot be used as variable names for this reason. The list of reserved words include ‘for’, ‘in’, ‘is’, ‘print’, ‘global’, ‘def’, ‘if, ‘elif’ and ‘else’.

As mentioned, variables can be given a value. The act of giving a value to a certain variable is called assignment. In the first example, the variable named placeName is assigned the value ‘Leiden’.

In [ ]:
placeName = 'Leiden'
numberOfInhabitants = 123924

Data types

Values are always of a specific type. In the example above, the variable placeName is assigned a string value. Simply put, a string is a sequence of alphanumeric characters and spaces. Strings can be created either with single quotes or with double quotes. The variable 'numberofInhabitants' is an example of an integer (i.e. a natural number). Next to such integers, Python can also work with rational numbers. They are called floats or floating point numbers. Unlike strings, numbers are not surrounded by quotes.

In [ ]:
pi = 3.14159265359

The type of a variable can always be found by using the type() function, as follows:

In [ ]:
age = 20
print( type(age) )
## this will print <class 'int'>


Incidentally, the example above also illustrates another feature of the Python language. Lines can be preceded by the hash (‘#’) symbol. Lines that start with a hash are called comments. Such comments can be very useful when you want to document your code. They can be used to explain what happens exactly in specific parts of the code. Comments are ignored by the Python interpreter.

Mathematical operators

Computer are generally written to process certain data values. Values can be manipulated by making use of so-called operators. They are symbols that represent specific actions. They can be applied to values, or to variables representing certain values. The values that are combined with these operators are called the operands.

Numerical values can be used in combination with mathematical operators:

+ Addition
- Subtraction
* Multiplication
/ Division

The code below contains a number of examples of how such mathematical operators can be used.

In [ ]:
# Firstly, we create two values a and b
# and assign them numerical values

a = 4
b = 3

c = a + b

# c now has value 7

c = a * b ;
#c now has value 12

c = a - b
# c has value 1

c = c+1
#c now has value 2

c += 1

# c now has value 3.
# c += 1 is a shorthand notation for  c = c + 1