Exercises
Join Lists
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Join the three lists into one, without changing any of them.
Python
first = [1, 2]
second = [3]
third = [4, 5]
# print the combined list
Either + twice, or unpack all three with *.
first = [1, 2]
second = [3]
third = [4, 5]
print([*first, *second, *third])Exercise 2Passed
This raises TypeError. Print the numbers joined by commas.
Python
numbers = [1, 2, 3]
print(", ".join(numbers))join needs strings. Convert each one first.
numbers = [1, 2, 3]
print(", ".join(str(n) for n in numbers))Exercise 3Passed
Build a 3 by 2 grid of zeros where changing one cell does not change the others.
Python
grid = [[0] * 2] * 3
grid[0][0] = 99
print(grid)Repeating a list repeats the reference. Build each row in a comprehension.
grid = [[0] * 2 for _ in range(3)]
grid[0][0] = 99
print(grid)