How to Create an Array in Python with Numpy



Python


In this article, we show how to create an array in Python with numpy.

Numpy is a package for scientific computing in Python.

With numpy, we can do many things with data science such as create arrays.

Arrays are like lists but they are still very different in terms of computational abilities.

With arrays, we can do many mathematical operations, such as addition, subtraction, multiplication, and division.

With lists, these types of computations aren't as possible as with arrays.

So arrays can be seen as more fit with mathematical intensive operations.

So how can arrays be created in Python with numpy?

It's very basic.

We basically just have to use the arange() function.

This is shown in the code below.



So the first thing we have to do is import numpy as np.

We then create a variable named array1 and set it equal to np.arange(0,11)

What this does is it creates an array with a range from 0 to 10.

When we show the contents of array1, we see an array that goes from 0 to 10.

So we've now created an array in Python with numpy.

The amazing thing about arrays is that it lends better to mathematical computations than its next closest cousin, which are lists.

Let's look at the following below.



So just like before, we created an array.

Look at what happens when we do a mathematical operation now on the array, in this case, multiply the array by 2.

Every item in the array gets multiplied by 2.

Perfect mathematical computation.

Then we create a list.

We then multiply the list by 2. Only this time, it does not multiply each item in the list, as you might expect. Instead it duplicates every item of the list.

You may have expected each item to be multiplied by 2, as the array, but this doesn't happen with lists.

This is why arrays are special.

Arrays lend themselves perfectly to mathematical computations and are the choice item to use when you have a list of numbers on which you have to do mathematical computations.

Don't use lists when doing mathematical computations. Use arrays. Use numpy. Ok.

How to Create an Array with a Step

If you want to create an array with a step other than the default of 1, then the np.arange() function can be fed a third parameter. The third parameter is the step value, which is the spacing between numbers.

For example, if we specify a third parameter of 2, then every second value is given.

Examples of this is shown below.



So the first thing we have to do is import numpy as np.

We then create an array called array1, which goes from 0 to 10 with a step, or spacing, of 2.

We then call this variable and get the even numbers from 0 to 10.

We then create another array called array2 and give this a step of 3 of numbers from 0 to 10.

We then get all numbers with a step of 3 from 0 to 10, which is 0,3,6, and 9.

So this is how we can create an array with a default spacing other than 1 in Python.

And this is a quick introduction of how to create an array in Python with numpy.


Related Resources



HTML Comment Box is loading comments...