0
0
NumPydata~10 mins

View vs copy behavior in NumPy - Interactive 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 original NumPy array.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
view_arr = arr[1]
Drag options to blanks, or click blank then click option'
A.view()
B.copy()
C.reshape(2, 2)
D.flatten()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .copy() instead of .view() creates a separate array, not a view.
Using .reshape() changes shape but does not guarantee a view.
2fill in blank
medium

Complete the code to modify the view and see the change in the original array.

NumPy
import numpy as np
arr = np.array([10, 20, 30, 40])
view_arr = arr.view()
view_arr[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 other than 0 and expecting no change in original array.
Using .copy() instead of .view() so changes don't reflect.
3fill in blank
hard

Fix the error in the code to ensure modifying the copy does NOT change the original array.

NumPy
import numpy as np
arr = np.array([5, 6, 7, 8])
copy_arr = arr[1]
copy_arr[0] = 100
result = arr
Drag options to blanks, or click blank then click option'
A.copy()
B.view()
C.reshape(2, 2)
D[:]
Attempts:
3 left
💡 Hint
Common Mistakes
Using .view() instead of .copy() causes changes to reflect in original array.
Using slicing [:] creates a view, not a copy.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths only for words longer than 3 letters.

NumPy
words = ['cat', 'house', 'dog', 'elephant']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword.startswith('e')
Dword == 'dog'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Filtering words incorrectly with wrong condition.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values for words longer than 3 letters.

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