# 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])
print(my_array)
``````
``````[0, 1, 2, 3, 4]
``````

The type of `my_array` is a `numpy.ndarray`.

``````print(type(my_array))
``````
``````<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]
a_list
``````
``````[1, 2, 3, 4]
``````
``````an_array = np.array(a_list)
an_array
``````
``````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'])
print(months_array)
``````
``````Apr
``````

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

``````print(months_array[2:5])
``````
``````['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
print(prices_array)
print(earnings_array)
``````

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]
``````

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

Check out our Python Numpy Array Tutorial.

### .css-1531qan{-webkit-text-decoration:none;text-decoration:none;color:inherit;}Introduction to NumPy

BeginnerSkill Level
4 hr
27.3K
Master your skills in NumPy by learning how to create, sort, filter, and update arrays using NYC’s tree census.
See Details
Start Course

### Python for MATLAB Users

BeginnerSkill Level
4 hr
6.4K
Transition from MATLAB by learning some fundamental Python concepts, and diving into the NumPy and Matplotlib packages.

### Introduction to Python for Finance

BeginnerSkill Level
4 hr
60.8K
Learn to use Python for financial analysis using basic skills, including lists, data visualization, and arrays.
See More
Related  ### Free Access Week | Aug 28 – Sept 3

Access DataCamp's entire platform for free, including all 440+ courses, for an entire week. No catch, no credit card required—just unlimited learning for anyone with internet access.

Will Rix

5 min

### How to Choose The Right Data Science Bootcamp in 2023 (With Examples)

Learn everything about data science bootcamps, including a list of top programs to kickstart your career.  ### DataCamp Portfolio Challenge: Win \$500 Publishing Your Best Work

Win up to \$500 by building a free data portfolio with DataCamp Portfolio. DataCamp Team

5 min

### 10 Essential Python Skills All Data Scientists Should Master

All data scientists need expertise in Python, but which skills are the most important for them to master? Find out the ten most vital Python skills in the latest rundown.

Thaylise Nakamoto

9 min

### A Complete Guide to Socket Programming in Python

Learn the fundamentals of socket programming in Python Serhii Orlivskyi

41 min

### Chroma DB Tutorial: A Step-By-Step Guide

With Chroma DB, you can easily manage text documents, convert text to embeddings, and do similarity searches.
See MoreSee More