📖 Scenario: Imagine you are working with two sets of numbers in a scientific experiment. One set contains whole numbers (integers), and the other set contains decimal numbers (floats). You want to combine these numbers and see how Python handles their types when doing math.
🎯 Goal: You will create two NumPy arrays with different data types, add them together, and observe how NumPy changes the data type of the result automatically. This is called type promotion.
📋 What You'll Learn
Create two NumPy arrays with specified data types
Add the two arrays together
Check and print the data type of the result
💡 Why This Matters
🌍 Real World
In real scientific and engineering work, data often comes in different types. Understanding how these types combine helps avoid mistakes and ensures accurate calculations.
💼 Career
Data scientists and engineers frequently work with mixed data types. Knowing type promotion helps in writing efficient and correct code for data analysis and modeling.
Progress0 / 4 steps
1
Create two NumPy arrays with different types
Import NumPy as np. Create a NumPy array called array_int with values [1, 2, 3] and data type int32. Create another NumPy array called array_float with values [1.5, 2.5, 3.5] and data type float64.
NumPy
Hint
Use np.array() with the dtype parameter to set the data type.
2
Add the two arrays
Create a new variable called result that stores the sum of array_int and array_float.
NumPy
Hint
Use the + operator to add the two arrays element-wise.
3
Check the data type of the result
Create a variable called result_dtype that stores the data type of the result array using the .dtype attribute.
NumPy
Hint
Use result.dtype to get the data type of the array.
4
Print the result and its data type
Print the result array and then print the result_dtype variable to see the values and the data type after addition.
NumPy
Hint
Use two print() statements: one for result and one for result_dtype.
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.