0% found this document useful (0 votes)
10 views

Numpy - Module - Jupyter Notebook

This document demonstrates how to use the NumPy module in Python to perform mathematical operations on arrays and scalars. It shows functions for addition, subtraction, multiplication, division and other operations. It also introduces the math, time, and datetime modules.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Numpy - Module - Jupyter Notebook

This document demonstrates how to use the NumPy module in Python to perform mathematical operations on arrays and scalars. It shows functions for addition, subtraction, multiplication, division and other operations. It also introduces the math, time, and datetime modules.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

6/5/23, 5:07 AM Numpy_module - Jupyter Notebook

In [1]: import numpy as np

In [2]: arr1 = np.array([2,3,4])


arr2 = np.array([7,8,9])

In [3]: print(arr1)
print(arr2)

[2 3 4]
[7 8 9]

In [4]: # Mathematical operations or vector maths:

In [5]: c = arr1+arr2
print(c)

[ 9 11 13]

In [7]: d = np.add(arr1,arr2)
print(d)

[ 9 11 13]

In [8]: c1 = arr1-arr2
print(c1)

[-5 -5 -5]

In [9]: d1 = np.subtract(arr1,arr2)
print(d1)

[-5 -5 -5]

In [10]: c2 = arr1*arr2
print(c2)

d2 = np.multiply(arr1,arr2)
print(d2)

[14 24 36]
[14 24 36]

localhost:8888/notebooks/Desktop/New folder/Numpy_module.ipynb 1/6


6/5/23, 5:07 AM Numpy_module - Jupyter Notebook

In [11]: c3 = arr1/arr2
print(c3)

d3 = np.divide(arr1,arr2)
print(d3)

[0.28571429 0.375 0.44444444]


[0.28571429 0.375 0.44444444]

In [13]: # Scalar math:

In [14]: ar = np.array([4,5,6,7,8])
print(ar)

[4 5 6 7 8]

In [15]: print(np.add(ar,2))

[ 6 7 8 9 10]

In [16]: print(np.subtract(ar,2))

[2 3 4 5 6]

In [17]: print(np.multiply(ar,2))

[ 8 10 12 14 16]

In [18]: print(np.divide(ar,2))

[2. 2.5 3. 3.5 4. ]

In [19]: print(np.power(ar,2))

[16 25 36 49 64]

In [20]: print(np.power(ar,3))

[ 64 125 216 343 512]

In [22]: print(np.sqrt(ar))

[2. 2.23606798 2.44948974 2.64575131 2.82842712]

In [23]: print(np.log(ar))

[1.38629436 1.60943791 1.79175947 1.94591015 2.07944154]

localhost:8888/notebooks/Desktop/New folder/Numpy_module.ipynb 2/6


6/5/23, 5:07 AM Numpy_module - Jupyter Notebook

In [24]: ab = np.array([-8,-6,5])
print(np.abs(ab))

[8 6 5]

In [25]: # Statistical math:

In [26]: a = np.array([8,6,5,0])
print(a)

[8 6 5 0]

In [27]: print(a.max())

In [28]: print(a.min())

In [29]: print(a.mean())

4.75

In [30]: print(a.sum())

19

In [31]: print(a.std())

2.947456530637899

In [32]: print(a.var())

8.6875

Modules :- This are the collection of codes that can be


called and used in other programs of python.
In [33]: import math

In [34]: # math.sqrt() :- returns the squareroot of the number.



x = math.sqrt(64)
print(x)

8.0

localhost:8888/notebooks/Desktop/New folder/Numpy_module.ipynb 3/6


6/5/23, 5:07 AM Numpy_module - Jupyter Notebook

In [36]: # math.ceil() :- method to round off to its highest near integer.


x = math.ceil(1.2)
print(x)

In [37]: # math.floor() :- method to round off to its lowest near integer.


x = math.floor(1.9)
print(x)

In [39]: # math.pi :- return the PI value.


x = math.pi
print(x)

3.141592653589793

In [40]: # math.sin() :- return the sin value.


x = math.sin(5)
print(x)

-0.9589242746631385

In [41]: # math.pow(x,y) :- returns the value of x to the power of y.


x = math.pow(12,3)
print(x)

1728.0

In [42]: import time

In [44]: for i in range(5):


print('hello')
time.sleep(5)

hello
hello
hello
hello
hello

localhost:8888/notebooks/Desktop/New folder/Numpy_module.ipynb 4/6


6/5/23, 5:07 AM Numpy_module - Jupyter Notebook

In [45]: # electronic watch:


for i in range(10):
time.sleep(1)
print(time.ctime())

Mon Jun 5 04:55:43 2023


Mon Jun 5 04:55:44 2023
Mon Jun 5 04:55:45 2023
Mon Jun 5 04:55:46 2023
Mon Jun 5 04:55:47 2023
Mon Jun 5 04:55:48 2023
Mon Jun 5 04:55:49 2023
Mon Jun 5 04:55:50 2023
Mon Jun 5 04:55:51 2023
Mon Jun 5 04:55:52 2023

In [46]: print(time.ctime())

Mon Jun 5 04:56:48 2023

In [47]: import datetime

In [48]: today = datetime.date.today()


print(today)

2023-06-05

In [49]: print(today.year)

2023

In [50]: print(today.month)

In [51]: print(today.day)

In [57]: now = datetime.datetime.now()


print(now)

2023-06-05 05:01:13.505419

In [58]: print(now.year)

2023

localhost:8888/notebooks/Desktop/New folder/Numpy_module.ipynb 5/6


6/5/23, 5:07 AM Numpy_module - Jupyter Notebook

In [59]: print(now.day)

In [60]: print(now.hour)

In [61]: print(now.minute)

In [62]: print(now.second)

13

In [ ]: ​

localhost:8888/notebooks/Desktop/New folder/Numpy_module.ipynb 6/6

You might also like