Write Python program to demonstrate use of built-in packages (e.g. NumPy, Pandas) and user defined packages
NumPy, which stands for Numerical Python, is a library consisting of
multidimensional array objects and a collection of routines for processing those arrays.
Using NumPy, mathematical and logical operations on arrays can be performed.
Write Python program to demonstrate use of built-in packages (e.g. NumPy, Pandas) and user defined packages |
Steps for Installing numpy in windows OS
1. goto Command prompt
2. run command pip install numpy
3. open IDLE Python Interpreter
4. Check numpy is working or not
>>> import numpy
>>> import numpy as np
>>> a=np.array([10,20,30,40,50])
>>> print(a)
[10 20 30 40 50]
Example:
>>> student=np.dtype([('name','S20'),('age','i1'),('marks','f4')])
>>> a=np.array([('Vijay',43,90),('Prasad',38,80)],dtype=student)
>>> print(a)
[('Vijay', 43, 90.) ('Prasad', 38, 80.)]
Example:
>>> print(a)
[10 20 30 40 50 60]
>>> a.shape=(2,3)
>>> print(a)
[[10 20 30]
[40 50 60]]
>>> a.shape=(3,2)
>>> print(a)
[[10 20]
[30 40]
[50 60]]
Creating and accessing a Python Package
Steps to create package in Python
1. First, we create a directory and give it a package name, preferably related to its
operation.
Directory name=Cars
2. Then we put the classes and the required functions in it.
Filename=Maruti.py
class Maruti:
def __init__(self):
self.models=['800','Alto','WagonR']
def PModel(self):
print("Models of Maruti")
for model in self.models:
print('\t%s ' % model)
Filename=Mahindra.py
class Mahindra:
def __init__(self):
self.models=['Scorpio','Bolero','Xylo']
def PModel(self):
#totalprogrammingcode
print("Models of Mahendra")
for model in self.models:
print('\t%s ' % model)
3. Finally we create an __init__.py file inside the directory, to let Python know that the
directory is a package.
Filename=__init__.py
from Maruti import Maruti
from Mahindra import Mahindra
4. To access package car, create sample.py file and access classes from directory car
Filename=sample.py
from Maruti import Maruti
from Mahindra import Mahindra
ModelMaruti=Maruti()
ModelMaruti.PModel()
ModelMahindra=Mahindra()
ModelMahindra.PModel()
Output:
Models of Maruti
800
Alto
WagonR
Models of Mahendra
Scorpio
Bolero
Xylo
Example
import numpy as np
m = np.array([1,2])
n = np.array([4,5])
print("addition of two matrix")
print(np.add(m,n))
print("substraction of two matrix")
print(np.subtract(m,n))
print("multiplication of two matrix")
print(np.multiply(m,n))
print("division of two matrix")
print(np.divide(m,n))
2. Write a NumPy program to generate six random integers between 10 and 30.
import numpy as np
x = np.random.randint(low=10,high=30,size=6)
print(x)