Explain Indexing and Slicing in 1D Array?

Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples.

Example:

# Python program to demonstrate
# the use of index arrays.
import numpy as np
 
# Create a sequence of integers from 10 to 1 with a step of -2
a = np.arrange(10, 1, -2)
print("\n A sequential array with a negative step: \n",a)
 
# Indexes are specified inside the np.array method.
newarr = a[np.array([3, 1, 2 ])]
print("\n Elements at these indices are:\n",newarr)

Output:

A sequential array with a negative step:
[10  8  6  4  2]

Elements at these indices are:
[4 8 6]

We can slice a NumPy array in a similar way to slicing a list - except you can do it in more than one dimension.