Type promotion is when numpy automatically converts data types during operations to a common type that can hold all values without loss.
Click to reveal answer
beginner
What happens when you add an integer array and a float array in numpy?
Numpy promotes the integer array to float type before adding, so the result is a float array.
Click to reveal answer
beginner
Why does numpy promote types during operations?
To avoid losing information by ensuring the result can represent all input values correctly.
Click to reveal answer
intermediate
What is the result type when you multiply a float32 array with an int64 array in numpy?
The result is promoted to float64 because numpy chooses a common type that can hold both values safely.
Click to reveal answer
intermediate
How does numpy handle type promotion with boolean and integer arrays?
Boolean values are promoted to integers (0 or 1) before the operation, so the result is an integer array.
Click to reveal answer
What type does numpy promote when adding int32 and float64 arrays?
Afloat64
Bint32
Cfloat32
Dint64
✗ Incorrect
Numpy promotes to the more general type, float64, to avoid losing decimal information.
When multiplying a boolean array with an int array, what is the result type?
Aboolean
Binteger
Cfloat
Dstring
✗ Incorrect
Booleans are promoted to integers (0 or 1) before multiplication, so the result is integer.
If you add a float32 array and a float64 array, what will be the output type?
Afloat32
Bobject
Cint64
Dfloat64
✗ Incorrect
Numpy promotes to the higher precision float64 to keep accuracy.
Why does numpy promote types during operations?
ATo speed up calculations
BTo save memory
CTo avoid losing data
DTo convert all to strings
✗ Incorrect
Type promotion ensures no data is lost during operations.
What happens when you add an int8 array and a uint16 array?
AResult is int32
BResult is uint16
CResult is float64
DResult is int8
✗ Incorrect
Numpy promotes to int32 to safely hold values from both types.
Explain how numpy decides the output data type when performing operations on arrays with different types.
Think about how numpy chooses a type that can hold all input values safely.
You got /4 concepts.
Describe what happens when you perform arithmetic operations between boolean and numeric numpy arrays.
Remember how True and False behave as numbers in numpy.
You got /4 concepts.
Practice
(1/5)
1. What happens in NumPy when you add an integer array to a float array?
easy
A. The result array will have integer type, truncating floats.
B. The result array will have float type due to type promotion.
C. NumPy throws a type error and stops execution.
D. The result array will have string type.
Solution
Step 1: Understand type promotion in mixed operations
When adding int and float arrays, NumPy promotes the int to float to avoid losing decimal information.
Step 2: Check the resulting data type
The output array will have float dtype because float has higher precision than int.
Final Answer:
The result array will have float type due to type promotion. -> Option B
Quick Check:
int + float = float [OK]
Hint: Mixed int and float always promote to float [OK]
Common Mistakes:
Assuming result stays integer and truncates decimals
Thinking NumPy throws an error on mixed types
Believing result becomes string type
2. Which of the following NumPy operations correctly promotes types when multiplying an int array by a float scalar?
easy
A. np.array([1, 2, 3]) * 2.0
B. np.array([1, 2, 3]) * '2'
C. np.array([1, 2, 3]) * 2
D. np.array([1, 2, 3]) * np.array(['2', '3', '4'])
Solution
Step 1: Identify valid type promotion in multiplication
Multiplying an int array by a float scalar promotes the result to float.
Step 2: Check each option for correct type promotion
np.array([1, 2, 3]) * 2.0 multiplies by 2.0 (float), so result is float array. np.array([1, 2, 3]) * 2 multiplies by int 2, no promotion. The options involving strings cause errors.
Final Answer:
np.array([1, 2, 3]) * 2.0 -> Option A
Quick Check:
int array * float scalar = float array [OK]
Hint: Multiply int array by float scalar for float result [OK]
Common Mistakes:
Using string instead of numeric scalar
Expecting int result when multiplying by float
Confusing string multiplication with numeric
3. What is the dtype of the result when you run this code?
You want to add them and keep the result as float32 to save memory. Which code correctly achieves this with type promotion?
hard
A. result = arr_int.astype(np.float32) + arr_float
B. result = arr_int + arr_float.astype(np.float64)
C. result = arr_int + arr_float
D. result = arr_int.astype(np.int32) + arr_float
Solution
Step 1: Understand default type promotion
Adding int16 and float32 promotes to float64 by default, increasing memory.
Step 2: Convert int16 to float32 before addition
By converting arr_int to float32, adding float32 arrays keeps result as float32, saving memory.
Step 3: Check other options
result = arr_int + arr_float.astype(np.float64) promotes to float64, increasing memory. result = arr_int + arr_float promotes to float64 by default. result = arr_int.astype(np.int32) + arr_float promotes int16 to int32, then adds float32, promoting to float64.
Final Answer:
result = arr_int.astype(np.float32) + arr_float -> Option A
Quick Check:
Cast int to float32 first to keep float32 result [OK]
Hint: Cast int to float32 before adding float32 to keep dtype [OK]
Common Mistakes:
Ignoring default promotion to float64
Casting float to float64 increasing memory
Not casting int before addition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.