Comp.science
Comp.science
Computer Science
(Python& MySQL)
(2024-25)
Created by
Name: Aryan Kamboj
Class XII B
Roll No: __________
#PROGRAM 14: Write a program to show push and pop operation using
stack.
#PROGRAM 15 Write a python code to add student using MySQL
connectivity and show result to the examiner. Create database and table
as below:
Name of database =school
Name of table = student (roll, name, age, class, city) give primary key
and not null constraints
#Program 1: WAP to display frequencies of all the element of a list.
L=[5,15,21,5,21,8,15,6]
L1=[]
L2=[]
fori in L:
ifi not in L2:
x=L.count(i)
L1.append(x)
L2.append(i)
print('element','\t\t\t',"frequency")
fori in range (len(L1)):
print(L2[i],'\t\t\t',L1[i])
#Program 2: Write a program to accept values from a user and create a
tuple.
t=tuple()
n=int(input("enter limit:"))
fori in range(n):
a=input("enter number:")
t=t+(a,)
print("output is")
print(t)
#Program 3: WAP to input name of a student and class and roll no, it in a
dictionary and display in tabular form also search and display for a
particular name.
d={}
i=1
n=int(input("enter number of entries"))
whilei<=n:
name=input("enter name of student")
cls=int(input("enter class"))
rollno=int(input("enter roll no"))
d[name]= [cls , rollno]
i=i+1
l=d.keys()
print("\n name\t\t","class\t\t","rollno")
fori in l:
k=d[i]
print(i,'\t\t',end=" ")
for j in k:
print(j,'\t\t',end='\t\t')
x=input("\nenter name to be searched:\ ")
fori in l:
ifi==x:
print("\n name\t\t","class\t\t","rollno")
k=d[i]
print(i,'\t\t',end=" ")
for j in k:
print(j,'\t\t',end="\t")
break
#Program4 WAP to print Fibonacci Series up to n terms, also find sum of
series
a,b=0,1
c=a+b
print(a,b,end=" ")
print(a+b,end=" ")
a,b = b,a+b
c=c+b
print()
import math
def fact(n):
if n<2:
return 1
else :
return n*fact(n-1)
ans=math.factorial(x)
importdatetime
M1 = 0
months = range(1,13)
for year in range(2021, 2022):
for month in months:
ifdatetime(year, month,1).weekday() == 0:
M1 += 1
print('Monday','/t/t/t','frequency ', M1)
#Program 7: Write a function Div() which takes a 10 elements numeric
tuple and return the sum of elements which are divisible by 3 & 5
def div(n):
s=0
fori in n:
if i%3==0 and i%5==0:
s=s+i
return s
l=[]
fori in range(10):
print("enter the ", i + 1,"th number of the tuple ",end = '',sep
="")
t=int(input("enter"))
l.append(t)
n=tuple(l)
print("tuple=",n)
print("sum of number in tuple divisible by 3 &5",div(n))
#Program 8: WAP to read data from a text file DATA.TXT, and display
word which have maximum/minimum characters.
f1=open("Pbx.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,", ",len(words))
maxC=len(words[0])
minC=len(words[0])
minfinal=""
maxfinal=""
for word in words[1:]:
length=len(word)
ifmaxC<length:
maxC=length
maxfinal=word
ifminC>length:
minC=length
minfinal=word
print("Max word : ",maxfinal,", maxC: ",maxC)
print("Min word : ",minfinal,", minC: ",minC)
#Program 9: WAP to read a file And Print the Contents of file along with
numbers of vowels and frequency of word ‘the’ in it
f = open( "str.txt","r")
b=f.read()
word1=0
ctr=0
v=['a','e','i','o','u']
print("Contents of file:")
print(b)
for i in b:
ctr = ctr +1
f.close()
print()
str = f1.read()
word=str.split()
import pickle
str="this is business i'mdoin business "
a=open('Pbx.dat','wb')
pickle.dump(str,a)
a.close()
a=open('comp.dat','rb')
str=pickle.load(a)
print("\n\nthe string in the binary file is : \n",str)
dl={}
for x in str:
if x not in dl:
dl[x]=1
else:
dl[x]=dl[x]+1
print("\nThe count of each letter of string is :\n", dl)
a.close()
#Program 11: Write a function to input a list and arrange in ascending
order using BUBBLE SORT.
for j in range(len(l)-1) :
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print("arranged list:",l)
#Program 12: Write a program that rotates the elements of a list so that the
element at the first index moves to the second index, the element in the
second index moves to the third
index, etc., and the element in the last index moves to the first index.
defselect_errors(STL):
newlist=[]
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
surname = name_surname[1]
if name[0].islower() or surname[0].islower():
newlist.append(record)
returnnewlist
defselect_correct(STL):
newlist = []
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
surname = name_surname[1]
if not name[0].islower() and not surname[0].islower():
newlist.append(record)
returnnewlist
defcorrect_entries(STL):
newlist = []
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
surname =name_surname[1]
newlist.append(name.capitalize()+''+
surname.capitalize())
returnnewlist
#__main__
STL = []
ch = 0
while (ch != 4):
print("\t----")
print("\tMENU")
print("\t----")
print("1.Apply for the School Post")
print("2.List of all applicants")
print("3.Correct the Incorrect Enteries")
print("4.Exit")
ch = int(input("Enter your choice (1-4):"))
ifch == 1:
name = input("Enter your name :")
STL.append(name)
elifch == 2:
print("\tStudents applied so far:")
print(STL)
elifch == 3:
ok_enteries = select_correct(STL)
error_enteries = select_errors(STL)
corrected_enterie = correct_enteries(STL)
print("Correctly entered names:", ok_enteries)
print("Incorrectly entered names;",error_enteries)
print("corrected names:", corrected_enteries)
elifch !=4:
print("valid choices are1..4:")
else:
print("THANK YOU")
#PROGRAM 14:Write a program to show push and pop operation using
stack.
#stack.py
def push(stack,x): #function to add element at the end of
list
stack.append(x)
def pop(stack): #function to remove last element from list
n = len(stack)
if(n<=0):
print("Stack empty....Pop not possible")
else:
stack.pop()
def display(stack): #function to display stack entry
iflen(stack)<=0:
print("Stack empty...........Nothing to display")
fori in stack:
print(i,end=" ")
#main program starts from here
x=[]
choice=0
while (choice!=4):
print("********Stack Menu***********")
print("1. push(INSERT)")
print("2. pop(DELETE)")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice :"))
if(choice==1):
value = int(input("Enter value "))
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this program")
#PROGRAM 15 Write a python code to add student using MySQL
connectivity and show result to the examiner. Create database and table as
below:
Name of database =school
Name of table = student (roll, name, age, class, city) give primary key
and not null constraints
import mysql.connector
from mysql.connector import Error
def insert_rec():
try:
connection =
mysql.connector.connect(host='127.0.0.1',database='school',u
ser='root',passwd='123456')
print('success')
cursor=connection.cursor()
cursor.execute('use school')
r=int(input('Enter Rno'))
n=input('Enter Name to be added in table')
a =int(input('Enter Age'))
c=int(input('Enter Class'))
ci = input('Enter City')
connection.commit()
except Error as e:
print('error in connection ',e)
# call it
insert_rec()
SQL PRACTICAL FILE
Q1. Consider the following table named ACCESSORIES and answer the
following questions based on it.
Field name Type Width Constraint
Price Integer 10
Type Varchar 10
Qty Integer 5
(d) T
o
(f) Displ
a
y
SenderName for every Recipient who resides in Mumbai.