← Back to NumPy Course | Chapter 4: Indexing & Slicing | Lesson 1 of 7

1D indexing

Use a number in square brackets to grab one element by position, starting from zero.

In this page:

  1. 1D indexing
Syntax
python
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

python
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
  1. Starting at 1 instead of 0
  2. Indexing beyond the last element
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.