Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to change the first item of the list to 10.
Python
numbers = [1, 2, 3] numbers[0] = [1] print(numbers)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using quotes around 10 makes it a string, not an integer.
Trying to assign a value to numbers instead of numbers[0].
โ Incorrect
Lists are mutable, so you can change an item by assigning a new value to its index. Here, we assign 10 to the first item.
2fill in blank
mediumComplete the code to add the number 4 at the end of the list.
Python
numbers = [1, 2, 3] numbers.[1](4) print(numbers)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using
insert without specifying the index.Using
extend which expects an iterable, not a single item.โ Incorrect
The append method adds a single item to the end of the list.
3fill in blank
hardFix the error in the code to change the second item to 20.
Python
numbers = [5, 6, 7] numbers[[1]] = 20 print(numbers)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using index 2 which is the third item.
Using index 3 which is out of range.
โ Incorrect
List indexes start at 0, so the second item is at index 1.
4fill in blank
hardFill both blanks to create a new list with squares of numbers greater than 2.
Python
numbers = [1, 2, 3, 4, 5] squares = [x[1]2 for x in numbers if x [2] 2] print(squares)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using
% which is modulo, not power.Using
+ which adds numbers instead of squaring.โ Incorrect
Use ** to square numbers and > to filter numbers greater than 2.
5fill in blank
hardFill all three blanks to create a dictionary with uppercase keys and values greater than 3.
Python
numbers = {'a': 1, 'b': 4, 'c': 5}
result = [1]: [2] for [3], v in numbers.items() if v > 3}
print(result) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using
v.upper() which is invalid because values are integers.Using
k instead of k.upper() for keys.โ Incorrect
Use k.upper() for uppercase keys, v for values, and k as the loop variable for keys.