```
import numpy as np
```

```
# from list to array
list = [1,2,3,4]
arr = np.array(list)
arr
```

OUTPUT:

`array([1, 2, 3, 4])`

```
x = ("red","yellow","blue")
colors = np.array(x)
colors
```

OUTPUT:

`array(['red', 'yellow', 'blue'], dtype='<U6')`

```
x = np.array([1,2,3,4])
x
```

OUTPUT:

`array([1, 2, 3, 4])`

`type(x)`

OUTPUT:

`numpy.ndarray`

```
np_heights = np.array([74, 75, 72, 72, 71])
np_heights.dtype
```

OUTPUT:

`dtype('int32')`

```
l_heights= (74, 75, 72, 72, 71)
np_heights = np.array(l_heights,dtype='float')
np_heights
```

OUTPUT:

`array([74., 75., 72., 72., 71.])`

`np_heights.dtype`

OUTPUT:

`dtype('<U2')`

```
l_heights=(74, 75, 72, 72, 71)
np_heights = np.array(l_heights,dtype='str')
np_heights
```

OUTPUT:

`array(['74', '75', '72', '72', '71'], dtype='<U2')`

```
import numpy as np
l_heights=(74.6, 75, "72", 72, 71)
np_heights = np.array(l_heights)
np_heights
```

OUTPUT:

`array(['74.6', '75', '72', '72', '71'], dtype='<U32')`

```
np_heights = np.array([74, 75, 72, 72, 71])
np_heights + 20
```

OUTPUT:

`array([94, 95, 92, 92, 91])`

The following ways are commonly used when you know the size of the array beforehand:

```
np.ones(): Create array of 1s
np.zeros(): Create array of 0s
```

```
[1,2,3,4,5,6,7,8]
[1,2,3
4,5,6
7,8,9]
arr = np.ones(10)
arr
```

OUTPUT:

`array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])`

`arr.shape ## 1 D array with 9 elements`

OUTPUT:

`(10,)`

```
arr = np.ones(15,dtype='int')
arr
```

OUTPUT:

`array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])`

`arr.dtype`

OUTPUT:

`dtype('int32')`

```
arr1 = np.ones((5,3))
arr1
```

OUTPUT:

```
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
```

`arr1.shape`

OUTPUT:

`(5,3)`

`np.zeros((5,6),dtype='float')`

OUTPUT:

```
array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0.,0., 0., 0.],
[0., 0., 0.,0., 0., 0.],
[0., 0., 0.,0., 0., 0.],
[0., 0., 0.,0., 0., 0.]])
```

`arr1 = np.zeros(5,dtype='int')`

OUTPUT:

`array([0, 0, 0, 0, 0])`

```
arr=np.arange(2,20,3)
arr
```

OUTPUT:

`array([ 2, 5, 8, 11, 14, 17])`

`np.arange(5.0,dtype='int')`

OUTPUT:

`array([0, 1, 2, 3, 4])`

`np.arange(15,1,-2)`

OUTPUT:

`array([15, 13, 11, 9, 7, 5, 3])`

```
arr=np.linspace(4,100,20)
arr
```

OUTPUT:

```
array([ 4. , 9.05263158, 14.10526316, 19.15789474,
24.21052632, 29.26315789, 34.31578947, 39.36842105,
44.42105263, 49.47368421, 54.52631579, 59.57894737,
64.63157895, 69.68421053, 74.73684211, 79.78947368,
84.84210526, 89.89473684, 94.94736842, 100. ])
```

Let's define two lists and perform '+' operation on that.

```
list_1 = [1,2,3]
list_2 = [4,5,6]
list_1 + list_2
```

OUTPUT:

`[1, 2, 3, 4, 5, 6]`

```
np1 = np.array([1,2,3])
np2 = np.array([4,5,6])
np1 + np2
```

OUTPUT:

`array([5, 7, 9])`

Lesson Assignment

Challenge yourself with our lab assignment and put your skills to test.

```
# Python Program to find the area of triangle
a = 5
b = 6
c = 7
# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
```

Sign up to get access to our code lab and run this code.

Sign up Keeping up with current marketing trends

Unsubscribe any time, no hard feelings.

Trusted By Experts

"The team was thrilled with the quality of instruction provided."

~**Avinash Purohit, **DGM, Canara Bank

Enjoy AI-Powered Learning