Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q4 of 15
NumPy - Creating Arrays
What is the output of the following code?
import numpy as np
arr = np.arange(2, 9, 3)
print(arr)
A[2 5 8]
B[2 3 4 5 6 7 8]
C[2 6 9]
D[3 6 9]
Step-by-Step Solution
Solution:
  1. Step 1: Understand np.arange(2, 9, 3)

    This creates values starting at 2, adding 3 each time, stopping before 9.
  2. Step 2: Calculate values

    Values are 2, 5 (2+3), 8 (5+3). Next would be 11 which is >=9, so stop.
  3. Final Answer:

    [2 5 8] -> Option A
  4. Quick Check:

    np.arange(2, 9, 3) = [2 5 8] [OK]
Quick Trick: np.arange stops before the stop value [OK]
Common Mistakes:
  • Including stop value
  • Wrong step increments
  • Confusing start and stop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes