Complete the code to select a random element from the array using NumPy.
import numpy as np arr = np.array([10, 20, 30, 40, 50]) random_element = np.random.[1](arr) print(random_element)
The np.random.choice() function picks a random element from the array.
Complete the code to select 3 random elements from the array with replacement.
import numpy as np arr = np.array([5, 10, 15, 20, 25]) sample = np.random.choice(arr, size=[1], replace=True) print(sample)
The size parameter controls how many elements to pick. Here, 3 elements are selected.
Fix the error in the code to select 2 unique random elements from the array.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) sample = np.random.choice(arr, size=[1], replace=False) print(sample)
To select 2 unique elements, replace must be False. The size is 2.
Fill both blanks to create a dictionary with elements as keys and their squares as values for elements greater than 3.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) squares = {x: x[1]2 for x in arr if x [2] 3} print(squares)
The operator ** is used for power, and > filters elements greater than 3.
Fill all three blanks to create a dictionary with uppercase keys and values greater than 10.
data = {'a': 5, 'b': 15, 'c': 20}
result = [1]: [2] for [3], [2] in data.items() if [2] > 10
print(result)item as loop variable instead of k.Keys are converted to uppercase with k.upper(). Values are v. Loop variable is k.