0
0
NumPydata~10 mins

Indexing returns views not copies in NumPy - Interactive Code Practice

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

Complete the code to create a view of the first three elements of the array.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
view = arr[1]
Drag options to blanks, or click blank then click option'
A[0,3]
B(0:3)
C[0:3]
D[0-3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for slicing.
Using commas inside the brackets which is for indexing multiple dimensions.
Using subtraction inside the brackets which is invalid syntax.
2fill in blank
medium

Complete the code to modify the view and show it affects the original array.

NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
view = arr[1:4]
view[1] = 99
result = arr
Drag options to blanks, or click blank then click option'
A[1]
B[0]
C[2]
D[3]
Attempts:
3 left
💡 Hint
Common Mistakes
Modifying an index outside the view's range causing an error.
Assuming the view is a copy and changes won't affect the original array.
3fill in blank
hard

Fix the error in the code to create a view of the last two elements.

NumPy
import numpy as np
arr = np.array([5, 10, 15, 20, 25])
view = arr[1]
Drag options to blanks, or click blank then click option'
A[-2:]
B[-2]
C[-2::-1]
D[-2;]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single index instead of a slice.
Using invalid slice syntax like semicolons.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

NumPy
words = ['data', 'is', 'fun', 'science']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Clen(word) < 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the wrong condition to filter words.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.

NumPy
words = ['apple', 'is', 'red', 'big']
result = [1]: [2] for w in words if [3]
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 2
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Not filtering words by length correctly.