DEV Community

Cover image for Installing and using NumPy in Python
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Installing and using NumPy in Python

First of all, let me explain a bit what NumPy is and why you might need it.
NumPy is a Python library that is used for working with arrays.
It stands short for Numeric Python

This, of course, is still a bit vague. In general, it makes working with arrays (lists) about 50x faster than traditional python lists.

Installing and using NumPy

To install NumPy, we must run a pip install command for it.

pip install numpy
Enter fullscreen mode Exit fullscreen mode

Then we have to import it into our Python file.

import numpy
Enter fullscreen mode Exit fullscreen mode

Now we can convert a list into a numpy array:

arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
Enter fullscreen mode Exit fullscreen mode

However, it's quite often used to have the numpy imported as the np alias.

We can do so like this:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)
Enter fullscreen mode Exit fullscreen mode

This now does the exact same thing, but it's easier to write.

If you ever wonder what version of numpy you have installed, you can simply print that out.

print(np.__version__)
# 1.20.3
Enter fullscreen mode Exit fullscreen mode

Types of arrays

The cool part about the NumPy arrays is that they can be built from all array-like data types of Python.

Which include the list, tuple, dictionary.

tuple = np.array((1, 2, 3, 4, 5))
print(tuple)

list = np.array(["dog", "cat", "penguin"])
print(list)

set = np.array({"dog", "cat", "penguin"})
print(set)
Enter fullscreen mode Exit fullscreen mode

It's super easy to convert this stuff to NumPy arrays since we can eventually do more stuff and faster!

In a follow-up article, I'll go more in-depth about the options for the NumPy arrays.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (2)

Collapse
 
waylonwalker profile image
Waylon Walker

Keep going Chris, I'm really enjoying your python series.

Speaking of series, you should stitch these as a series on dev.

One extra tip, if you have a package that does not implement __version__ you can run pip show <package_name> to see details about it.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Hey Waylon, I'll convert these to a series yeah didn't expect to be going for so long on Python 😀