XYX Public School
Kalyanpur
Session 2022-2023
Computer Science Project File
Submitted To: Submitted By:
Subject Teacher Name-
Class-
CERTIFICATE
This is to certify that <Your name here> of class XII (STREAM) of
<School name> has done his/her Practical on Python program and
Mysql queries under my supervision. He / She has taken interest
and has shown at most sincerity in completion of this project. I
certify this Project up to my expectation & as per guidelines issued
by CBSE, NEW DELHI.
Internal Examiner
External Examiner
Acknowledgement
It is with pleasure that I acknowledge my sincere gratitude to our
teacher, < Teacher name > who taught and undertook the
responsibility of teaching the subject computer science. I have
been greatly benefited from his/her classes.
My sincere thanks goes to our Principal <Principal name> who has
always been a source of encouragement and support and without
whose inspiration, this project would not have been a successful.
Finally, I would like to express my sincere appreciation for all the
other students for my batch their friendship & the fine times that
we all shared together.
Last but not least, I would like to thank all those who had helped
directly or indirectly towards the completion of this project.
Name of student
Class
Part A
Python programs
Q1: Write a python program to accept the percentage of student and display the
grade accordingly.
Ans:
per=float(input("Enter Percentage:"))
if per>85:
print("A")
elif per > 70 and per <=85:
print("B")
elif per > 60 and per <=70:
print("C")
elif per > 45 and per <=60:
print("D")
else:
print("E")
Q2: Write python program to check largest/smallest number among the three
inputted numbers.
Ans:
n1=int(input("Enter first number:"))
n2=int(input("Enter second number:"))
n3=int(input("Enter third number:"))
if n1>n2 and n1>n3:
print(n1," is largest")
elif n2>n1 and n2>n3:
print(n2," is largest")
else:
print(n3," is largest")
Q3: Write a program to accept number from user and print the table of the
number.
Ans:
Using while loop:
n=int(input("Enter a number:"))
i=1
while i<=10:
print(n,"X",i,"=",n*i)
i=i+1
Q4: Write a program to enter a number and check if it is a prime number or
composite number.
Ans:
num=int(input("Enter any number:"))
if num >1:
for i in range(2,num):
if num%i==0:
print(num,'is a composite number')
break
else:
print(num,'is a prime number')
else:
print("Enter greater than 1 number")
Q5: Write a program to the check leap year.
Ans:
year=int(input("Enter year:"))
if year%100==0:
if year%400==0:
print(year, "is leap year")
else:
print(year, "is not leap year")
elif year%4==0:
print(year, "is leap year")
else:
print(year, "is not leap year")
Q6: Write a program to print prime numbers between given interval.
Ans:
lower=int(input("Enter lower limit, Greater than 1:"))
upper=int(input("Enter upper limit:"))
print("Prime number between",lower," to ",upper)
for num in range(lower,upper+1):
if num>1:
for i in range(2,num):
if num%i==0:
break
else:
print(num)
Q7: Write a program to compute LCM of two integers.
Ans:
n1=int(input("Enter first number:"))
n2=int(input("Enter second number:"))
if n1>n2:
higher=n1
else:
higher=n2
value=higher
while True:
if higher%n1==0 and higher%n2==0:
print("LCM of ",n1, "and",n2,"is ",higher)
break
else:
higher=higher+value
Q8: Write a program to count the number of vowels, consonants, uppercase,
lowercase in the string.
Ans:
word=input("Enter any string:")
vcount=0
ccount=0
ucount=0
lcount=0
for i in word:
if i in "AEIOUaeiou":
vcount=vcount+1
if i not in "AEIOUaeiou":
ccount=ccount+1
if i.isupper():
ucount=ucount+1
if i.islower():
lcount=lcount+1
print("Number of Vowels are: ",vcount)
print("Number of Consonants are: ",ccount)
print("Number of Uppercases are: ",ucount)
print("Number of Uppercases are: ",lcount)
Q9: Give python code to accept values from the user up to a certain limit entered
by the user, if the number is even, then add it to a list.
Ans:
l=[]
n=int(input("No. of item:"))
for i in range(n):
a=int(input("Enter element:"))
if a%2==0:
l.append(a)
print(l)
Q10: Write a python program to create a dictionary with the roll number, name
and marks of n students in a class and display the names of students who have
marks above 75.
Ans:
d={}
n=int(input("Enter Limit:"))
for i in range(n):
roll_no=input("Enter Roll no.:")
name=input("Enter name:")
marks=int(input("Enter marks:"))
d[roll_no]=(name,marks)
print(d)
for i in d:
if d[i][1]>75:
print(d[i][0])
Q11. Write a user defined function in Python that displays the number of lines
starting with ‘H’ in the file story.txt. Eg: if the file contains:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
Then the line count should be 2
Ans:
def countH():
f=open("story.txt","r")
d=f.readlines()
c=0
for i in d:
if i[0]=="H":
c=c+1
print("Line count is",c)
countH()
Q12: Writing Python program to add multiple record in a binary file.
Ans:
import pickle
def write():
file=open('demo.txt','ab')
y='y'
while y=='y':
roll=int(input('Enter roll no.:'))
name=input('Enter name:')
marks=int(input('Enter Marks:'))
data=[roll,name,marks]
pickle.dump(data,file)
y=input('Do you want to enter data again:')
file.close()
Q13: Write python program to Search record in a binary file.
Ans:
def search():
file=open('demo.txt','rb')
roll=int(input('Enter roll no.:'))
try:
while True:
i=pickle.load(file)
if i[0]==roll:
print(i)
break
except EOFError:
print()
file.close()
Q14. Write a function Push() which takes "name" as argument and add in a stack
named "MyStack". After calling push() three times, a message should be
displayed "Stack is Full"
st=[ ]
StackSize=3
def push():
y="y"
while y=="y":
sn=input("Enter name of student:")
if len(st)<StackSize:
st.append(sn)
else:
print("Stack is full!")
print(st)
break
y=input("Do you want to enter more name(y/n):")
Output:-
Q15: Julie has created a dictionary containing names and marks as key value
pairs of 6 students. Write a program, with separate user defined functions to
perform the following operations:
Push the keys (name of the student) of the dictionary into a stack, where
the corresponding value (marks) is greater than 75.
Pop and display the content of the stack.
R={"OM":76, "JAI":45, "BOB":89,"ALI":65, "ANU":90, "TOM":82}
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
ST=[]
for k in R:
if R[k]>75:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
Output:
Part B
SQL Queries
Answer the following questions according to given tables:
T_id Name Age Department Doj Salary Gender Adhar_no
1 Jugal gupta 34 CS 2017-01-10 12000 M 987655
2 Sharmilla 31 History 2008-03-24 20000 F 998877
3 Sandeep 32 Maths Null 30000 M 887766
4 Sangeeta 35 History 2015-07-01 40000 F 776655
5 Rakesh gupta 42 Maths 2007-09-05 25000 M Null
6 Shyam singh 50 History Null 30000 M 554433
7 Shiv om 44 CS 2017-02-25 21000 M 443322
8 Shalakha 33 Maths 2018-07-31 20000 F 332211
9 Shyam Kumar 35 Maths 2016-09-24 40000 M 221100
10 Moahn kumar 30 Maths 2016-09-24 25000 M 110022
Table: Teacher
P_id Department Place
1 History Agra
2 Maths Raipur
3 CS Delhi
Table: posting, P_id
Set1: ALTER table to add new attributes / modify data type / drop attribute
1) To Add new column “City” in teacher table.
Ans: alter table teacher add city varchar(20);
2) To rename column “City” to “T_City” in teacher table.
Ans: alter table teacher change city T_city varchar(30);
3) To delete “City” column from teacher table.
Ans: alter table teacher drop T_city;
4) To delete primary key constraint from from T_id column.
Ans: alter table teacher drop primary key;
5) To add primary key constraint to T_id column.
Ans: alter table teacher add primary key(t_id);
Set2: Conditions Based on Pattern – LIKE operators
6) To display name, age and salary of those teacher who’s name start with ‘S’.
Ans: select name,age,salary from teacher where name like "s%";
7) To display name, age and salary of those teacher who’s name ends with ‘a’.
Ans: select name,age,salary from teacher where name like "%a";
8) To display name, age and salary of those teacher who’s name contains letter ‘a’ at the second place.
Ans: select name,age,salary from teacher where name like "_a%";
9) To display name, age and salary of those teacher who’s name not having 7 character.
Ans: select name,age,salary from teacher where name like "_ _ _ _ _ _ _ ";
10) To display the number of all teacher whose name ends with ‘Kumar’.
Ans: select name,age,salary from teacher where name like "%kumar";
Set3: Insert into and Update Queries
11) Write query to add new data in T_id, name, department, salary column in ‘teacher’ table.
Ans: insert into teacher (t_id, name, department, salary) values (12, 'Arun kumar', 'CS',25000);
12) Write query to change department of all male teacher to ‘History’.
Ans: update teacher set department="History" where gender="M";
13) Query to increase salary of all Maths teacher by 5000.
Ans: update teacher set salary=salary+5000 where department="Maths";
14) Query to delete record of those teacher whose salary is NULL.
Ans: delete from teacher where salary is NULL;
15) Query to delete all record at once free the memory of table.
Ans: truncate table teacher;
Set4: ORDER By to display data in ascending / descending order
16) To display all record teacher table name-wise.
Ans: select * from teacher order by name;
17) To display all record of teacher table in decreasing order age-wise.
Ans: select * from teacher order by name DESC;
18) To display sum of salary department wise in teacher table.
Ans: select department,count(salary) from teacher group by department;
19) To display maximum salary gender wise or According to gender.
Ans: select gender,max(salary) from teacher group by gender;
20) To display minimum, maximum, sum, average of salary department wise if number of teacher in department in
more than 2.
Ans: select department,min(salary),max(salary),sum(salary),avg(salary) from teacher group by department;
Set5: Queries based on joining of two tables
21) Write query to join teacher and posting table on the basic of department columns in both table.
Ans: select * from teacher natural join posting;
22) Write query to display name and place of all teacher belongs to delhi.
Ans: select name,place from teacher natural join posting where place='delhi';
23) Write query to display number of teachers city-wise.
Ans: select place,count(place) from teacher natural join posting group by place;
24) Write query to display number of teachers city-wise. Order by city name.
Ans: select place,count(place) from teacher natural join posting group by place order by place;
25) Write query to display number of teachers city-wise if in each city having more than 3 teachers order by city name.
Ans: select place,count(place) from teacher natural join posting group by place having count(place)>3;
Part C
Programs Based
on Python-SQL
Connectivity
Q1: Write a program to Creating database and Show database in Mysql through
Python.
Code:
import mysql.connector
mydb=mysql.connector.connect
(host="localhost",
user="root",
password="123456")
mycursor=mydb.cursor()
mycursor.execute("create database demo")
mycursor.execute("Show databases")
for i in mycursor:
print(i)
Q2: Write a program to Creating table inside any school database through
Python-Mysql connectivity program.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
mycursor.execute("create table std_details3\
(rno int primary key, name varchar(20), age int, city varchar(20))")
std=mycursor.execute("desc std_details3")
for i in mycursor:
print(i)
Q3: Write a program to Display all male teachers data from ‘teacher’ table using
fetchall() Through Python-Mysql connectivity program.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
mycursor.execute("select name,department from teacher where gender='m'")
myrecords=mycursor.fetchall()
for i in myrecords:
print(i)
print("Total number of rows retrieved",mycursor.rowcount)
Q4: Write a Parameterized program to insert data in std_details3 table Through
Python-Mysql connectivity program.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
y="y"
while y=="y":
r=int(input("Enter rollno:"))
n=input("Enter name:")
a=int(input("Enter age:"))
c=input("Enter City:")
mycursor.execute("insert into std_details3\
values({},'{}',{},'{}')".format(r,n,a,c))
mydb.commit()
print(mycursor.rowcount,"Record Inserted")
y=input("Do you want to insert more data:")
Thanks