DEV Community

Punitha
Punitha

Posted on

NumPy Array

What is a NumPy Array?

  • A NumPy array is a collection of values stored in a structured way.

Numpy also supports:

  • 1D arrays

  • 2D arrays

  • 3D arrays

  • Higher-dimensioanl arrays

Access Array Elements

  • We can access individual elements using their index.
arr = np.array([10, 20, 30, 40])

print(arr[0])
print(arr[3])
Enter fullscreen mode Exit fullscreen mode

Output:

10
40
Enter fullscreen mode Exit fullscreen mode

Array Slicing

  • Slicing is used to extract a portion of an array.
arr = np.array([10, 20, 30, 40])

print(arr[0:3])
Enter fullscreen mode Exit fullscreen mode

Output:

[10 20 30]
Enter fullscreen mode Exit fullscreen mode

1D Array

  • A 1D array contains values in a single row.

Output:

Attributes:

Output:

2D Array

  • A 2D array contains rows and columns.

Output:

Attributes:

Output:

3D Array

  • NumPy can also create 3D arrays.

Output:

Attributes:

Output:

Top comments (0)