Practical File: School Name School Logo
Practical File: School Name School Logo
School Logo
PRACTICAL FILE
Informatics Practices /
Computer Science
(Python)
Name :
Class:
School:
Roll No.
INDEX
Sign of the
S.No. Name of Practical Date Page No.
Teacher
2 Python program to
3 Python program to
4 Python program to
5 Python program to
6 Python program to
7 Python program to
8 Python program to
9 Python program to
10 Python program to
11 Python program to
12 Python program to
13 Python program to
14 Python program to
15 Python program to
23 MySQL Queries
1. Write a program to create a series using List with defined index
values.
import pandas as pd
list1 = [10,20,30,40,50]
series1 = pd.Series(list1, index = ['a','b','c','d','e'])
print(series1)
Output
2. Write a program to create a series using List with defined index
values and slicing data from a series.
import pandas as pd
list1 = [10,20,30,40,50]
s = pd.Series(list1, index = ['a','b','c','d','e'])
print(s[0]) #for 0 index position
print(s[:3]) #for first 3 index values
print(s[-3:]) #for last 3 index values
Output
3. Write a program to create a series using List with defined index
values and display the data using index wise and location wise.
import pandas as pd
list1 = [10,20,30,40,50]
s = pd.Series(list1, index = ['a','b','c','d','e'])
print(s.iloc[1:4])
print(s.loc['b':'e'])
Output
4. Write a program to create a series using dictionary
import pandas as pd
series = pd.Series({'Jan':31,'Feb':20,'Mar':31,'Apr':30})
print(series)
Output
5. Write a program to create a series through a mathematical
expression
import pandas as pd
import numpy as np
s1 = np.arange(10,15)
print(s1)
subj=pd.Series(index = s1,data=s1*4)
print(subj)
Output
6. Write a program to create a series using head() and tail() functions
import pandas as pd
series = pd.Series([10,20,30,40,50], index = ['a','b','c','d','e'])
print(series)
print(series.head(2))
#if no argument is provided then by default 5 rows are displayed
print(series.tail(2))
Output
7. Write a Pandas program to creat dataframe from a sample data
Sample data: {'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],
'Z':[86,97,96,72,83]}
import pandas as pd
df = pd.DataFrame({'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],
'Z':[86,97,96,72,83]});
print(df)
Output
8. Write a Pandas program to creat dataframe from a sample data
import pandas as pd
import numpy as np
df = pd.DataFrame(exam_data , index=labels)
print(df)
Output
9. Write a Pandas program to creat dataframe from a sample data
import pandas as pd
import numpy as np
df = pd.DataFrame(exam_data , index=labels)
print("First three rows of the data frame:")
print(df.iloc[:3])
Output
10. Write a Pandas program to creat dataframe from a sample data and create
one column with different name of colours
import pandas as pd
import numpy as np
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no',
'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
print("Original rows:")
print(df)
color =
['Red','Blue','Orange','Red','White','White','Blue','Green','Green','Red']
df['color'] = color
print("\nNew DataFrame after inserting the 'color' column")
print(df)
Output
11. Write a program to read csv file using pandas
import pandas as pd
data = pd.read_csv("file2.csv")
print(data.head())
Output
12. Write a program to write the data in file1.csv file using pandas
import pandas as pd
Output
13. Write a program to creat DataFrame from Lists with column
heading
import pandas as pd
data1 = [['Shreya', 20],['Rakshit', 22], ['srijan',18]]
df1 = pd.DataFrame(data1, columns=['Name','Age']) #Defining
column names to be displayed as headings
print(df1)
Output
14. write a program to create dataframe from two series of student
data
import pandas as pd
student_marks =
pd.Series({'Vijay':80,'Rahul':92,'Meghna':67,'Radhika':95,'Shaur
ya':97})
student_age =
pd.Series({'Vijay':32,'Rahul':28,'Meghna':30,'Radhika':25,'Shaur
ya':20})
student_df =
pd.DataFrame({'Marks':student_marks,'Age':student_age})
print(student_df)
Output
15. write a program to create dataframe from two series of student
and sort data in ascending and descending order by using marks
import pandas as pd
student_marks =
pd.Series({'Vijay':80,'Rahul':92,'Meghna':67,'Radhika':95,'Shaur
ya':97})
student_age =
pd.Series({'Vijay':32,'Rahul':28,'Meghna':30,'Radhika':25,'Shaur
ya':20})
student_df =
pd.DataFrame({'Marks':student_marks,'Age':student_age})
print(student_df)
#sorting the data on the basis of marks in ascending order
print(student_df.sort_values(by=['Marks'])) #by keyword
defines
#the field on the basis of which the data is to be sorted
print(student_df.sort_values(by=['Marks'],ascending=False))
#sorted in descending order of Marks
Output
16. write a program to plot the elements of two lists using a bar
chart.
x = [2,4,6,8,10]
y = [6,7,8,2,4]
x2 = [1,3,5,7,9]
y2 = [7,8,2,4,2]
plt.bar(x,y, label="Bars1")
plt.bar(x2,y2, label="Bars2")
plt.xlabel('x')
plt.ylabel('y')
def fnplot(list1):
plt.plot(list1)
plt.title("Marks Line Chart")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
list1=[50,50,50,65,65,75,75,80,80,90,90,90,90]
fnplot(list1)
Output
18. write a program to draw two lines along with proper titles and
legends
x = [1,2,3]
y = [5,7,4]
plt.plot(x,y,label='First line')
x2 = [1,2,3]
y2 = [10,11,14]
plt.plot(x2,y2,label='Second line')
plt.xlabel('Plot number')
plt.ylabel('Important variables')
plt.title('New Graph')
plt.legend()
plt.show()
Output
19. write a program to depict the relationshi between unemployment
Rate and Stock index price through a scatter plot.
Unemployment_Rate Stock_Index_Price
6.1 1500
5.8 1520
5.7 1525
5.7 1523
5.8 1515
5.6 1540
5.5 1545
5.3 1560
5.2 1555
5.2 1565
Unemployment_Rate = [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2]
Stock_Index_Price =
[1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
x1 = [10,20,30]
y1 = [20,40,10]
x2 = [10,20,30]
y2 = [40,10,30]
plt.xlabel('X - axis')
plt.ylabel('Y - axis')
plt.legend()
plt.show()
Output
21. Write a Python program to established connection with MySQL
and display all the data
import mysql.connector
mydb =
mysql.connector.connect(host="localhost",user="root",passwd="1
234")
mycursor = mydb.cursor()
mycursor.execute("show databases")
for x in mycursor:
print(x)
Output
22. Write a Python program to established connection with MySQL
and insert some data into client table
import mysql.connector
mydb =
mysql.connector.connect(host="localhost",user="root",passwd="1234
",database="gvn")
mycursor = mydb.cursor()
mycursor.execute("insert into client values(6,'mahesh','sagar',128)")
mydb.commit()
TABLE DEPARTMENT
Note:
– EName refers to Employee Name
– DName refers to Department Name
– Dept refers to Department Code
– HOD refers to Employee number (ENO) of the Head of the Department
Write SQL queries for the following:
SELECT e.ENo,e.EName,e.Salary,d.DName
FROM EMPLOYEE e, DEPARTMENT d
WHERE e.Dept = d.Dept AND e.Age BETWEEN 25 AND 35;
SELECT e.EName,d.DName
FROM EMPLOYEE e, DEPARTMENT d
WHERE e.ENO = d.HOD;
5.b) Consider the following tables Product and Client. Write SQL commands for the statement (i) to (iv) and give
outputs for SQL queries (v) to (vi)
Table: PRODUCT
Table: CLIENT
(i) To display the details of those Clients whose city is Delhi.
(ii) To display the details of Products whose Price is in the range of 50 to 100(Both values included).
(iii) To display the ClientName, City from table Client, and ProductName and Price from table Product, with their
corresponding matching P_ID.
NO_ NO_
FL_NO STARTING ENDING
FLGHTS STOPS
IC301 MUMBAI DELHI 8 0
IC799 BANGALORE DELHI 2 1
MC101 INDORE MUMBAI 3 0
IC302 DELHI MUMBAI 8 0
AM812 KANPUR BANGLORE 3 1
IC899 MUMBAI KOCHI 1 4
AM501 DELHI TRIVENDRUM 1 5
MU499 MUMBAI MADRAS 3 3
IC701 DELHI AHMEDABAD 4 0
TABLE:FLIGHTS
(ii) Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.
(iii) Display the FL_NO and fare to be paid for the flights from DELHI to MUMBAI using the tables
FLIGHTS and FARES, where the fare to paid = FARE+FARE+TAX%/100.
(iv) Display the minimum fare “Indian Airlines” is offering from the tables FARES.