Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
NumPy - Array Data Types
What will be the output of this code?
import numpy as np
arr = np.array([0, 1, 2, 3])
print(arr.astype(bool))
A[True True True True]
B[False True True True]
C[False False False False]
D[0 1 2 3]
Step-by-Step Solution
Solution:
  1. Step 1: Understand int to bool conversion

    In NumPy, 0 converts to False, and any non-zero integer converts to True when cast to bool.
  2. Step 2: Convert each element

    0 becomes False, 1, 2, 3 become True, so output is [False True True True].
  3. Final Answer:

    [False True True True] -> Option B
  4. Quick Check:

    astype(bool) converts 0 to False, others True [OK]
Quick Trick: astype(bool) treats 0 as False, others True [OK]
Common Mistakes:
  • Assuming all values become True
  • Expecting numeric output after bool cast
  • Confusing bool conversion rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes