Task 1
13.1: basic
def blank():
print("")
numbers = ["One", "Two", "Three", "Four", "Five", "Six"]
print("Created list")
blank()
for e in numbers:
print("Checking list in order:", e)
blank()
print("The 1st element is:", numbers[0])
print("The 4th element is:", numbers[3])
print("The 5th element is:", numbers[4])
blank()
for i in range(len(numbers)):
print(numbers[i])
Output
>>> Created list
>>> Checking list in order: One
>>> Checking list in order: Two
>>> Checking list in order: Three
>>> Checking list in order: Four
>>> Checking list in order: Five
>>> Checking list in order: Six
>>> The 1st element is: One
>>> The 4th element is: Four
>>> The 5th element is: Five
>>> One
>>> Two
>>> Three
>>> Four
>>> Five
>>> Six
Task 2
This was the task on editing lists
You may notice that I did not use the del
keyword
This is because personally I dislike using this to edit lists in this way, as it can be hard to read and have unintended side effects.
del
in python deletes objects, and everything in python is an object (including lists), so it can really delete anything.
.pop
however can only be used on lists, and it does NOT delete the object, but rather removes it from the list, until garbage collection comes and cleans it up.
This may be less efficient, but Garbage Collection is coming anyway, so we may as well let it do its job, and additionally del
can be confusing as it is not called on the object, but is rather a keyword in front of it.
13.2: editing
def print_list():
stdout.append(str(numbers.copy()))
numbers = ["One", "Two", "Three"]
stdout: list[str] = []
print_list()
numbers.append("Four")
print_list()
numbers.insert(2, "Five")
print_list()
numbers.remove("One")
print_list()
numbers.pop(1)
print_list()
numbers.sort()
print_list()
print("\n".join(stdout))
Output
>>> ['One', 'Two', 'Three']
>>> ['One', 'Two', 'Three', 'Four']
>>> ['One', 'Two', 'Five', 'Three', 'Four']
>>> ['Two', 'Five', 'Three', 'Four']
>>> ['Two', 'Three', 'Four']
>>> ['Four', 'Three', 'Two']
Task 3
13.3: numbers.py
number = 16
print("Enter a number: " + str(number))
numbers: list[str] = []
for i in range(1, number):
numbers.append(f"{i} x 13 = {i * 13}")
for n in numbers:
print(n)
Output
>>> Enter a number: 16
>>> 1 x 13 = 13
>>> 2 x 13 = 26
>>> 3 x 13 = 39
>>> 4 x 13 = 52
>>> 5 x 13 = 65
>>> 6 x 13 = 78
>>> 7 x 13 = 91
>>> 8 x 13 = 104
>>> 9 x 13 = 117
>>> 10 x 13 = 130
>>> 11 x 13 = 143
>>> 12 x 13 = 156
>>> 13 x 13 = 169
>>> 14 x 13 = 182
>>> 15 x 13 = 195
Extension:
Calling remove
when element exists multiple times, only removes the first instance of the element
Eg.
>>> array = ["one", "two", "one"]
>>> array.remove("one")
>>> array
["two", "one"]
Task 4
13.4: random
from random import randint
def random_question() -> str:
index = randint(0, len(questions) - 1)
return questions.pop(index)
questions = [
"Why is the sky blue?",
"Why is the sun yellow?",
"Why is the moon white?",
"Why is the grass green?",
"Why is the dirt brown?",
]
for i in range(0, 5):
print(random_question())
Output
>>> Why is the grass green?
>>> Why is the moon white?
>>> Why is the sky blue?
>>> Why is the sun yellow?
>>> Why is the dirt brown?