0
0
Pythonprogramming~10 mins

List indexing and slicing in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the first element of the list.

Python
my_list = [10, 20, 30, 40]
first_element = my_list[[1]]
print(first_element)
Drag options to blanks, or click blank then click option'
A0
B1
C-1
D4
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 1 as the index to get the first element.
Using 4 which is out of range.
2fill in blank
medium

Complete the code to get the last element of the list using negative indexing.

Python
numbers = [5, 10, 15, 20, 25]
last_num = numbers[[1]]
print(last_num)
Drag options to blanks, or click blank then click option'
A-2
B4
C0
D-1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using -2 which gives the second last element.
Using 0 which is the first element.
3fill in blank
hard

Fix the error in the code to slice the list and get elements from index 1 to 3 (inclusive of 1, exclusive of 4).

Python
letters = ['a', 'b', 'c', 'd', 'e']
sublist = letters[[1]]
print(sublist)
Drag options to blanks, or click blank then click option'
A1-4
B1,4
C1:4
D1..4
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using commas or dashes instead of colon in slicing.
Using '..' which is not valid syntax.
4fill in blank
hard

Fill both blanks to slice the list and get every second element from index 0 to 5.

Python
data = [2, 4, 6, 8, 10, 12, 14]
slice_result = data[[1]:[2]:2]
print(slice_result)
Drag options to blanks, or click blank then click option'
A0
B5
C6
D1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 6 as end index which includes index 5 element.
Using 1 as start index which skips the first element.
5fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {: [1] for [2] in words if len(word) > 3}
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
D{
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Not starting with '{' which causes syntax error.
Using 'word' twice incorrectly for key and loop variable.