# Python NumPy Tutorial – Getting Started With NumPy

Hello everyone, in this post we will learn a very important module of python that is NumPy. NumPy is extremely popular python module in python community. It is heavily used in Scientific computing. So let’s start Python NumPy Tutorial .

In this post we will learn following concepts of NumPy :

• What is NumPy
• NumPy vs List
• Features of NumPy
• NumPy data types and many more

## Python NumPy Tutorial – Learn NumPy With Examples

### What Exactly Is NumPy ?

• NumPy is a high-performance multidimensional array library in python.
• It is primarily used for Numerical analysis.
• It is core library for scientific computing in python.
• The name is an acronym for “Numeric Python” or “Numerical Python”

### Features Of NumPy

It has following features –

• A powerful N-dimensional array object
• Tools for integrating C/C++ and Fortran code
• Useful linear algebra, Fourier transform, and random number capabilities

### Why We Use NumPy

When we talk about arrays, we have different types of arrays. Example, the first one is single dimensional array and the second one is multidimensional arrays.

Single Dimensional array means, it have only one row and multiple columns. But what if you want to work with multidimensional array, which simply means you have an array which have multiple rows and multiple columns. Multidimensional arrays can be 2-dimensional, 3 dimensional or n-dimensional.

You can use multiple dimensional  array for implementing matrix. Let’s say, if you are working on some scientific calculation, you have matrix there. So it is useful to working with multiple dimensional array.

So now the question arise, can we use multiple dimensional arrays in python. And answer is that obviously, python is one of the best language. But there is a case, if you talk about the array which are available in python for you, in that you can’t work with multi dimensional array unfortunately. Because even if you try for that, for example if you want to create a multi dimensional array  with array module, you can’t do that, you will get error. Let’s see it practically, here i am creating a multiple dimensional array with array module.

• If you run this code you will get following error

Now you can see you got an error here. So how can we work with multi dimensional array because in array module we don’t have that. So the package which are using here, which is array doesn’t support multi dimensional.

That’s why, we can use a third party package and that is your NumPy . Using NumPy package, we can create single dimensional as well as multi-dimensional arrays. By default NumPy package is not available in python, you have to install it manually. We will see installing NumPy package later.

### NumPy Data Types

#### Numerical Types :

• integers (int)
• unsigned integers (uint)
• floating point (float)
• complex

#### Other Data Types

• booleans (bool)
• string
• datetime
• python object

The below image shows a detailed overview of NumPy datatypes.

### How To Create NumPy Array

Now we will see, how to create a NumPy array in python.

Importing NumPy Module

First of all you have to import NumPy module. For this run the following code on terminal.

And now write the following code to create a NumPy array.

• array() function is used to create NumPy array

So lets see the result of NumPy array.

### Advantages Of NumPy Over List

So now a question arise, why we use NumPy instead of a list. So answer is here –

#### Save Coding Time :

• No for loops : many vector and matrix operations saves coding time.

#### Faster Execution :

• Single type for each field to avoid type checking.
• Uses contiguous blocks of memory.

Now i am going to prove practically that actually NumPy array is faster and more convenient than a list. So let me show you how this happen.

#### What We Did ?

• in this example, we have added two lists and two NumPy arrays and shown their execution time.
• To add two lists, you need to run a for loop because if you directly write L1+L2 , it is going to give a result which is nothing but concatenation of both the lists.
• But when we talk about array, what you need to do is, you need to just write A1+A2 that’s it.
• So it is more convenient to work with NumPy array instead of list.

And now see the result –

You can see that there is a huge difference between List and numPy execution. As a result NumPy is much faster than  a List.

#### Uses Less Memory :

• Python List : an array of pointers to python objects, with 4B+ per pointer plus 16B+ for a numerical object.
• NumPy Array : No pointers ; type and itemsize is same for columns.
• Compact datatypes like uint8 and float16.

Now we will see it practically, so let’s start.

So now let’s check the result –

So you can see that there is quite lot of difference between them. And now it is proved that NumPy occupy less memory than list.

### NumPy Array Pointers

• Data Type : All elements have same NumPy data type.
• Item Size : Memory size of each item in bytes
• Shape : Dimensions of the array
• Data : The easiest way to access the data is trough indexing , not this pointer.

### Ways Of Creating Arrays In NumPy

So now we will discuss about various ways of creating arrays in NumPy. So basically we have 6 ways to creating arrays in NumPy, which are following –

• array()
• arange()
• linspace()
• logspace()
• zeros()
• ones()

Now we will learn each ways with examples.

#### array()

We can create NumPy array using array() in the following way.

• If you remember in a normal array package, we need to specify the type as well, but here we don’t need to do that. You can specify the type here but by default it will guess the value you are entering, and based on that it will convert .
• Let’s see the result –

#### arange()

The another way we have is arange().

• In arange() method, you have to mention the 1st element and last element and then you have to also mention the step.
• Here i have mention step as 3, so it will print 1 then skip two numbers then print 4 and so on.
• So let’s see the result  of this way.

#### linspace()

• linspace() will take 3 parameters. That are Start , Stop and Retstep.

So the code for this is following.

• linspace(0,15,16) will take the values from 0-15 and here retstep is 16 so it will break the values into 16 different parts.
• So if you run this code, you got the following output.

Now you might be thinking why dot(.) here. Because all the values here, they are float. Why float – because you are breaking the range into parts wise so this range is divided into 16 parts and normally you divide as the part you might get float value. This is awesome.

If you increase the retstep let’s say 25, then what will happen.

So see the result, this will divide the numbers into 25 parts.

And now we will see, if you don’t specify the retstep then what will happen.

It will divide the numbers into 50 parts which is by default. So lets see the result. #### logspace()

• logspace() is an another way of creating array. So let’s see how it works.
• logspace() is different from linspace().
• In logspace() and linspace(), you create parts. that means you break down the range into number of parts.
• In linspace() the gap between the two elements is fixed but in logspace() the case is different.
• The spacing between two elements would be depends upon the log of it
• So let’s see it’s implementation, write the following code.

• logspace() computes its start and end points as base**start and base**stop respectively. The base value can be specified, but is 10.0 by default.
• so here, it will start from 10 rest to 1 to 10 rest to 50 and it will get divided into 5 parts.
• And now lets see the result.

Till this point, we have seen 4 functions and now we have two more. This two more is actually very efficient. The first one is zeros() and second one is ones(). So let’s discuss about them.

#### zeros()

• So if you want to create an array, let’s say of size 10 and all the values by default should be zero. In that case, you will be using zeros() as a function.
• So let’s see how it works. Write the following code.

• It will create a array, size of 10 with all the elements are zero. see the output.

In output, you are getting 0. which means you are getting float values. But if you want to work with integer then you have to specify data type. So see how to do that.

And the output for this will be – #### ones()

• It is also same as the zeros() but the difference is that ones() will give you 1 that means all the elements will be 1.
• so let’s write the following code.