1D indexing
Use a number in square brackets to grab one element by position, starting from zero.
In this page:
Syntax
arr[index]
arr[-1]
1D indexing
Indexing a 1-D array works like a list: arr[0] is the first element. You can also assign to an index to modify the array in place. Going past the end raises IndexError.
Note:
Indexing returns a NumPy scalar such as np.int64, not a plain Python int.
Example: 1D indexing
import numpy as np
a = np.array([10, 20, 30, 40])
print(a[0], a[2])
a[1] = 99
print(a)
# Output:
# 10 30
# [10 99 30 40]
Related Topics
Common Mistakes
- Starting at 1 instead of 0
- Indexing beyond the last element
- Forgetting assignment changes the original array
Chapter Summary
- Indices start at 0
- arr[i] reads or assigns an element
- Out-of-range raises IndexError
- Results are NumPy scalars
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: