Array’s are the foundation for all data science in Python. Arrays can be multidimensional, & all elements in an array need to be of the same type, all integers or all floats.

Advantages of Using an Array

Basics of an Array

In Python, we can create new datatypes, called arrays using the NumPy package. NumPy arrays are optimized for numerical analyses & contain only a single data type.

We first import NumPy & then use the array() function to create an array. The array()function takes a list as an input.

The type of my_array is a numpy.ndarray.

Array Examples

In the below example, we will convert a list to an array using the array() function from NumPy. We will create a list a_list comprising of integers. Then, using the array() function, convert it an array.

Example of Creating an Array

Example of an Array Operation

In the below example, we add two numpy arrays. The result is an element-wise sum of both the arrays.

Example of Array Indexing

We can select a specific index element of an array using indexing notation.

We can also slice a range of elements using the slicing notation specifying a range of indices.


Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store