This first lab sheet will introduce you to programming. You are expected to work through all the exercises marked as prior to and including the worked example. There are other exercises that aim to push your understanding further. Finally at the end of the sheet there are links to other resources and tutorials that you might find helpful and/or interesting.

There are videos throughout the sheets describing the concepts as well as showing demos.

Here is an overview of the course.

Installing and running Python

Python is a programming language. There are various other programming languages:

  • Java
  • C
  • C++
  • Ruby
  • VBA
  • and many more.

A programming language allows you to write a program which is a sequence of instructions that specifies how to perform a computation.

When writing a program you need two things:

  • Something to save the code (a text editor for example)
  • Something to run the code

We will be using a combination of these 2 things called notebooks.

Install Python

There are various distributions of Python, we will use Anaconda which comes packaged with a variety of other useful tools (including the notebooks I mentioned above).

To install it on your personal machine follow these steps:

  1. Go to this webpage:
  2. Identify and download the version of Python 3 for your operating system (Windows, Mac OSX, Linux).
  3. Run the installer.

We will use a Jupyter notebook which runs in your browser. To open a local server find the Continuum navigator and click on Jupyter. You do not need to be connected to the internet to use this.

This video is a demo of using a notebook.

These lab sheets will include code snippets. They show what code you should write but also the output you should see. Try the following:

>>> print("Hello world")  # Code you should write and below is the output
Hello world

Building blocks

These questions aim to show you the basic building blocks of programming

  1. Creating numeric variables.

    A video describing the concept.

    A video demo.

    One of the building block of any programming language is variables. This is how we store a particular variable that we can reuse:

    >>> age = 20  # Pointing the variable age at the numeric value 20
    >>> print(age)  # Recalling the value of the variable

    It is possible to carry out a variety of numeric operations and reassigning the value of the variable:

    >>> age = age + 1  # Adding 1 to age
    >>> print(age)

    Python has some short hand for the above:

    >>> age += 1  # Adding 1 to age
    >>> print(age)

    We can do more than just addition (experiment with these as you might need them later on):

    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Exponentiation: **
    • Modulo division: %

    We can check the type of our variables:

    >>> type(5)  # An integer
    <class 'int'>
    >>> type(5.4)  # A 'float'
    <class 'float'>

    Experiment with manipulating numeric variables.

  2. Creating character variables.

    A video describing the concept.

    A video demo.

    Another type of variable used is called a character variable. In programming these are called strings.

    >>> firstname = "Vince"
    >>> print(firstname)
    >>> print(type(firstname))  # Checking the type
    <class 'str'>

    We can also create new strings from other ones:

    >>> lastname = "Knight"
    >>> fullname = firstname + " " + lastname
    >>> fullname
    'Vince Knight'

    Experiment by creating your own strings variables.

  3. Boolean variables and if statements.

    A video describing the concept.

    A video demo.

    Programming languages can be used to check if statements are true or not. A variable that can either be True or False is called a boolean variable. Here is a simple example:

    >>> a = 4
    >>> b = 8 / 2
    >>> a == b  # Check if a is equal to b
    >>> a != b  # Check if a is NOT equal to b
    >>> a >= b  # Check if a is bigger or equal to b
    >>> a < b + 1  # Check if a is stricly small than b + 1

    Note that we can set the statement to be a variable itself:

    >>> statement = a == b
    >>> statement
    >>> type(statement)  # This variable is a boolean
    <class 'bool'>

    We can use this to carry out different operations depending on whether or not a boolean is True or False.

    >>> n = 11  # Experiment by changing the value of n
    >>> if n <= 5:
    ...    value = 1
    ... elif n % 2 == 0:  # Otherwise if (else if)
    ...    value = 2
    ... else:  # Otherwise
    ...    value = 3
    >>> value

    The indentation is important: everything indented after the if, elif, else statements is what will be evaluated in that specific case.

    The above is in essence producing:

    Experiment by changing the function \(f\) and modifying the code.

  4. While loops.

    A video describing the concept.

    A video demo.

    It is possible to use code to repeat various actions. Here is a while loop that repeats whatever is indented until the boolean condition is no longer true:

    >>> count = 0  # A variable to count
    >>> total = 0  # We will sum the first ten numbers
    >>> while count < 10:  # Keep repeating until count if >= 10
    ...     count += 1  # Adding 1 to count
    ...     total += count  # Adding the count to the total
    >>> total

    Experiment by summing (or perhaps multiplying?) over a different list of items.

  5. Worked example

    A video describing the concept.

    A video demo.

    This is a slightly more complex example that brings together the various concepts above.

    Let us aim to verify (this is not a proof!) that the following statement is true:

    We will do this by computing the left hand side and the right hand side:

    >>> n = 20
    >>> rhs = n * (n +  1) * (2 * n + 1) / 6
    >>> lhs = 0
    >>> i = 0
    >>> while i < n:
    ...     i += 1
    ...     lhs += i ** 2
    >>> lhs == rhs

    We can put all of the above in a while loop that will check it for a large number of values of n:

    >>> max_n = 2000
    >>> n = 0
    >>> while n < max_n:
    ...     n += 1
    ...     rhs = n * (n +  1) * (2 * n + 1) / 6
    ...     lhs = 0
    ...     i = 0
    ...     while i < n:
    ...         i += 1
    ...         lhs += i ** 2
    ...     if lhs != rhs:
    ...         print(False)

    Further work

    These questions aim to push a bit further.

  6. Debugging exercise

    A video demo.

    Below is code that attempts to verify the following identity for all values less than 2000.

    There is at least one error (also called a bug) in the code. Find and fix all the bugs.

    max_n = 2000
    n = 0
    while n > max_n:
        n += 2
        rhs = ((n ** 2 + 2 * n) ** 2) / 4
        lhs = 0
        i = 0
        while i < n:
            i += 1
            lhs += i ** 2
        if lhs != rhs:
  7. Use code to check the following identity:

    for (n=20).

  8. (Optional) Modify the above (Q7) to check it for all values less than 2000.

  9. (Optional) Calculates the sum of the first natural numbers less than 1000 that are not divisible by 3.

  10. (Optional) Calculates the sum of the first 1000 natural numbers that are not divisible by 3.

  11. (Optional) It can be shown (you are not required to check this) that the following sequence:

    approaches \(\sqrt{K}\) as \(n\) increases. Write some code to verify this to any given level of precision.

Further resources


You can find the solutions here: