Articles

Indexing and Slicing in numpy array along with examples

Indexing and Slicing in numpy array along with examples


In this numpy tutorial, we will discuss about:

  • indexing in numpy array,
  • numpy array indexing examples,
  • slicing in numpy array,
  • slicing example in numpy

Before going ahead with indexing and slicing in numpy, lets see what is numpy and array.

 

numpy stands for numeric python which is used to perform mathematical operations on arrays.

 

It is a module in which we have to import from the python

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

Now, we can directly use np to call the numpy module.


Array

An array is an one dimensional data structure used to store single data type data. It will only store all integer data or all string type data.or all float type data.

 

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.


Indexing in numpy Array

By Indexing, we will get the element through its position. This process is known as indexing in numpy array.

 

In numpy Array, indexing starts with 0.

Syntax:

array_name[index_pos]

where,

1. array_name is the input numpy array

2. index_pos is the index

 

Example 1numpy array indexing examples

In this numpy array indexing examples program, we will create an integer numpy array and get the elements through indexing in numpy array.

#import the numpy module
import numpy

#create an integer array with 5 elements 
integers=numpy.array([12,34,43,34,21])

#get the first element
print(integers[0])

#get the third element
print(integers[2])

#get the fifth element
print(integers[4])

Outputnumpy array indexing examples result

12
43
21

From the above code, we are getting first,third and fifth elements.

 

Example 2numpy array indexing examples

In this numpy array indexing examples program, we will create an float numpy array and get the elements through indexing.

#import the numpy module
import numpy

#create an float array with 5 elements 
floats=numpy.array([1.2,3.4,4.3,3.4,2.1])

#get the first element
print(floats[0])

#get the third element
print(floats[2])

#get the fifth element
print(floats[4])

Outputnumpy array indexing examples result

1.2
4.3
2.1

From the above code, we are getting first,third and fifth elements.

 

Now its time to learn about slicing in numpy.


Slicing in numpy Array

Here, we will slice the numpy array using index positions. Slicing in numpy array means iterating the numpy elements through index positions.

 

Syntax:

array_name[index_pos_start:index_pos_end]

where,

1. array_name is the input numpy array

2. index_pos_start is the starting index

3. index_pos_end is the ending index

 

Example 1slicing example in numpy

In this slicing example in numpy program, we will create an integer numpy array and get the elements through slicing.

#import the numpy module
import numpy

#create an integer array with 5 elements 
integers=numpy.array([12,34,43,34,21])

#get the elements from first position to third position
print(integers[0:3])

#get the elements from first position to forth position
print(integers[0:4])

#get the elements from second position to forth position
print(integers[1:4])

Output: slicing example in numpy result

[12 34 43]
[12 34 43 34]
[34 43 34]

 

Example 2slicing example in numpy

In this slicing example in numpy program, we will create an float numpy array and get the elements through slicing.

#import the numpy module
import numpy

#create an float array with 5 elements 
floats=numpy.array([1.2,3.4,4.3,3.4,2.1])

#get the elements from first position to third position
print(floats[0:3])

#get the elements from first position to forth position
print(floats[0:4])

#get the elements from second position to forth position
print(floats[1:4])

Outputslicing example in numpy result

[1.2 3.4 4.3]
[1.2 3.4 4.3 3.4]
[3.4 4.3 3.4]

This concludes our session on indexing and slicing in numpy.


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!