My Practical File
My Practical File
1
This Practical File Is Submitted For
Fulfillment Of
COMPUTER
SCIENCE(PYTHON)
2
SUBMITTED BY
(Signature)
3
CERTIFICATE
Teacher Name:
Teacher Signature:
Date:
4
INTRODUCTION
programming
5
Programs List
1) Write a NumPy program to create 3x3 matrix with values ranging from 2 to 10.
3) Write a Pandas program to convert a Pandas module series to Python list and it’s
type.
4) Write a Pandas program to compare the elements of the two pandas Series.
6) Write a Pandas program to Add, Subtract, and Multiple and divide two pandas
series.
9) Write a NumPy program to create a 8x8 matrix and fill it with a checker board
pattern.
11) Write a NumPy program to test whether each element of 1-D array is also present
in a second array.
6
12) Write a NumPy program to find the number of elements of an array, length of one
13) Write pandas program to select the rows where the height is not known , i.e. is
NaN.
14) Write a pandas program to select the name of persons whose height is between 5
15) Write pandas program to read marks detail of Ram Kohli and calculate sum of all
marks.
16) Write pandas program to sort the data frame first by ‘designation’ in ascending
7
Year:2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018
Production:4,6,7,15,24,2,19,5,16,4.
18) Write a program to create data frame for 3 students including name and roll
numbers. And new columns for 5 subjects and 1 column to calculate percentage.
Week 1 2 3 4 5
number
8
Program:1
Write a NumPy program to create 3x3
matrix with values ranging from 2 to 10.
Answer:
import numpy as np
NUM=np.arange(2,11).reshape(3,3)
print(NUM)
OUTPUT :
9
Program:2
Write a NumPy program to generate six
random integers between 25 to 55.
Answer:
import numpy as np
NUM=np.random.randint(low=25, high=55,
size=6)
print(NUM)
OUTPUT :
10
Program:3
Write a Pandas program to convert a
Pandas module series to Python list and it’s
type.
Answer:
import pandas as pd
Rollno=pd.Series([2,4,6,8,10])
print('pandas Series and type')
print(Rollno)
print(type(Rollno))
print("Convert pandas Series to python
list")
print(Rollno.tolist())
print(type(Rollno.tolist()))
OUTPUT :
11
12
Program:4
Answer:
import pandas as pd
ds1=pd.Series([2,4,6,8,10])
ds2=pd.Series([1,3,5,7,10])
print("Series1:")
print(ds1)
print("Series2:")
print(ds2)
13
print('compare the elements of the said
Series')
print("Equals:")
print(ds1==ds2)
print("Greater than")
print(ds1>ds2)
print("Less than")
print(ds1<ds2)
OUTPUT :
14
Program:5
Write a Python program to convert a
dictionary to a pandas series.
Answer:
import pandas as pd
15
My_Dict={'a':100,'b':200,'c':300,'d':400,'e':80
0}
print("My Dictionary Content are:")
print(My_Dict)
My_Con_Dict=pd.Series(My_Dict)
print("My Converted Series Content are:")
print(My_Con_Dict)
OUTPUT :
Program:6
Write a Pandas program to Add, Subtract,
and Multiple and divide two pandas series.
16
Answer:
import pandas as pd
s1=pd.Series([2,3,1,3,33])
s2=pd.Series([1,4,6,8,3,5,7,10])
SUMS=s1+s2
print("Add Two Series:")
print(SUMS)
17
DIV=s1/s2
print(DIV)
OUTPUT :
Program:7
18
Write a program to sort the element of
Answer:
import pandas as pd
X=pd.Series(['100','200','python','300.12','400'])
print("Series before sorting:")
print(X)
Y=pd.Series(s1).sort_values()
print("Series after sorting:")
print(Y)
OUTPUT :
19
Program:8
Write a NumPy program to reverse an array.
Answer:
import numpy as np
NUM=[1,2,3,4,5]
Array=np.array(NUM)
print("Before Reverse",NUM)
NUM=NUM[::-1]
print("Reverse Array is",NUM)
OUTPUT :
20
Program:9
Write a NumPy program to create a 8x8
matrix and fill it with a checker board pattern.
[01010101]
[10101010]
[01010101]
[10101010]
[01010101]
[10101010]
[01010101]
[10101010]
Answer:
import numpy as np
x=np.ones((3,3))
print('Checkerboard pattern:')
21
x=np.zeros((8,8),dtype=int)
x[1::2,::2]=1
x[::2,1::2]=1
print(x)
OUTPUT :
Program:10
Write a NumPy program to append values to
the end of an array.
Answer:
import numpy as np
22
x=[10,20,30]
print('original array:')
print(x)
x=np.append(x,[40,50,60,70,80,90])
print("After append values to the end of the
array:")
print(x)
OUTPUT :
Program:11
Write a NumPy program to test whether each
element of 1-D array is also present in a second
array.
Answer:
23
import numpy as np
ar1=np.array([0,12,22,40,67])
print("Array1:",ar1)
ar2=[0,22]
print("Array2",ar2)
print("Compare each element of array1 and
array2")
print(np.in1d(ar1,ar2))
OUTPUT :
24
Program:12
Write a NumPy program to find the number of
elements of an array, length of one array
element in bytes consumed by the elements.
Answer:
import numpy as np
x=np.array([1,2,3],dtype=np.float64)
print("size of the array:",x.size)
print("Length of one array element in
bytes:",x.itemsize)
OUTPUT :
Program:13
25
Write pandas program to select the rows where
the height is not known , i.e. is NaN.
‘name’ :[‘Asha’, ‘Radha’, ‘Kamal’, ‘Divy’, ‘Anjali’],
‘height’ :[5.5, 5, np.nan, 5.9, np.nan],
‘age’:[11, 23, 22, 33, 22].
Answer:
import pandas as pd
import numpy as np
pers_data={'name':['Asha','Radha','Kamal','Divy',
'Ram Kohli'],
'height':[5.5,5,np.nan,5.9,np.nan],
'age':[11,23,22,33,22]}
labels=['a','b','c','d','e']
df=pd.DataFrame(pers_data,index=labels)
print('Persons whose height not known:')
print(df[df['height'].isnull()])
OUTPUT :
26
Program:14
Write a pandas program to select the name of
persons whose height is between 5 to 5.5 (both
values inclusive).
‘name’ :[‘Asha’, ‘Radha’, ‘Kamal’, ‘Divy’, ‘Anjali’],
‘height’ :[5.5, 5, np.nan, 5.9, np.nan],
‘age’:[11, 23, 22, 33, 22].
Answer:
import pandas as pd
import numpy as np
27
pers_data={'name':['Asha','Radha','Kamal','Divy','R
am Kohli'],
'height':[5.5,5,np.nan,5.9,np.nan],
'age':[11,23,22,33,22]}
labels=['a','b','c','d','e']
df=pd.DataFrame(pers_data,index=labels)
print('Persons whose height is between 5 and
5.5:')
print(df[df['height']>=5&(df['height']<=5.5)])
OUTPUT :
28
Program:15
Write pandas program to read marks detail of
Ram Kohli and calculate sum of all marks.
Answer:
import pandas as pd
import numpy as np
data={'Ram Kohli':['Physics','Chemistry','English',
'Maths','Computer Sc'],
'marks':[89,99,97,99,98],}
df=pd.DataFrame(data)
print("Sum of marks:")
print(df['marks'].sum())
OUTPUT :
29
Program:16
Write pandas program to sort the data frame
first by ‘designation’ in ascending order, then
by ‘name’ in descending order.
Answer:
import pandas as pd
data1={'Name':['Ram
Kohli','Sameer','Chetak','Neha'],
'Age':[28,34,29,42],'Designation':['Accountant','
Clerk','Clerk','Manager']}
df1=pd.DataFrame(data1)
print(df1.sort_values(by=['Designation','Name'],
ascending=[True,False]))
30
OUTPUT :
Program:17
Draw a histogram based on production of
wheat in different years,
Year:2000, 2002, 2004, 2006, 2008, 2010, 2012,
2014, 2016, 2018
Production:4,6,7,15,24,2,19,5,16,4.
Answer:
import pandas as pd
import matplotlib.pyplot as plt
31
data={'Year':[2000, 2002, 2004, 2006, 2008, 2010,
2012, 2014, 2016, 2018],\
'Production':[4,6,7,15,24,2,19,5,16,4]}
d=pd.DataFrame(data)
print(d)
x=d.hist(column='Production',bins=5,grid=True)
plt.show(x)
OUTPUT :
Program:18
32
Write a program to create data frame for 3
students including name and roll numbers.
And new columns for 5 subjects and 1 column
to calculate percentage.
Answer:
import pandas as pd
import numpy as np,random
D={'Roll':[1,2,3],'Name':['Sangeeta','Shanti','Swat
i']}
P=[]
C=[]
M=[]
E=[]
H=[]
SD=pd.DataFrame(D)
for i in range(3):
P.append(random.randint(1,101))
33
C.append(random.randint(1,101))
M.append(random.randint(1,101))
E.append(random.randint(1,101))
H.append(random.randint(1,101))
SD['Phy']=P
SD['Che']=C
SD['Math']=M
SD['Eng']=E
SD['Hin']=H
SD['Total']=SD.Phy+SD.Che+SD.Math+SD.Eng+
SD.Hin
SD['Per']=SD.Total/5
print(SD)
OUTPUT :
34
Program:19
Print the following pattern
1
22
333
4444
55555
Answer:
for num in range(6):
for i in range(num):
print (num, end=" ") #print number
# new line after each row to display pattern
correctly
print("\n")
OUTPUT :
35
Program:20
The number of bed-sheets manufactured by a
factory during five consecutive weeks is given
below.
Week 1 2 3 4 5
Bed- 600 850 700 300 900
sheets
number
Answer:
import matplotlib.pyplot as p
x=["First","Second","Third","Fourth","Fifth"]
y=[600,850,700,300,900]
36
plt.title("Production by factory")
plt.xlabel("WEEK")
plt.ylabel("Number of Bed-sheets")
plt.bar(x,y,color="Blue",width=.50)
plt.show()
OUTPUT :
37
38
39
40