Untitled Document
Untitled Document
Kalyanpur
Session 2024-2025
Computer Science Practical File
Submitted To: Submitted By:
Mr. Govind Singh Rathore Name- SUDARSHAN SINHA
(PGT Computer) Class- XII B
CERTIFICATE
This is to certify that SUDARSHAN SINHA of class XII B of
GARDENIA PUBLIC SCHOOL has done his Practical on Python
program and Mysql queries under my supervision. He 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,Mr. Govind Singh Rathore who taught and undertook the
responsibility of teaching the subject computer science. I have
greatly benefited from his classes.
My sincere thanks goes to our Principal Mrs. AVNEET KAUR who
has always been a source of encouragement and support and
without whose inspiration, this project would not have been
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-SUDARSHAN SINHA
Class-XII B
Part A
Python
programs
Q1: Write a program to covert the time inputted in minutes into hours and remaining
minutes.
Ans:
time=int(input("Enter the time in minutes:"))
hour=time//60
mins=time % 60
print("Hours are:",hour)
print("Minutes are:",mins)
Q2: 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")
Q3: 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:"))
n=int(input("Enter a number:"))
i=1
while i<=10:
print(n,"X",i,"=",n*i)
i=i+1
Q5: 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")
Q6: 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")
Q7: 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)
Q8: Write a program to enter a three digit number and check if it is a Armstrong
number or not.
Ans:
num=int(input("Enter three digit number:"))
temp=num
sum=0
while temp>0:
digit=temp%10
temp=temp//10
sum=sum+digit**3
if num==sum:
print(num,"is an armstrong number")
else:
print(num,"is not an armstrong number")
Q9: Write a program to enter number and check if it is a perfect number or not.
Ans:
num=int(input("Enter any number:"))
sum=0
i=1
while i<num:
if num%i==0:
sum=sum+i
i=i+1
if num==sum:
print(num," is a perfect number")
else:
print(num," is not a perfect number")
Q10. Write a function in Python that counts the number of “Me” or “My” (in smaller
case also) words present in a text file “STORY.TXT”. If the “STORY.TXT” contents are as
follows:
My first book was Me and My Family. It gave me chance to be Known to the world.
The output of the function should be: Count of Me/My in file: 4
Ans:
def countmemy():
f=open("story.txt","r")
d=f.read()
m=d.split()
c=0
for i in m:
if i=="me" or i=="my" or i=="Me" or i=="My":
c=c+1
print("Count of me or my in file",c)
countmemy()
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.
def pop():
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())
Output:-
Q 14: Write a function push(l) to perform push opration in stack. Where a list is given:
L=[10,12,15,16,17,18,20,21,25]
Push only those element who is divisible by 5.
def push(l):
L2.append(l)
L2=[]
L1=[10,12,15,16,17,18,20,21,25]
for i in L1:
if i%5==0:
push(i)
print("Original List:\n",L1)
print("List after using push:\n",L2)
Output:-
Q15. 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:-
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
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);
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);
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;
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';
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