CLASS XII - IP List of Practicals with Coding 2020
CLASS XII - IP List of Practicals with Coding 2020
CLASS XII
INFORMATICS PRACTICES
1. Write a program to create a Series object using an Nd-array that has 10 elements
in the range 100 to 200?
Solution:
import numpy as np
import pandas as pd
s = pd.Series(np.linspace(110,200,10))
print(s)
Output
2. Write a program to create a Series object using a dictionary that stores the number
of students in each section of class 12 in your school?
Solution:
import pandas as pd
students = {'A':39,'B':41,'C':42,'D':44}
s1 = pd.Series(students)
print(s1)
Output
3. Consider the series object S that stores the contribution of each section, as shown
below:
A 4300
B 6500
C 3900
D 6100
Write code to modify the amount of section ‘A’ as 3400 and for section ‘C’ and ‘D’ as
5000. Print the changed object.
Solution:
import pandas as pd
D = {'A':4300,'B':6500,'C':3900,'D':6100}
S = pd.Series(D)
S[0] = 3400
S[2:] = 5000
print(S)
Output
4. Write a program to create a Data Frame Quarterly Sales where each row contains
the Quarterly Sales of TV, Freeze and AC. Show the DataFrame after deleting details
of Freeze.
Solution:
import pandas as pd
PData = {
'TV' : {'QTR1' : 200000, 'QTR2': 230000, 'QTR3' : 210000, 'QTR4':24000},
'Freeze' : {'QTR1' : 300000, 'QTR2': 200000, 'QTR3' : 290000, 'QTR4':210000},
'AC' : {'QTR1' : 240000, 'QTR2': 153000, 'QTR3' : 245000, 'QTR4':170000}
}
df = pd.DataFrame(PData)
print(df)
del df['Freeze']
print(df)
Output
5. Write a program to create a DataFrame to store Roll Number, Name and Marks of
five students. Print the DataFrame and its transpose.
Solution:
import pandas as pd
PData = {
'Roll Number' : [101,102,103,104,105],
'Name' : ['Karan','Taran','Piyush','Bhupinder','Simran'],
'Marks' : [82,85,79,86,94]
}
df = pd.DataFrame(PData)
print("Original DataFrame")
print(df)
print("DataFrame after Transpose")
print(df.T)
Output
6. Write a program to create a DataFrame from a CSV file. Display the shape
(Number of rows and columns) of the CSV file.
Solution:
import pandas as pd
df = pd.read_csv('F://PP.csv') # All Columns
print(df)
rows, columns = df.shape
print("Number of rows : ",rows)
print("Number of columns : ",columns)
Output
8. Given the school result data, analyses the performance of the students in
Accountancy and Business studies using bar graph.
Solution:
import matplotlib.pyplot as plt
import numpy as np
label = ['Anil', 'Vikas', 'Dharma', 'Mahen', 'Manish', 'Rajesh']
Accountancy = [94,85,45,25,50,54]
BusinessStudy =[98,76,34,65,55,88]
index = np.arange(len(label))
plt.bar(index, Accountancy,label="Accountancy", color='g')
plt.bar(index+0.3, BusinessStudy,label = "Business Study", color='r')
plt.xlabel('Student Name', fontsize=12)
plt.ylabel('Percentage', fontsize=12)
plt.xticks(index, label, fontsize=12, rotation=90)
plt.title('Percentage of Marks achieve by student Class XII')
plt.legend()
plt.show()
Output
9. Given datasets population of India and Pakistan in the given years. Show the
population of India and Pakistan using line chart with title and labels of both axis.
Year 1960 1970 1980 1990 2000 2010
Population of Pakistan 44.91 58.09 78.07 107.7 138.5 170.6
Population of India 449.48 553.57 696.783 870.133 1000.4 1309.1
Solution:
from matplotlib import pyplot as plt
year = [1960, 1970, 1980, 1990, 2000, 2010]
pop_pakistan = [44.91, 58.09, 78.07, 107.7, 138.5, 170.6]
pop_india = [449.48, 553.57, 696.783, 870.133, 1000.4, 1309.1]
plt.plot(year, pop_pakistan, color='k')
plt.plot(year, pop_india, color='orange')
plt.xlabel('Countries')
plt.ylabel('Population in million')
plt.title('Pakistan India Population till 2010')
plt.show()import matplotlib.pyplot as plt
plt.show()
Output
10. Write a program to plot a bar chart from the medals won by four countries. Make
sure that bars are separately visible.
Solution:
from matplotlib import pyplot as plt
import numpy as np
plt.figure(figsize = (10,7))
info = ['Gold','Silver','Bronze','Total']
India = [80,59,59,198]
England = [45,45,46,136]
Australia = [26,20,20,66]
Canada = [15,40,27,82]
X = np.arange(len(info))
plt.bar(info,India,width = 0.15,label="India")
plt.bar(X + 0.15,England,width = 0.15,label="England")
plt.bar(X + 0.30,Australia,width = 0.15,label="Australia")
plt.bar(X + 0.45,Canada,width = 0.15,label = "Canada")
plt.xlabel('Medal Type')
plt.ylabel('Medals')
plt.title('Medals won by four countries')
plt.legend()
plt.show()
Output
11. A survey gathers height and weight of 60 participants and recorded the
participants’ ages as:
Ages = [1,1,2,3,5,7,8,9,10,10,11,13,13,15,16,17,18,19,20,21,21,23,24,24,24,25,25,
25,25,26,26,26,27,27,27,27,27,29,30,30,30,30,31,33,34,34,34,35,36,36,37,37,37,3
8,38,39,40,40,41,41]
Write a program to plot a histogram from above data with 20 bins.
Solution:
from matplotlib import pyplot as plt
Ages = [1,1,2,3,5,7,8,9,10,10,11,13,13,15,16,17,18,19,20,
21,21,23,24,24,24,25,25, 25,25,26,26,26,27,27,27,
27,27,29,30,30,30,30,31,33,34,34,34,35,36,36,37,
37,37,38,38,39,40,40,41,41]
plt.hist(Ages,bins = 20)
plt.title("Participants' Ages Histogram")
plt.show()
Output
12. Write a program to create data series and then change the indexes of the Series
object in any random order.
Solution:
import pandas as pd
import numpy as np
s1 = pd.Series(data = [100,200,300,400,500], index = ['I','J','K','L','M'])
print("Original Data Series")
print(s1)
s1 = s1.reindex(index = ['K','L','M','J','I'])
print("Data Series after changing the index")
print(s1)
Output
13. In an online contest, two 2-player teams’ point in 4 rounds are stored in two
DataFrames as shown below:
Team 1’s points (df1) Team 1’s points (df2)
P1 P2 P1 P2
1 700 490 1 1100 1400
2 975 460 2 1275 1260
3 970 570 3 1270 1500
4 900 590 4 1400 1190
Write a program to calculate total points earned by both the teams in each round.
Solution:
import pandas as pd
data1 = {'P1' : {'1':700,'2':975,'3':970,'4':900},
'P2' : {'1':490,'2':460,'3':570,'4':590}}
data2 = {'P1' : {'1':1100,'2':1275,'3':1270,'4':1400},
'P2' : {'1':1400,'2':1260,'3':1500,'4':1190}}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
print("Performace of Team - 1")
print(df1)
print("Performace of Team - 2")
print(df2)
print("Points earned by both Teams")
print(df1 + df2)
Output
14. Write a program to create a DataFrame with two columns name and age. Add a
new column Updated Age that contains age increased by 10 years.
Solution:
import pandas as pd
data = {'Name' : ['Amit','Sumit','Rahul','Dev'],
'Age' : [20,22,16,15]
}
df = pd.DataFrame(data)
print("Original DataFrame")
print(df)
df['Updated Age'] = df['Age'] + 10
print("DataFrame After adding column Updated Age")
print(df)
Output
City Schools
Delhi 7654
Mumbai 8700
Kolkata 9800
Chennai 8600
Write a program to print DataFrame one row at a time. Also display first two rows of
City column and last two rows of Schools column.
Solution:
import pandas as pd
data = {'City' : ['Delhi','Mumbai','Kolkata','Chennai'],
'Schools' : [7654,8700,9800,8600]
}
df = pd.DataFrame(data)
for i, j in df.iterrows():
print(j)
print("-----------------------")
print("*******************************")
print("First two rows of City Column")
print(df.City.head(2))
print("*******************************")
print("Last two rows of Schools Column")
print(df.Schools.tail(2))
Output
16. Write a program to plot a horizontal bar chart from heights of some stdents.
Solution:
import matplotlib.pyplot as plt
import numpy as np
Names = ['Ram','Shyam','Rama','Kashish','Mayank']
Heights = [5.1,5.5,6.0,5.0,6.3]
Weights = [20,30,15,18,22]
X = np.arange(len(Names))
plt.barh(Names,Heights, height = 0.5,label = "Height", color ='g')
plt.barh(X+0.5,Weights, height =0.5,label = "Weight", color ='b')
plt.xlabel("Height")
plt.ylabel("Names")
plt.legend()
plt.show()
Output
17. Write SQL commands on the basis of the following tables.
Table: PROUDCT
3) Display Product Name and Price from table Product where product name starts
with “B”.
FROM PRODUCT
18. Consider the following tables HOSPITAL. Write SQL commands for (i) to (iv) and
give outputs for SQL queries (v) to (viii).
No Name Age Department Dateofadmin Charge Sex
1 Arpit 62 Surgery 21/01/06 300 M
2 Zayana 18 ENT 12/12/05 250 F
3 Kareem 68 Orthopedic 19/02/06 450 M
4 Abhilash 26 Surgery 24/11/06 300 M
5 Dhanya 24 ENT 20/10/06 350 F
6 Siju 23 Cardiology 10/10/06 800 M
7 Ankita 16 ENT 13/04/06 100 F
8 Divya 20 Cardiology 10/11/06 500 F
9 Nidhin 25 Orthopedic 12/05/06 700 M
10 Hari 28 Surgery 19/03/06 450 M
(i) To show all information about the patients whose names are having six
characters only.
SELECT *
FROM Hospital
WHERE length(name) = 6;
(ii) To reduce Rs. 200 from the charge of female patients who are in Cardiology
department.
UPDATE Hospital
SET Charge = Charge – 200
WHERE Sex = ‘F’ AND Department = ‘Cardiology’;
(iii) To insert a new row in the above table with the following data :
Sum(Charge)
1200
COUNT(DISTINCT Department )
4
Department Sum(Charge)
Cardiology 1300
ENT 700
Orthopedic 1150
Surgery 1050
(viii) Select Name from HOSPITAL where Sex=’F’ AND Age > 20;
Name
Zayana
Dhanya