# Numpy – Explanation by SagarJaybhay

## Create NumPy arrays :

First To check the version of NumPy in Python use below  command

```import numpy as np
print(np.__version__)```

To create an array from the list using numpy use below code.

```a=[1,2,3,4,5]
a=np.array(a) #create array from the list
print(a)
```

In this, if you want to multiply each element by a constant you can do but if you use python list the result is different it generates that much time items in the list.

`a=a*10;#o/p : [10 20 30 40 50]`

## Arange:

It is a function creates an array with a range of integers. This function at-least take 1 argument which is range and it creates an array that not includes the value is which is passed to it.

```A=np.arange(7)
#o/p= array([0,1,2,3,4,5,6]) which not include 7.
```

Also, this function has other parameters like start, stop step and data type.

Start– is used to where to start

Stop– is used to where to stop

Step– means the addition of an element with that step up to last stop.

Dtype= datatype which is created and the default value is None.

```a=np.array([[1,2,3],[1,2,3]])
a=np.arange(0,1000) #o/p like 0,999 array
a=np.zeros([3,4]) # first is row no and another value is column count
a=np.linspace(0,20,10) # this line means start from 0 and last result is 20 and third value is divide 20 into 10 steps
a=np.zeros(8)# o/p array([0., 0., 0., 0., 0., 0., 0., 0.])

a= np.array([1,2,3])
b=np.array([2,3,3])
c=a+b # o/p array([3, 5, 6])
```

Here nd stands for N-dimensional array. In array, every element should the same type.

C.shape is attributed and not a method and it returns tuples which send a number of item in dimension.

a.ndim # give the dimension

in this, you can multiply array with constant and array with array also you can add, sub, mul no of arrays. Why use numpy over the list because of speed.

`print(np.sin) #o/p <ufunc 'sin'> it means it is universal function`

Numpy is mutable means we can change the data.But if your numpy contains one type data suppose int and you try to change or assign some value at some point with float value your data is simply truncated and numpy get this data as int only.In numpy array, this is not a matrix is an array. When you start from the left-hand side the indexing starts with 0 and when you start from right-hand side indexing start with -1.

```a=np.array([10,11,12,13,14,15])

#print('\n',a)
print(a) #o/p =10
print(a[-1]) #o/p =15
```

## Slicing

It means you extract the portion of the sequence by specifying a lower(start) and upper(end) bound with step value is optional. In python slicing object is constructed with start, stop step parameters which are taken by slicing object or function.

```a=np.arange(10)
print(a) #o/p [0 1 2 3 4 5 6 7 8 9]
s=slice(2,6,2) #start-2 stop-6 step-2
print(a[s]) #o/p [2 4]
print(a[2:6:2])#o/p [2 4]
```
• In last line we get same output without slicing object.
• To get the first 3 elements we can use print(a[:3])
• To get the last 2 elements we can use print(a[-2::])
• To get every 2 elements you can use print(a[::2])

## To Create NumPy array from several ways

### 1)  From Tuple:

```import numpy as np
a=(1,2,3,4)
my_array=np.array(a)
print(my_array)
```

If your tuple contains complex number at least one record then whole array will converted into complex number so code look like this.

```a=(1,2,3,4+2j)
my_array=np.array(a)
print(my_array) #array([1.+0.j, 2.+1.j, 3.+0.j, 4.+0.j])
```

### 2) Internal Methods:

In this you can use arrange method to create the array.

`a=a.arange(10,22)`

## Difference between data structure of python and numpy data?

If you multiply tuple with any value then it then the values will repeating that much times for an example-

```a=(1,2+1j,3,4)
a=a*2
a #o/p -(1, (2+1j), 3, 4, 1, (2+1j), 3, 4)
```

But when you multiply some value with numpy array then the output Is different.

```a=(1,2+1j,3,4)
my_array=np.array(a)
my_array=my_array*2
print(my_array) #o/p -[2.+0.j 4.+2.j 6.+0.j 8.+0.j]
```

## How to get the size of an array in numpy?

```a=np.arange(10,22)
a.size #o/p-12
```

In this by using size function you can get the size of an array.

### Sagar Jaybhay Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole. Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.