# How to Access a Slice of a NumPy Array

Posted on

## Introduction

This is also another article which the main content has some sort of relation with several previous articles. Those articles focus on the NumPy library and also NumPy array. The first notable one is the article with the title of ‘How to Use NumPy’ in this link. Another article which is also have quite similar content with this one is the article with the title of ‘How to Access or Index NumPy Array’ in this link. That article is focusing on how to access one single element of a NumPy array. But in this article, it is accessing not only one element but several element from the NumPy Array. In other words, this article will focus on how to access a part or a slice of the NumPy array which containing several elements.

## How to Access a Slice of a NumPy Array

In this part, the main focus will be just access a NumPy array but specifically multi elements in the shape of the a slice from the NumPy array variable. So, basically, this article will demonstrate how to get multiple elements within a range from the NumPy array variable. Below is the steps for the demonstration :

1. The demonstration is in a command line interface since it will use a command execution in the command line. Just execute Command Prompt exist as in this context it uses a local device running using Microsoft Windows operating system. The appearance of the Command Prompt exist below :

```Microsoft Windows [Version 10.0.22000.856]

C:\Users\Personal>
```
2. Since it will be running in a Python command console, just type ‘python’ in the Command Prompt for getting in to the Python command console as follows :

```Microsoft Windows [Version 10.0.22000.856]

C:\Users\Personal>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
>>>
```
3. In order to define NumPy array, it need a NumPy library. Therefore, import the NumPy library first as follows :

```Microsoft Windows [Version 10.0.22000.856]

C:\Users\Personal>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
>>> import numpy as np
>>>
```
4. After importing the NumPy library, just define a NumPy array as an example as it exist below :

>>> numpy_array = np.array([1,2,3,4,5,6,7,8,9,10])

5. Following after, star slicing the NumPy array above. Actually, there is a certain pattern for extracting or slicing the NumPy array variable. Below is the pattern for slicing or extracting several multiple elements from NumPy array variable :
`numpy_array_variable_name[start_index:stop_index:step]`

Using the above pattern, the following is an example for slicing in order to extract several multiple elements. First, slicing or extracting the 5 (five) beginning number :

```>>> print(numpy_array[0:5:1])
[1 2 3 4 5]
>>>
```

Following after, below is an example of the source code execution for slicing another several multiple elements which is the 5 (five) ending number :

```>>> print(numpy_array[5:10:1])
[ 6 7 8 9 10]
>>>
```

Next, also using the above pattern, getting the even number of the NumPy array by executing the following command as an example :

```>>> print(numpy_array[1:10:2])
[ 2 4 6 8 10]
>>>
```

Continue on, still using the above pattern, this time just get the odd number of the NumPy array by executing the following command as an example :

```>>> print(numpy_array[0:10:2])
[1 3 5 7 9]
>>>```