Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Indexing and Slicing
What is the output of the following code?
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])
A[20 30 40]
B[10 20 30 40]
C[20 30 40 50]
D[30 40 50]
Step-by-Step Solution
Solution:
  1. Step 1: Understand slicing syntax

    The slice arr[1:4] selects elements from index 1 up to but not including 4.
  2. Step 2: Identify elements at these indices

    Indices 1, 2, 3 correspond to values 20, 30, 40.
  3. Final Answer:

    [20 30 40] -> Option A
  4. Quick Check:

    Slice 1:4 excludes index 4 [OK]
Quick Trick: Slice excludes end index, so 1:4 means 1,2,3 [OK]
Common Mistakes:
  • Including the end index element
  • Confusing zero-based indexing
  • Printing the whole array by mistake

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes