Worksheet

This page contains a number of questions and exercises to give you a chance to practise what you have learned this session. You should create a new .py Python file for each exercise.

Exercise 1

The first exercise is to practice searching the documentation. From the list of modules in the standard library, find one that contains a function to give the current date and time.

Fill in the ... in the snippet below.

import ...

time_now = ...

print(time_now.isoformat())

The output should be something like the following, but with today’s date and time:

2048-08-32T10:53:15.062603

The function we can use is datetime.datetime.now(). There is an example in that module as well.

import datetime

time_now = datetime.datetime.now()

print(time_now.isoformat())

Run the code cell above to see the output.

2025-09-15T09:28:43.952895
Exercise 2

Write a function which can accept a string as an argument and return the first word in that string. To start you off, here’s skeleton of what the function should look like.

def first_word(l):
    ...
    return ...

you should be able to use it like:

sentence = "This is a collection of words"
word = first_word(sentence)
print(word)

giving the output:

This

You could create a file called ex2.py containing:

ex2.py
def first_word(l):
    words = l.split()
    the_first_word = words[0]
    return the_first_word

You can then use it in a notebook cell like:

import ex2
sentence = "This is a collection of words"
word = ex2.first_word(sentence)
print(word)
This
Exercise 3

Write a function called count_word_match which accepts three agruments:

  1. a string which contains multiple words separated by spaces,
  2. a string which gives the word that you want to count the occurrences of and
  3. a boolean (True or False) which specifies whether the match should be case-sensitive.
def count_word_match(words, match, case_sensitive):
    ...
    return ...

you should be able to use it like:

count1 = count_word_match("To be or not to be", "to", True)
print(count1)

count2 = count_word_match("To be or not to be", "to", False)
print(count2)

giving the output:

1
2

You could create a file called ex3.py containing:

ex3.py
def count_word_match(words, match, case_sensitive):
    if not case_sensitive:
        # Make both the words and the match a consistent case
        words = words.casefold()
        match = match.casefold()
    
    word_list = words.split()
    
    count = 0
    for word in word_list:
        if word == match:
            count += 1
    
    return count

You can then use it in a notebook cell like:

import ex3
count1 = ex3.count_word_match("To be or not to be", "to", True)
print(count1)
count2 = ex3.count_word_match("To be or not to be", "to", False)
print(count2)
1
2
Exercise 4

For this exercise, you should write a function which can find references, like [4], in some text.

If the function is passed a string like:

"I recommend this book [1] but the other book [3] is better. Some people think that this website [10] is the best but I prefer this [7] one."

it should return a list of integers like:

[1, 3, 10, 7]

The function should be called find_references.

This exercise it trickier, but if you make some assumptions about the format of the input, you can make your life easier. For example, you can assume for now that every reference like [4] is surrounded by spaces. This allows str.split() to pull each one out as a “word”. You can also assume that every reference is an integer number.

You could create a file called ex4.py containing:

ex4.py
def find_references(text):
    words = text.split()
    
    refs = []
    # For each word in the text
    for word in words:
        # if it's surrounded by square brackets
        if word[0] == "[" and word[-1] == "]":
            # grab the bit between the square brackets
            reference = word[1:-1]
            # convert it to a number
            reference_number = int(reference)
            refs.append(reference_number)
    
    return refs

example_text = "I recommend this book [1] but the other book [3] is better. Some people think that this website [10] is the best but I prefer this [7] one."

references = find_references(example_text)

print(references)
[1, 3, 10, 7]

You can then use it in a notebook cell like:

import ex4

example_text = "I recommend this book [1] but the other book [3] is better. Some people think that this website [10] is the best but I prefer this [7] one."

references = ex4.find_references(example_text)

print(references)
[1, 3, 10, 7]
Exercise 5

Take the function that you wrote in the last exercise and move it into a module called refs. You should test it in a notebook cell by importing it and calling it as you did above, and comparing it against the expected output.

import refs

text = "I recommend this book [1] but the other book [3] is better. Some people think that this website [10] is the best but I prefer this [7] one."

numbers = refs.find_references(text)

expected = [1, 3, 10, 7]
if numbers == expected:
    print("Test passed")
else:
    print("Test failed:", numbers, "is not the same as", expected)

You’re refs module, refs.py, could look like this:

refs.py
def find_references(text):
    words = text.split()
    
    refs = []
    for word in words:
        if word[0] == "[" and word[-1] == "]":
            reference = word[1:-1]
            reference_number = int(reference)
            refs.append(reference_number)
    
    return refs

You can then test it in a notebook cell like this:

import refs

text = "I recommend this book [1] but the other book [3] is better. Some people think that this website [10] is the best but I prefer this [7] one."

numbers = refs.find_references(text)

expected = [1, 3, 10, 7]
if numbers == expected:
    print("Test passed")
else:
    print("Test failed:", numbers, "is not the same as", expected)
Test passed