0
0
NumPydata~10 mins

Element-wise arithmetic 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 add two numpy arrays element-wise.

NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = arr1 [1] arr2
print(result)
Drag options to blanks, or click blank then click option'
A/
B-
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes subtraction.
Using '*' multiplies elements instead of adding.
Using '/' divides elements instead of adding.
2fill in blank
medium

Complete the code to multiply two numpy arrays element-wise.

NumPy
import numpy as np

arr1 = np.array([2, 4, 6])
arr2 = np.array([3, 5, 7])
result = arr1 [1] arr2
print(result)
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds elements instead of multiplying.
Using '-' subtracts elements instead of multiplying.
Using '/' divides elements instead of multiplying.
3fill in blank
hard

Fix the error in the code to subtract two numpy arrays element-wise.

NumPy
import numpy as np

arr1 = np.array([10, 20, 30])
arr2 = np.array([1, 2, 3])
result = arr1 [1] arr2
print(result)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds elements instead of subtracting.
Using '*' multiplies elements instead of subtracting.
Using '/' divides elements instead of subtracting.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths if length is greater than 3.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'len(word)' for the dictionary value.
Using '<' instead of '>' in the condition.
Not using len() to get word length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths if length is greater than 4.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = [1] [2] for word in words if len(word) [3] 4
print(result)
Drag options to blanks, or click blank then click option'
A{word.upper():
Blen(word)
C>
D[word.upper() for word in words]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list comprehension instead of a dictionary comprehension.
Not converting words to uppercase for keys.
Using '<' instead of '>' in the condition.