This lab sheet will move on to understanding how to write functions (very similar to mathematical functions) and lists (a way of holding data).
Another type of variable in Python (we have already seen numeric and character variables) is the list. This type acts as a container that can hold multiple other items in an ordered way.
Here is a list of my favourite numbers:
>>> favourite_numbers = [9, 12, 13, 7] # Defining a list >>> favourite_numbers [9, 12, 13, 7] >>> type(favourite_numbers) <class 'list'>
We can do various things to the items in the list:
>>> sum(favourite_numbers) # Adding all the elements of our list 41 >>> max(favourite_numbers) # Getting the largest element of a list 13 >>> min(favourite_numbers) # Getting the minimum element of a list 7 >>> favourite_numbers.append(-100) # Add another element to a list >>> favourite_numbers [9, 12, 13, 7, -100]
We can also go in to our lists and get specific items. This works just as it did with strings:
>>> favourite_numbers # Getting the first element of a list 9 >>> favourite_numbers # Getting the second element of a list 12 >>> favourite_numbers[-1] # Getting the last element of a list -100 >>> favourite_numbers[2:4] # Getting the 3rd till the 4th element of a list [13, 7]
Strings (see in in the previous lab sheet) and lists are similar in that they are ‘containers’ for items. A lot of the manipulation that works with lists also works with strings:
>>> firstname = "Vince" >>> len(firstname) # How many characters are in the variable 5 >>> firstname # We can point at individual characters, 0 is the first 'V' >>> firstname 'e' >>> firstname[-1] # We can use negative number to start counting from the end 'e' >>> firstname[0:4] # We can 'slice' strings 'Vinc'
Experiment by creating lists and manipulating them.
In the previous sheet, we saw that a
whileloop can be used to repeat a code chunk until a boolean condition is
False. It is also possible to repeat an action for all elements of a list.
>>> items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Creating a list >>> total = 0 >>> for item in items: ... total += item >>> total 55
A quick way to get a list of integers is the
>>> for k in range(10): ... print(k) 0 1 2 3 4 5 6 7 8 9
Experiment by summing (or perhaps multiplying?) over a different list of items.
Here is a list of squares, done using the
appendmethod and a for loop.
>>> total = 0 >>> square_of_favourite_numbers =  # Create an empty list >>> for n in favourite_numbers: ... square_of_favourite_numbers.append(n ** 2) >>> square_of_favourite_numbers [81, 144, 169, 49, 10000]
We can however do this using some nice Python syntax called a list comprehension:
>>> square_of_favourite_numbers = [x ** 2 for x in favourite_numbers] >>> square_of_favourite_numbers [81, 144, 169, 49, 10000]
This is familiar, as it replicates mathematical notation. For example here is how to get the sum of the elements in the following set:
(This is mathematical notation for “the set of all things in \(S\) that are divisible by 2”.)
>>> sum([n for n in favourite_numbers if n % 2 == 0]) -88
Experiment by modifying this code to create a different list.
Writing simple functions.
Often we want to be able to use code more than once. The way to do this is to write a function. Here is a very simple example. This creates a function that returns a string saying “Hello world”:
>>> def say_hi(): ... return "Hello world"
Now, to use that function we need to call the function:
>>> say_hi() 'Hello world'
It is good practice to break down code in to smaller functions that make it easier to read.
Experiment with changing what the
Functions with variables.
It is more useful to include variables in our functions (in the exact same way as for mathematical functions!).
Let us revisit the mathematical function we described in the previous lab sheet:
Here is the code that defines this function (compare it to the code we wrote in the previous lab sheet):
>>> def f(n): ... """ ... This is text in between triple quoatation marks is a doc string. ... We use it to describe what a function does. For example here we would ... write: This function returns f(n) as described above. ... """ ... if n <= 5: ... return 1 ... elif n % 2 == 0: # Otherwise if (else if) ... return 2 ... else: # Otherwise ... return 3 >>> f(11) 3
We can also have functions with more than 1 variable:
>>> def simple_sum(a, b): ... """Returns the sum of a and b""" ... return a + b >>> simple_sum(5, 7) 12
Finally, it is also possible to have default variables:
>>> def simple_sum(a=5, b=1): ... """Returns the sum of a and b""" ... return a + b >>> simple_sum() 6 >>> simple_sum(b=2) 7 >>> simple_sum(a=3) 4
Experiment by creating your own function.
The Fibonacci numbers are defined by the following mathematical formula:
The goal of this question is to verify the following theorem about the sum of the first Fibonacci numbers:
As an example with \(n=3\) we have: \(f_0=f_1=1, f_2=2, f_3=3\) and \(f_5=8\). We indeed have:
We are going to automate checking the formula using Python. Let us first write a function for the Fibonacci sequence itself:
>>> def fibonacci(n): ... """Returns the n th fibonacci number""" ... if n in [0, 1]: # The special case of n being 0 or 1 ... return 1 ... a = 1 # Setting the starting values ... b = 1 ... for i in range(n - 1): # Going through the first n - 1 terms ... temp = b # A temporary variable to remember the value of b ... b = a + b # New value of b ... a = temp # New value of a (old value of b) ... return b
Let us now call this function to check that it is correct:
>>> for n in range(5): ... print(fibonacci(n)) 1 1 2 3 5
We can now obtain a list of the first \(K\) fibonacci numbers and check the theorem:
>>> K = 3 >>> list_of_fib = [fibonacci(n) for n in range(K + 1)] >>> sum(list_of_fib) == fibonacci(K + 2) - 1 True
We can put this code that checks if a relationship is true in to a new function:
>>> def check_theorem(K): ... """ ... Check the relationship for the sum of the first K fibonacci numbers ... """ ... list_of_fib = [fibonacci(n) for n in range(K + 1)] ... return sum(list_of_fib) == fibonacci(K + 2) - 1
Let us now check that our theorem can be checked for the first 200 values of:
>>> checks = [check_theorem(K) for K in range(200)] >>> all(checks) # `all` combines all booleans in a list True
These questions aim to push a bit further.
The following is an attempt to write \(n!\) as a function. Find and fix all the bugs.
def factorial(n): """A function that returns factorial n""" for i in range(n) prod *= i
Write a function that returns the triangular numbers \(T_n\):
Use code to check that the following relationship is true:
(Optional) Create a list with the first 1300 integers divisible by 3. What is the largest such number? What is the smallest such number? What is the mean of these numbers?
(Optional) Investigate the use of the python
randomlibrary. Use this to simulate the Monty hall problem:
- Write a function that simulates the play of the game when you ‘stick’ with the initial choice.
- Write a function that simulates the play of the game when you ‘change’ your choice.
- Repeat the play of the game using both those functions and compare the probability of winning.
(Optional) A data type that we have not considered are dictionaries. These are a specific type of what is generally called a ‘hash table’. Find information about dictionaries and experiment with them.