0
0
Data Analysis Pythondata~10 mins

Vectorized operations vs loops in Data Analysis Python - 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 NumPy array from a Python list.

Data Analysis Python
import numpy as np
numbers = [1, 2, 3, 4, 5]
arr = np.[1](numbers)
Drag options to blanks, or click blank then click option'
Amatrix
Blist
Carray
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' instead of 'array' which is not a NumPy function.
Using 'matrix' which is a different NumPy type.
Using 'range' which creates a range object, not an array.
2fill in blank
medium

Complete the code to add 5 to each element in the NumPy array using vectorized operation.

Data Analysis Python
import numpy as np
arr = np.array([1, 2, 3])
result = arr [1] 5
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' which multiplies instead of adds.
Using '-' which subtracts instead of adds.
Using '/' which divides instead of adds.
3fill in blank
hard

Fix the error in the loop that adds 5 to each element in the list.

Data Analysis Python
numbers = [1, 2, 3]
for i in range(len(numbers)):
    numbers[i] = numbers[i] [1] 5
Drag options to blanks, or click blank then click option'
A*
B/
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' which multiplies instead of adds.
Using '-' which subtracts instead of adds.
Using '/' which divides instead of adds.
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 letters.

Data Analysis Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1] : len([2]) for [1] in words if len([1]) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Bwords
Clen
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' instead of 'word' which is the loop variable.
Using 'len' or 'length' as variable names which are functions or undefined.
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 3 letters.

Data Analysis Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1] : [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Cword
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' as loop variable which is the list, not each item.
Not converting the key to uppercase.
Using incorrect expressions for length or key.