0
0
Data Analysis Pythondata~10 mins

Array indexing and slicing in Data Analysis 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 array.

Data Analysis Python
import numpy as np
arr = np.array([10, 20, 30, 40])
first_element = arr[[1]]
print(first_element)
Drag options to blanks, or click blank then click option'
A1
B0
C-1
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 to access the first element.
Using an index outside the array range.
2fill in blank
medium

Complete the code to slice the array and get elements from index 1 to 4 (exclusive).

Data Analysis Python
import numpy as np
arr = np.array([5, 10, 15, 20, 25])
slice_arr = arr[[1]]
print(slice_arr)
Drag options to blanks, or click blank then click option'
A2:5
B1:3
C0:3
D1:4
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1:3 which excludes index 3.
Using 2:5 which starts too late.
3fill in blank
hard

Fix the error in the code to correctly get the last two elements of the array.

Data Analysis Python
import numpy as np
arr = np.array([2, 4, 6, 8, 10])
last_two = arr[[1]]
print(last_two)
Drag options to blanks, or click blank then click option'
A-2:
B3:5
C4:6
D-3:-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3:5 which works but is less flexible.
Using -3:-1 which excludes the last element.
4fill 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 characters.

Data Analysis Python
words = ['cat', 'house', 'tree', 'a', 'elephant']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for length.
Using word > 3 which compares strings incorrectly.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, but only for words longer than 4 characters.

Data Analysis Python
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
result = { [1]: [2] for w in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 4
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of w.upper() for keys.
Filtering with wrong length condition.