0
0
NumPydata~20 mins

Specifying dtype during creation in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
dtype Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of numpy array with specified dtype
What is the output of this code snippet?
import numpy as np
arr = np.array([1, 2, 3], dtype='float32')
print(arr.dtype)
NumPy
import numpy as np
arr = np.array([1, 2, 3], dtype='float32')
print(arr.dtype)
Aint64
Bint32
Cfloat64
Dfloat32
Attempts:
2 left
💡 Hint
Check the dtype argument passed during array creation.
data_output
intermediate
2:00remaining
Resulting array values with dtype conversion
What is the output array when specifying dtype as int8 for this input?
import numpy as np
arr = np.array([300, -130, 127], dtype='int8')
print(arr)
NumPy
import numpy as np
arr = np.array([300, -130, 127], dtype='int8')
print(arr)
A[-44, 126, 127]
B[44, 126, 127]
C[44, -126, 127]
D[-44, -126, 127]
Attempts:
2 left
💡 Hint
Remember int8 stores values from -128 to 127 and wraps around on overflow.
🔧 Debug
advanced
1:30remaining
Identify error when specifying dtype incorrectly
What error does this code raise?
import numpy as np
arr = np.array([1, 2, 3], dtype='flot64')
NumPy
import numpy as np
arr = np.array([1, 2, 3], dtype='flot64')
ATypeError: data type 'flot64' not understood
BValueError: invalid dtype string
CSyntaxError: invalid syntax
DNo error, creates array with default dtype
Attempts:
2 left
💡 Hint
Check if the dtype string is a valid numpy type.
🚀 Application
advanced
2:00remaining
Choosing dtype for memory optimization
You have a large dataset of integers ranging from 0 to 1000. Which dtype should you specify to minimize memory usage without losing data?
Anp.int8
Bnp.int32
Cnp.uint16
Dnp.float64
Attempts:
2 left
💡 Hint
Consider the range of values and the size limits of each dtype.
🧠 Conceptual
expert
2:30remaining
Effect of dtype on arithmetic operations
Given two numpy arrays:
import numpy as np
arr1 = np.array([1000], dtype='int16')
arr2 = np.array([1000], dtype='int16')
result = arr1 + arr2
print(result, result.dtype)

What is the output and dtype of result?
NumPy
import numpy as np
arr1 = np.array([1000], dtype='int16')
arr2 = np.array([1000], dtype='int16')
result = arr1 + arr2
print(result, result.dtype)
A[2000] int16
B[2000] int32
C[-25536] int16
D[-25536] int32
Attempts:
2 left
💡 Hint
int16 max value is 32767; check if addition overflows.