Monday, 13 March 2023

NumPy Array Slicing - Accessing array elements using index python

 NumPy arrays can be sliced using the colon (:) operator, which allows you to extract a portion of the array. The slicing syntax is similar to that of Python lists, but with additional features for multidimensional arrays. Here are some examples of slicing NumPy arrays:

  1. We pass slice instead of index like this: [start:end].
  2. We can also define the step, like this: [start:end:step].
  3. If we don't pass start its considered 0
  4. If we don't pass end its considered length of array in that dimension
  5. If we don't pass step its considered 1
1. Slicing a 1-D array:

You can slice a 1-D array using the colon (:) operator. For example:

2. Slicing a 2-D array:

You can slice a 2-D array using the colon (:) operator for each dimension. For example:

3. Slicing with strides:

You can use the colon (:) operator with a third parameter, called stride, to skip elements in the array. For example:

4. Assigning a sliced array:

You can assign a sliced array to a new value. For example:


Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics


C Programming


Java Tutorial


Data Structures


MS Office


Database Management