Raghav Pract..
Raghav Pract..
Internal Examiner
External Examiner
ACKNOWLEDGEMENT
It is with pleasure that I acknowledge my sincere gratitude
to our teacher, Mr. Govind Rathore who taught and
undertook the responsibility of teaching the subject
computer science. I have been 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 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.
Raghav Kumar
Class XII
Part A
Python programs
Q1: 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])
Q2: 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)
Q3: Write a program to compute GCD or HCF of two integers.
Ans:
n1=int(input("Enter first number:"))
n2=int(input("Enter second number:"))
if n1<n2:
smaller=n1
else:
smaller=n2
i=1
while i<=smaller:
if n1%i==0 and n2%i==0:
gcd=i
i=i+1
print("GCD of ",n1, "and",n2,"is ",gcd)
Q4: 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")
Q5: 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)
Q6: 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")
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: Write a python code to check the frequency or number of occurrences of
input element/number in any list/or given list.
List1= [10, 20, 30,40,10,50,10]
Ans:
List1= [10, 20, 30,40,10,50,10]
num=int(input("Enter element to count:"))
count=0
for i in List1:
if i==num:
count=count+1
print(num," occurs ",count, " times")
Q10: 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()
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 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:-
Q14: 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.
ST=[]
for k in R:
if R[k]>75:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
Output:
Q15: Write a function push() and pop() to add a new student name and remove a
student name from a list student, considering them to act as PUSH and POP
operations of stack Data Structure in Python.
st=[ ]
def push():
y="y"
while y=="y":
sn=input("Enter name of student:")
st.append(sn)
y=input("Do you want to enter more name(y/n):")
def pop():
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())
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;
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";
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;
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:")
THANK
YOU