Skip to main content
HomeAbout PythonLearn Python

Python Arrays

Python arrays with code examples. Learn how to create and print arrays using Python NumPy today!
Jul 2020  · 3 min read

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

Advantages of using an Array

  • Arrays can handle very large datasets efficiently
  • Computationally-memory efficient
  • Faster calculations and analysis than lists
  • Diverse functionality (many functions in Python packages). With several Python packages that make trend modeling, statistics, and visualization easier.

Basics of an Array

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

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

import numpy
my_array = numpy.array([0, 1, 2, 3, 4])
[0, 1, 2, 3, 4]

The type of my_array is a numpy.ndarray.

<class 'numpy.ndarray'>

Array Examples

Example of creating an Array

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

import numpy as np

a_list = [1, 2, 3, 4]
[1, 2, 3, 4]
an_array = np.array(a_list)
array([1, 2, 3, 4])

Example of an Array operation

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

import numpy as np

array_A = np.array([1, 2, 3])
array_B = np.array([4, 5, 6])

print(array_A + array_B)
[5 7 9]

Example of Array indexing

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

import numpy as np

months_array = np.array(['Jan', 'Feb', 'March', 'Apr', 'May'])

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

['March', 'Apr', 'May']

Interactive Example of a List to an Array

In the below example, you will import numpy using the alias np. Create prices_array and earnings_array arrays from the lists prices and earnings, respectively. Finally, print both the arrays.

# IMPORT numpy as np
import numpy as np

# Lists
prices = [170.12, 93.29, 55.28, 145.30, 171.81, 59.50, 100.50]
earnings = [9.2, 5.31, 2.41, 5.91, 15.42, 2.51, 6.79]

# NumPy arrays
prices_array = np.array(prices)
earnings_array = np.array(earnings)

# Print the arrays

When you run the above code, it produces the following result:

[170.12  93.29  55.28 145.3  171.81  59.5  100.5 ]
[ 9.2   5.31  2.41  5.91 15.42  2.51  6.79]

Try it for yourself.

To learn more about NumPy arrays in Python, please see this video from our course Introduction to Python for Finance.

This content is taken from DataCamp’s Introduction to Python for Finance course by Adina Howe.

Check out our Python Numpy Array Tutorial.


Learn more about Python


Introduction to NumPy

4 hr
Master your skills in NumPy by learning how to create, sort, filter, and update arrays using NYC’s tree census.
See DetailsRight Arrow
Start Course
See MoreRight Arrow

A Deep Dive into the Phi-2 Model

Understanding the Phi-2 model and learning how to access and fine-tune it using the role-play dataset.
Abid Ali Awan's photo

Abid Ali Awan

12 min

Python List Size: 8 Different Methods for Finding the Length of a List in Python

Compare between 8 different methods for finding the length of a list in Python.
Adel Nehme's photo

Adel Nehme

8 min

An End-to-End ML Model Monitoring Workflow with NannyML in Python

Learn an end-to-end workflow to monitor any model in your Jupyter notebook in production environments.
Bex Tuychiev's photo

Bex Tuychiev

15 min

How to Delete a File in Python

File management is a crucial aspect of code handling. Part of this skill set is knowing how to delete a file. In this tutorial, we cover multiple ways to delete a file in Python, along with best practices in doing so.
Amberle McKee's photo

Amberle McKee

5 min

Finding the Size of a DataFrame in Python

There are several ways to find the size of a DataFrame in Python to fit different coding needs. Check out this tutorial for a quick primer on finding the size of a DataFrame. This tutorial presents several ways to check DataFrame size, so you’re sure to find a way that fits your needs.
Amberle McKee's photo

Amberle McKee

5 min

Exploring the Python 'Not Equal' Operator

Comparing values in Python to check if they are not equal is simple with the not equal operator. Check out this quick tutorial on how to use the not equal Python operator, as well as alternatives for comparing floats.
Amberle McKee's photo

Amberle McKee

5 min

See MoreSee More