Python Program
Python Program
PYTHON
What is Python?
Python is an interpreted, high level general purpose of programming
language created by Guido Van Rossum and first released in 1991.
What is Data Frame?
A Data Frame is a two dimensional data structure, i.e., data is aligned
in a tabular fashion in rows and columns. Pandas Data Frame consists
of three principal components: data, rows and columns.
What are Series?
Series is one-dimensional labeled array capable of holding data of any
type (integer, string, float, python objects, etc.). The axis labels are
collectively called index or index labels.
What is Data Visualization?
Pictorial representation of data using various visual elements like
charts, graphs, maps, etc. The purpose of data visualization is about
saving efforts, times and make you process easier to analyze the
particular data.
What is Matplotlib?
Matplotlib is a plotting library for the python programming language
and its numerical mathematical expression Numpy. It provides an
object oriented API for embedding plots into applications using
general purpose.
1
Python Programs
Based on basic operations
1. Write a program to find the area and circumference of a circle.
2. Write a program to find the distance between the points (x1, y1) and
(x2, y2).
3. Write a program to compute Simple Interest and Amount.
4. Write a program to find the hypotenuse of right angled triangle.
5. To calculate the bonus given to sales men.
6. Write a program to accept marks of student in five subjects and print
total marks and percentage obtained.
7. To perform the calculation of arithmetic progression.
Based on loop
1. Write a program to find the table of any number.
2. Write a program to print patterns.
3. Write a program to find factorial of any number.
4. Write a program to print the square and cube of N numbers.
2
Based on Pandas Series
1. Write a pandas program to create and display a one dimensional array-
like object containing an array of data using Pandas module.
2. Write a pandas program to convert a panda module series to Python list
and its type.
3. Write a Pandas program to add, subtract, multiple and divide two
Pandas series.
4. Write a pandas program to compare the elements of the two Pandas
series.
5. Write a pandas program to change the order of index of a series.
6. Write a pandas program to filter the elements of a series.
7. Write a pandas program to create the mean and standard deviation of
the data of a given series.
8. Write a pandas program to compute the minimum, 25th percentile,
median, 75th and maximum of a series.
9. Write a pandas program to sort a given series.
3
Based on Matplotlib
1. Write a program to plot a line chart to depict unemployment rate year
by year.
2. Write a program to plot a line graph to display the marks of classes X,
XI, XII.
3. Write a program to plot a pie chart depicting the percentage of gases in
our atmosphere.
4. Write a program to plot a multi range bar chart.
5. Write a program to create a histogram.
4
Python Programs
Output
Enter radius=35
Area= 3850.0
Circumference= 220.0
5
2. Write a program to find the distance between the
points (x1, y1) and (x2, y2).
Input
# Write a program to find the distance between the points (x1, y1)
and (x2, y2).
x1=float(input("x1= "))
y1=float(input("y1= "))
x2=float(input("x2= "))
y2=float(input("y2= "))
d=((x2-x1)**2+(y2-y1)**2)**0.5
print("Distance between two point is= ",d)
Output
x1= 10
y1= 6
x2= 5
y2= 8
Distance between two point is= 5.385164807134504
Input
Output
Enter the perpendicular side of triangle= 10
Enter the base of triangle= 12
Hypotenuse of right angled triangle= 15.620499351813308
7
5. To calculate the bonus given to sales men.
Input
#To find the percentage & amount of bonus given to a sales man on the
basis of his monthly sales
n=float(input("Enter the amount of your monthly sales="))
if(n>=15000 ):
print('You will get the bonus of 15% of sales you done this month,
which is =',n*15/100)
elif(n>=10000 and n<15000):
print('You will get the bonus of 10% of sales you done this month
,which is =',n*10/100)
elif(n>=5000 and n<10000):
print('You will get the bonus of 5% of sales you done this month,
which is =',n*5/100)
else:
print("No bonus")
Output
Enter the amount of your monthly sales=15678
You will get the bonus of 15% of sales you done this month, which
is = 2351.7
8
6. Write a program to accept marks of student in five
subjects and print total marks and percentage obtained.
Input
#program to calculate your %age and division for 12th.
print('''Note: here, we will calculate the percentage taking total as
500 marks and division you will get on the basis of your
percentage.''')
n1=float(input('Enter your Maths marks='))
n2=float(input('Enter your Chemistry marks='))
n3=float(input('Enter your English marks='))
n4=float(input('Enter your IP marks='))
n5=float(input('Enter your Physics ='))
a=n1+n2+n3+n4+n5
d=a/5
print("This is your percentage=",d)
if(d>=60):
print("you get 1st division")
elif(d>=45 and d<60):
print("You get 2nd division")
elif(d>=33 and d<45):
print("You get 3rd division")
else:
print("Fail")
9
Output
Note: here, we will calculate the percentage taking total as 500
marks and division you will get on the basis of your percentage.
Enter your Maths marks=78
Enter your Chemistry marks=87
Enter your English marks=65
Enter your IP marks=87
Enter your Physics =87
This is your percentage= 80.8
you get 1st division
10
7. To perform the calculation of arithmetic progression.
Input
#Here is the program to find arithmetic progression it's sum till N
#term and also to find any term
#of that arithmetic progression
a=float(input("Enter the first term of arithmetic progression="))
b=float(input("Enter the common difference of arithmetic
progression="))
c=float(input("Enter the Nth term till you want the sum of
arithmetic progression="))
d=float(input("Enter the Mth term whose value you want="))
f=a+(c-1)*b
g=c*(a+f)/2
h=a+(d-1)*b
print("This the value of Nth term=",f)
print("This the value of Mth term=",h)
print("This the sum of your progression till Nth term=",g)
print("This is your arithmetic progression=”, a, ',' ,a+b ,',' ,a+2*b ,','
,a+3*b,'.......')
Output
Enter the first term of arithmetic progression=2
Enter the common difference of arithmetic progression=2
Enter the Nth term till you want the sum of arithmetic
progression=8
Enter the Mth term whose value you want=9
This the value of Nth term= 16.0
This the value of Mth term= 18.0
This the sum of your progression till Nth term= 72.0
This is your arithmetic progression= 2.0, 4.0, 6.0, 8.0.......
11
Based on loop
1. Write a program to find the table of any number.
Input
#Here is the program to find the table of any number.
n=int(input("Enter the number whose tables you want="))
for i in range(1,11):
print(n,'x',i,'=',n*i)
Output
Enter the number whose tables you want=25
25 x 1 = 25
25 x 2 = 50
25 x 3 = 75
25 x 4 = 100
25 x 5 = 125
25 x 6 = 150
25 x 7 = 175
25 x 8 = 200
25 x 9 = 225
25 10 = 250
12
2. Write a program to print patterns.
*****
*****
*****
*****
*****
Input
#write a program to print pattern.
for row in range(5):
for col in range(5):
print("*",end=" ")
print()
Output
*****
*****
*****
*****
*****
13
3. Write a program to find factorial of any number.
Input
#Program to find the factorial of any number
a=int(input("Enter the number whose factorial you want= "))
b=1
if(a<0):
print("Sorry, factorial does not exist for negative numbers")
elif(a==0):
print("The factorial of 0 is 1")
else:
for i in range(1,a+1):
b=b*i
print("The factorial of",a,"is=",b)
Output
Enter the number whose factorial you want= 8
The factorial of 8 is= 40320
14
4. Write a program to print the square and cube of N
numbers.
Input
#to print the square and cube of n numbers.
a=int(input("Enter the value of N till the square and cube you
want="))
print("No."," square ","cube")
for i in range(1,a+1):
print(i," - ",i**2," - ",i**3)
Output
Enter the value of N till the square and cube you want=10
No. square cube
1 - 1 - 1
2 - 4 - 8
3 - 9 - 27
4 - 16 - 64
5 - 25 - 125
6 - 36 - 216
7 - 49 - 343
8 - 64 - 512
9 - 81 - 729
10 - 100 - 1000
15
Based on list and dictionaries in python
1. Write a program to add item in list at runtime.
Input
#Write a program to add item in list at runtime (real time)
#item must be entered by user any number of item.
l=[ ]
n=int(input("How many elements="))
for i in range(n):
print("Enter",i+1,"item=")
item=input()
l.append(item)
print("This is your list=",l)
Output
How many elements=3
Enter 1 item=
Gourav
Enter 2 item=
Sarthak
Enter 3 item=
Sachin
This is your list= ['Gourav', 'Sarthak', 'Sachin']
16
2. Write a program to delete item from list at runtime.
Input
#Write a program to delete item from list if item is in list
#then item delete otherwise item not found
#message print.
l=[ ]
n=int(input("How many elements="))
for i in range(n):
print("Enter",i+1,"item")
item=input()
l.append(item)
print("This is your list:",l)
print()
a=input("Enter which item you want to delete:")
print()
if a in l:
l.remove(a)
print("Item deleted")
else:
print("Item not found")
print()
print("This is updated list",l)
17
Output
How many elements=3
Enter 1 item
Gourav
Enter 2 item
Sarthak
Enter 3 item
Sachin
This is your list: ['Gourav', 'Sarthak', 'Sachin']
Item deleted
18
3. Write a program to modify item in list at runtime.
Input
#write a program to modify item in list.
l=[ ]
n=int(input("How many elements="))
for i in range(n):
print("Enter",i+1,"item")
item=input()
l.append(item)
print("This is your list:",l)
print()
a=int(input("Enter index number of the item which you want to
modify="))
print()
b=input("Enter new value=")
d=(len(l))
for p in range(d):
if(p==a):
l[p]=b
print("Item updated")
print()
print("This is your updated list:",l)
19
Output
How many elements=3
Enter 1 item
Sarthak
Enter 2 item
Gourav
Enter 3 item
Sachin
This is your list: ['Sarthak', 'Gourav', 'Sachin']
20
4. Write a program to add new key-value pair(s) in
dictionary at runtime.
Input
#program to add new key:value pair(s) in dictionary at runtime.
f={}
a=int(input("Enter how many key:value pairs you want in
dictionary="))
for i in range(1,a+1):
print("Enter the value of",i,"'key'=")
b=input()
print("Enter the value of",i,"'value'=")
c=input()
f[b]=c
import json
print("___________________")
print("This is your dictionary")
print(json.dumps(f,indent=1))
21
Output
Enter how many key:value pairs you want in dictionary=3
Enter the value of 1 'key'=
Sachin
Enter the value of 1 'value'=
78
Enter the value of 2 'key'=
Sarthak
Enter the value of 2 'value'=
80
Enter the value of 3 'key'=
Gourav
Enter the value of 3 'value'=
84
___________________
This is your dictionary
{
"Sachin": "78",
"Sarthak": "80",
"Gourav": "84"
}
22
5. Write a program to delete key-value pair(s) in
dictionary at runtime.
Input
#program to delete multiple key:value pair(s) in dictionary at
runtime.
f={}
a=int(input("Enter how many key:value pairs you want in
dictionary="))
for i in range(1,a+1):
print("Enter the value of",i,"'key'=")
b=input()
print("Enter the value of",i,"'value'=")
c=input()
f[b]=c
import json
print("___________________")
print("This is your dictionary")
print(json.dumps(f,indent=1))
d=int(input("Enter how many key:value pairs you want to delete
from dictionary="))
for j in range(1,d+1):
print("Enter the key which you want to delete",j,"=")
e=input()
del f[e]
print("___________________")
print("This is your updated dictionary")
print(json.dumps(f,indent=1))
23
Output
Enter how many key:value pairs you want in dictionary=3
Enter the value of 1 'key'=
Shubhanshu
Enter the value of 1 'value'=
87
Enter the value of 2 'key'=
Aayuh
Enter the value of 2 'value'=
78
Enter the value of 3 'key'=
Sagar
Enter the value of 3 'value'=
80
___________________
This is your dictionary
{
"Shubhanshu": "87",
"Aayuh": "78",
"Sagar": "80"
}
Enter how many key:value pairs you want to delete from
dictionary=1
Enter the key which you want to delete 1 =
Aayuh
___________________
This is your updated dictionary
{
"Shubhanshu": "87",
"Sagar": "80"
}
24
6. Write a program to modify the value of key(s) in
dictionary at runtime.
Input
#program to modify value of key:value pair(s) in dictionary at
runtime.
f={}
a=int(input("Enter how many key:value pairs you want in
dictionary="))
for i in range(1,a+1):
print("Enter the value of",i,"'key'=")
b=input()
print("Enter the value of",i,"'value'=")
c=input()
f[b]=c
import json
print("___________________")
print("This is your dictionary")
print(json.dumps(f,indent=1))
d=int(input("Enter how many value you want to modify in
dictionary="))
for j in range(1,d+1):
print("Enter the",j,"key whose value you want to modify=")
e=input()
print("Enter the",j,"new value=")
g=input()
f[e]=g
print("___________________")
print("This is your updated dictionary")
print(json.dumps(f,indent=1))
25
Output
Enter how many key:value pairs you want in dictionary=3
Enter the value of 1 'key'=
Nachiket
Enter the value of 1 'value'=
78
Enter the value of 2 'key'=
Gourav
Enter the value of 2 'value'=
80
Enter the value of 3 'key'=
Mohit
Enter the value of 3 'value'=
77
___________________
This is your dictionary
{
"Nachiket": "78",
"Gourav": "80",
"Mohit": "77"
}
Enter how many value you want to modify in dictionary=1
Enter the 1 key whose value you want to modify=
Mohit
Enter the 1 new value=
75
___________________
This is your updated dictionary
{
"Nachiket": "78",
"Gourav": "80",
"Mohit": "75"
}
26
Based on Pandas Series
1. Write a pandas program to create and display a one
dimensional array-like object containing an array of
data using Pandas module.
Input
#Write a pandas program to create and display a one dimensional
array-like object containing an array of data using Pandas module.
import pandas as pd
s1=pd.Series([1,2,3,4,5])
print(s1)
Output
0 1
1 2
2 3
3 4
4 5
dtype: int64
27
2. Write a pandas program to convert a panda module
series to Python list and its type.
Input
#Write a pandas program to convert a panda module series to
Python list and its type.
import pandas as pd
ds=pd.Series([2, 4, 6, 8, 10])
print("Pandas series and its type")
print(ds)
print(type(ds))
print("Convert pandas series to pandas list")
print(ds.tolist())
print(type(ds.tolist()))
Output
Pandas series and its type
0 2
1 4
2 6
3 8
4 10
dtype: int64
<class 'pandas.core.series.Series'>
Convert pandas series to pandas list
[2, 4, 6, 8, 10]
<class 'list'>
28
3. Write a Pandas program to add, subtract, multiple and
divide two Pandas series.
Input
#Write a Pandas program to add, subtract, multiple and divide two
pandas series.
s1=pd.Series([1,3,5,7,9])
s2=pd.Series([2,4,6,8,10])
print(s2+s1)
print(s2-s1)
print(s2*s1)
print(s2/s1)
29
Output
0 3
1 7
2 11
3 15
4 19
dtype: int64
0 1
1 1
2 1
3 1
4 1
dtype: int64
0 2
1 12
2 30
3 56
4 90
dtype: int64
0 2.000000
1 1.333333
2 1.200000
3 1.142857
4 1.111111
dtype: float64
30
4. Write a pandas program to compare the elements of the
two Pandas series.
Input
#Write a pandas program to compare the elements of the two
Pandas series.
s1=pd.Series([1,3,5,7,10])
s2=pd.Series([2,4,6,8,10])
print(s1)
print(s2)
print("Comparing the elements of above two series")
print("Equals:")
print(s1==s2)
print("Greater than:")
print(s1>s2)
print("Less than:")
print(s1<s2)
31
Output
0 1
1 3
2 5
3 7
4 10
dtype: int64
0 2
1 4
2 6
3 8
4 10
dtype: int64
Comparing the elements of above two series
Equals:
0 False
1 False
2 False
3 False
4 True
dtype: bool
Greater than:
0 False
1 False
2 False
3 False
4 False
dtype: bool
Less than:
0 True
1 True
2 True
3 True
4 False
dtype: bool
32
5. Write a pandas program to change the order of index
of a series.
Input
#Write a pandas program to change the order of index of a series.
import pandas as pd
s1=pd.Series(data=[1,2,3,4,5], index=["a","b","c","d","e"])
print("Original data series")
print(s1)
s1=s1.reindex(index=["b","c","a","e","d"])
print("Data series after changing the index")
print(s1)
Output
Original data series
a 1
b 2
c 3
d 4
e 5
dtype: int64
Data series after changing the index
b 2
c 3
a 1
e 5
d 4
dtype: int64
33
6. Write a pandas program to filter the elements of a
series.
Input
#Write a pandas program to filter the elements of a series.
s1=pd.Series([120,23,34,54,64])
print(s1[s1>40])
print(s1<60)
Output
0 120
3 54
4 64
dtype: int64
0 False
1 True
2 True
3 True
4 False
dtype: bool
34
7. Write a pandas program to create the mean and
standard deviation of the data of a given series.
Input
#Write a pandas program to create the mean and standard deviation
of the data of a given series.
import pandas as pd
s1=pd.Series([1,2,3,4,5,6,7,1,2,3,4,5])
print("Original data series:")
print(s1)
print("Mean of the data series:")
print(s1.mean())
print("Standard deviation of data series")
print(s1.std())
Output
Original data series:
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 1
8 2
9 3
10 4
11 5
dtype: int64
Mean of the data series:
3.5833333333333335
Standard deviation of data series
1.9286515936521478
35
8. Write a pandas program to compute the minimum,
25th percentile, median, 75th and maximum of a
series.
Input
#Write a pandas program to compute the minimum, 25th percentile,
median, 75th and maximum of a series.
import pandas as pd
s1=pd.Series([10,20,30,40,50,60])
print("Maximum and Minimum values are")
print(s1.max(),"and",s1.min(),"respectively")
print("25th percentile, median and 75th percentile are")
print(s1.quantile(0.25),",",s1.quantile(0.5),"and",s1.quantile(0.75),"
respectively.")
Output
Maximum and Minimum values are
60 and 10 respectively
25th percentile, median and 75th percentile are 22.5, 35.0 and 47.5
respectively.
36
9. Write a pandas program to sort a given series.
Input
#Write a pandas program to sort a given series.
import pandas as pd
s1=pd.Series(["100","200","Python","500.34","400",])
print("Original data series")
print(s1)
ns=s1.sort_values()
print("Data series after sorting")
print(ns)
Output
Original data series
0 100
1 200
2 Python
3 500.34
4 400
dtype: object
Data series after sorting
0 100
1 200
4 400
3 500.34
2 Python
dtype: object
37
Based on Pandas Data Frame
1. Write a Pandas program to create a data frame from
dictionary having values as list/ ND array.
Input
#Write a Pandas program to create a dataframe from dictionary
having values as list/ndarray.
import pandas as pd
data=(
{
"Name":["Gourav","Kushagra","Almas","Prince","Sarthak","Sumit"
],
"Age":[17,18,16,19,20,15],
"City":["Delhi","Mumbai","Bangalore","Pune","Goa","Karnataka"]
}
)
df=pd.DataFrame(data)
print(df)
Output
Name Age City
0 Gourav 17 Delhi
1 Kushagra 18 Mumbai
2 Almas 16 Bangalore
3 Prince 19 Pune
4 Sarthak 20 Goa
5 Sumit 15 Karnataka
38
2. Write a Pandas program to create a data frame from
dictionary having values as dictionary object.
Input
#Write a Pandas program to create a dataframe from dictionary
having values as dictionary object.
import pandas as pd
data={
"Engineer":{"Name":"Gourav","Age":24,"City":"Delhi"},
"Doctor":{"Name":"Kushagra","Age":23,"City":"Mumbai"},
"Pilot":{"Name":"Almas","Age":22,"City":"Pune"}
}
df=pd.DataFrame(data)
print(df)
Output
Engineer Doctor Pilot
Name Gourav Kushagra Almas
Age 24 23 22
City Delhi Mumbai Pune
39
3. Write a Pandas program to create a data frame from
list of dictionaries.
Input
#Write a Pandas program to create a dataframe from list of
dictionaries.
import pandas as pd
A={"Roll no.":101,"Name":"Gourav","Age":24,"City":"Delhi"}
B={"Roll no.":102,"Name":"Sarthak","Age":21,"City":"Mumbai"}
C={"Roll no.":103,"Name":"Almas","Age":23,"City":"Pune"}
D={"Roll no.":104,"Name":"Kushagra","Age":22,"City":"Patna"}
E={"Roll no.":105,"Name":"Prince","Age":29,"City":"Bangalore"}
df=pd.DataFrame([A,B,C,D,E])
print(df)
Output
Roll no. Name Age City
0 101 Gourav 24 Delhi
1 102 Sarthak 21 Mumbai
2 103 Almas 23 Pune
3 104 Kushagra 22 Patna
4 105 Prince 29 Bangalore
40
4. Write a Pandas program to create a DF 2D array.
Input
#Write a Pandas program to create a df 2D array.
import pandas as pd
import numpy as np
arr=np.array([[11,12],[13,14],[15,16]])
df=pd.DataFrame(arr, columns=["one",'two'],index=["a","b","c"])
print(df)
Output
one two
a 11 12
b 13 14
c 15 16
41
5. Write a Pandas program to create a data frame from
list of series objects.
Input
#Write a Pandas program to create a dataframe from list of series
object.
import pandas as pd
s1=pd.Series(["Gourav","Sarthak","Kushagra","Almas","Prince"])
s2=pd.Series([17,18,16,19,15])
s3=pd.Series([50000,40000,45000,50000,47000])
data={"Name":s1,"Age":s2,"Fees":s3}
df=pd.DataFrame(data)
print(df)
Output
Name Age Fees
0 Gourav 17 50000
1 Sarthak 18 40000
2 Kushagra 16 45000
3 Almas 19 50000
4 Prince 15 47000
42
6. Write a Pandas program to create a data frame from
csv file.
Input
#Write a Pandas program to create a dataframe from csv file.
import pandas as pd
df=pd.read_csv("/content/Dataframe.csv")
print(df)
Output
0 A B C D
0 Maths 99 94 92 99
1 Physics 94 94 92 97
2 Chemistry 95 89 91 89
3 IP 94 87 99 94
4 English 97 100 99 99
43
7. Write a Pandas program to create a DF with Boolean
indexing.
Input
#Write a Pandas program to create a df with Boolean indexing.
import pandas as pd
days=["Mon","Tues","Wed","Thus","Fri"]
classes=[6,0,3,0,8]
data={"Days":days,"No.of classes":classes}
df=pd.DataFrame(data,index=[True,False,True,False,True])
print(df)
Output
Days No.of classes
True Mon 6
False Tues 0
True Wed 3
False Thus 0
True Fri 8
44
8. Write a program to perform descriptive statics with
pandas.
Input
#Write a program to perform descriptive statics with pandas.
import pandas as pd
df=df=pd.read_csv("/content/Dataframe.csv")
print(df)
df1=df.rename(index={0:"Maths",1:"Chemistry",2:"English",3:"IP"
,4:"Physics"})
print(df1)
print("Maximum")
print(df1.max())
print("Maximum along axis=1")
print(df1.max(axis=1))
print("Minimum")
print(df1.min())
print("Minimum alon axis=1")
print(df1.min(axis=1))
print("Mode")
print(df1.mode())
print("Mode along axis=1")
print(df1.mode(axis=1))
print("Mean")
print(df1.mean())
print("Mean along axis=1")
print(df1.mean(axis=1))
print("Standard deviation")
print(df1.std())
print("Standard deviation along axis=1")
print(df1.std(axis=1))
print("Variance")
print(df1.mode())
print("Variance along axis=1")
print(df1.mode(axis=1))
45
Ouput
0 A B C D
0 Maths 99 94 92 99
1 Physics 94 94 92 97
2 Chemistry 95 89 91 89
3 IP 94 87 99 94
4 English 97 100 99 99
0 A B C D
Maths Maths 99 94 92 99
Chemistry Physics 94 94 92 97
English Chemistry 95 89 91 89
IP IP 94 87 99 94
Physics English 97 100 99 99
Maximum
0 Physics
A 99
B 100
C 99
D 99
dtype: object
Maximum along axis=1
Maths 99
Chemistry 97
English 95
IP 99
Physics 100
dtype: int64
Minimum
0 Chemistry
A 94
B 87
C 91
D 89
dtype: object
46
Minimum alon axis=1
Maths 92
Chemistry 92
English 89
IP 87
Physics 97
dtype: int64
Mode
0 A B C D
0 Chemistry 94.0 94.0 92.0 99.0
1 English NaN NaN 99.0 NaN
2 IP NaN NaN NaN NaN
3 Maths NaN NaN NaN NaN
4 Physics NaN NaN NaN NaN
Mode along axis=1
0
Maths 99
Chemistry 94
English 89
IP 94
Physics 99
Mean
A 95.8
B 92.8
C 94.6
D 95.6
dtype: float64
Mean along axis=1
Maths 96.00
Chemistry 94.25
English 91.00
IP 93.50
Physics 98.75
dtype: float64
47
Standard deviation
A 2.167948
B 5.069517
C 4.037326
D 4.219005
dtype: float64
Standard deviation along axis=1
Maths 3.559026
Chemistry 2.061553
English 2.828427
IP 4.932883
Physics 1.258306
dtype: float64
Variance
0 A B C D
0 Chemistry 94.0 94.0 92.0 99.0
1 English NaN NaN 99.0 NaN
2 IP NaN NaN NaN NaN
3 Maths NaN NaN NaN NaN
4 Physics NaN NaN NaN NaN
Variance along axis=1
0
Maths 99
Chemistry 94
English 89
IP 94
Physics 99
48
Based on Matplotlib
1. Write a program to plot a line chart to depict
unemployment rate year by year.
Input
#Write a program to plot a line chart to depict unemployment rate
year by year.
import matplotlib.pyplot as plt
year=[1920,1930,1940,1950,1960,1970,1980,1990,2000,2010,2020]
ur=[9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3,6]
plt.plot(year,ur)
plt.title("Unemployement rate vs year")
plt.xlabel("Year")
plt.ylabel("Unemployment Rate")
plt.show()
Output
49
2. Write a program to plot a line graph to display the
marks of classes X, XI, XII.
Input
# Write a program to plot a line graph to display the marks of
classes X, XI, XII.
import matplotlib.pyplot as plt
x=[56,67,98,78,23,45,79]
xi=[67,89,90,65,78,60,91]
xii=[56,86,79,74,29,76,88]
stu=["Tom","Jerry","Nobita","Doraemon","Henry","Sinchan","Hatt
ori"]
plt.title("Student percentage graph")
plt.xlabel("Name of student")
plt.ylabel("Student progress")
plt.plot(stu,x,marker="d",color="red")
plt.plot(stu,xi,color="black",linestyle="dotted")
plt.plot(stu,xii,color="m",linestyle="dashdot")
plt.show()
Output
50
3. Write a program to plot a pie chart depicting the
percentage of gases in our atmosphere.
Input
# Write a program to plot a pie chart depicting the percentage of
gases in our atmosphere.
import matplotlib.pyplot as plt
per=[78,21,7,2]
gas=["Nitrogen","Oxygen","Argon","Carbon Dioxide"]
plt.title("Atmosphere")
color=["red","cyan","pink","yellow"]
plt.pie(per,labels=gas,colors=color,autopct="%5.2F%%")
Output
51
4. Write a program to plot a multi range bar chart.
Input
#Write a program to plot a multirange bar chart.
import matplotlib.pyplot as plt
import numpy as np
val=[[5,25,45,20],[4,23,49,17],[6,22,47,19]]
x=np.arange(4)
plt.bar(x+0.00,val[0],width=0.25,label="range1")
plt.bar(x+0.25,val[1],width=0.25,label="range2")
plt.bar(x+0.50,val[2],width=0.25,label="range3")
plt.legend(loc="upper left")
plt.title("Multirange bar chart")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
Output
52
5. Write a program to create a histogram.
Input
#Write a program to create a histogram.
import matplotlib.pyplot as plt
f=[78,72,69,81,63,67,65,75,79,74,71,79,80,69]
plt.hist(f,cumulative=True)
plt.show()
Output
53