ex1.py
import datetime
= datetime.datetime.now()
time_now
print(time_now.isoformat())
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 practise 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.
2024-10-09T11:37:31.405758
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:
True
or False
) 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
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
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)
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 tricker, 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.
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]
Take the function that you wrote in the last exercise and move it into a module called refs
. You should then write a test file called test_refs.py
containing:
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 should make sure that the test passes when the test script is run with:
test_refs.py
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