0
0
NumPydata~10 mins

Integer random with integers() 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 generate a random integer between 1 and 10 using numpy.

NumPy
import numpy as np
random_number = np.random.[1](1, 11)
print(random_number)
Drag options to blanks, or click blank then click option'
Aintegers
Brandint
Crandom
Dchoice
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint instead of integers, which is older numpy syntax.
Forgetting that the upper bound is exclusive.
2fill in blank
medium

Complete the code to generate 5 random integers between 0 and 20 using numpy.

NumPy
import numpy as np
random_numbers = np.random.[1](0, 20, size=5)
print(random_numbers)
Drag options to blanks, or click blank then click option'
Arandint
Bchoice
Crandom
Dintegers
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint without size parameter which may cause errors.
Confusing the upper bound as inclusive.
3fill in blank
hard

Fix the error in the code to generate a random integer between 5 and 15 using numpy integers().

NumPy
import numpy as np
random_num = np.random.integers([1], 15)
print(random_num)
Drag options to blanks, or click blank then click option'
A5, 15
B5
C5, 16
D5, 14
Attempts:
3 left
💡 Hint
Common Mistakes
Putting multiple values in one blank.
Confusing inclusive and exclusive bounds.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.

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 the word itself as the value instead of its length.
Using the wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 3 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1]: [2] for word in words if len(word) [3] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as key instead of uppercase.
Using wrong comparison operator.
Mixing up keys and values.