import datetime
= datetime.datetime.now()
time_now
print(time_now.isoformat())
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.
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.
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.
Run the code cell above to see the output.
2025-09-15T09:28:43.952895
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.
you should be able to use it like:
giving the output:
This
Write a function called count_word_match
which accepts three agruments:
- a string which contains multiple words separated by spaces,
- a string which gives the word that you want to count the occurrences of and
- a boolean (
True
orFalse
) which specifies whether the match should be case-sensitive.
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:
You can then use it in a notebook cell like:
1
2
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:
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:
[1, 3, 10, 7]
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:
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