Concept Flow - Single element access
Create numpy array
Specify index positions
Access element at index
Return single element value
Accessing a single element in a numpy array involves specifying its index and retrieving the value stored there.
import numpy as np arr = np.array([10, 20, 30, 40]) element = arr[2] print(element)
| Step | Action | Index Accessed | Value Retrieved | Output |
|---|---|---|---|---|
| 1 | Create numpy array | - | [10, 20, 30, 40] | - |
| 2 | Access element at index 2 | 2 | 30 | 30 |
| 3 | Print element | - | - | 30 |
| 4 | End | - | - | - |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| arr | undefined | [10 20 30 40] | [10 20 30 40] | [10 20 30 40] |
| element | undefined | undefined | 30 | 30 |
Single element access in numpy: - Use arr[index] to get element - Indexing starts at 0 - Returns the value at that position - Out-of-range index causes error - Works for 1D and multi-D arrays
arr?import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) print(arr[2, 1])
import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr[2, 1])
data with shape (4, 3, 2). How do you access the single element in the 4th block, 2nd row, and 1st column?Pass the test and this pattern moves unknown