Professional Documents
Culture Documents
CS Final Project - 231024 - 183926
CS Final Project - 231024 - 183926
COMPUTER
SCIENCE
JOURNAL
Name-Nidhi Salian
Class- XII S2
Academic year-2023-24
Name-Nidhi Salian Academic year- Computer Science
Class-XII-S2 2023-2024 Journal
SQL Queries
1. Count & display the total number of Managers in the STAFF table.
2. Display Name, StNo, and BS of all Managers who get salary more than Rs 40000
7. Display details of Staff whose name has a ‘y’ in the third place.
9. Display Name, StNo, BS, Commission (Comm), Net Salary (NS) (BS + Comm), department
and city of each salesman, where Comm is calculated as 10% of BS & NS is Basic + Comm
10. Display details of salesmen with highest allowance (which is 10% of BS) from the STAFF
table.
12. Display details of those employees and their department who work in dept no. ’d02’
13. Display details of those employees who get a salary greater than equal to 50000 or less than
equals to 100000
15. Display the average amount of the salary received by the salesmen & the Managers.
16. Display average salary for all those employees who stay in Delhi.
17. Display all the designation listed in the staff table and the number of employees working
under that designation. List only those records where the number of employees working is
less than 2.
21. Write a query to display details of all staff with staff names and designation listed in capital
letters.
22. Display details of all staff members in ascending order of their names.
24. Show designation wise average and sum of salary of staff members listed in ascending order
of their designation.
25. Delete details of all staff members who are not ‘salesman’
27. Modify the age column in staff table to make it float type.
#Q1
def factorial(n): #Factorial of n
p=1
for i in range(1,n+1):
p=p*i
print('Factorial of',n,'=',p)
def fibonacci(N): #Fibonacci series upto N
s=0
f,t=1,1
while(t<=N):
print(t, end=' ')
f=s
s=t
t=f+s
print()
while(1):
print('\t','Choose a Number','\t')
print('1','\t','To find factorial of a given number')
print('2','\t','Print Fibonacci series upto N')
print('3','\t','Exit')
no=int(input("Choose a Number: "))
if no==1:
number=int(input("Enter a number: "))
factorial(number)
Output:
ccount=0
V=['A','a','E','e','I','i','O','o','U','u']
C='bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
for i in s:
if i in V:
vcount+=1
elif i in C:
ccount+=1
else:
if vcount==0 and ccount==0:
print("No vowels or consonants found in",s)
else:
print("Total no of vowels in",s,"=",vcount)
print("Total no of consonants in",s,"=",ccount)
rs=s[::-1]
if s==rs:
print(s,'is a palindrome')
else:
while(1):
print('\t','Choose a Number','\t')
print('1','\t','To find the vowels and consonants in a given string.')
print('2','\t','Check if a given String is Palindrome or not.')
print('3','\t','To count the total no. of words in the given string.')
print('4','\t','Exit')
no=int(input("Choose a Number: "))
if no==1:
st=input("Enter a string: ")
vowcon(st)
elif no==2:
words(st)
elif no==4:
print("You choose to exit")
break
else:
print("Invalid Choice")
print("Try again")
Code:
#Q3
def substring_hunting(sub): #To find number of occurrences of substring
lst=s.split()
count=0
for i in range(len(lst)):
if sub==lst[i]:
count+=1
else:
if count==0:
print(sub,'is not in string','"',s,'"')
else:
print("No. of occurences of",sub,'=',count)
if len(lst[i])>max:
max=len(lst[i])
pos=i
while(1):
print('\t','Choose a Number','\t')
print('1','\t','To print the number of occurrences of a sub string in an accepted string.')
print('2','\t','To print an accepted string in reverse order.')
print('3','\t','To find the longest word & print its position.')
print('4','\t','Exit')
no=int(input("Choose a Number: "))
if no==1:
longword(st)
elif no==4:
print("You choose to exit")
break
else:
print("Invalid Choice")
print("Try again")
return l
def position(no): #To tell the position of the element
for i in range(len(l)):
if no==l[i]:
pos=i
print(no,'is at position',i,'in',l)
break
else:
print(no,'not in list')
def acc_del(no): #To delete an element
for i in range(len(l)):
if no==l[i]:
l.remove(no)
print('New list: ',l)
break
else:
print(no,'not in list')
position(n)
elif no==3:
n=int(input("Enter a number: "))
acc_del(n)
elif no==4:
print("You choose to exit")
break
else:
print("Invalid Choice")
print("Try again")
Output:
iii. To find sum & average of all elements in the Tuple (without using any in-built function)
iv. Exit
Code:
#Q5
def tup(): #Create a tuple
ans='y'
l=[]
while ans=='y' or ans=='Y':
mn=t[0]
for i in range(len(t)):
if t[i]>mx:
mx=t[i]
if t[i]<mn:
mn=t[i]
print('The highest number in tuple: ',mx)
print('The lowest number in tuple: ',mn)
max_min()
elif no==3:
sum_avg()
elif no==4:
print("Try again")
4. Exit
Code:
#Q6
while(1):
print('\t','Choose a Number','\t')
print('1','\t','To create a dictionary by accepting Name and marks of N students.')
print('2','\t','Display the contents of the dictionary.')
print('3','\t','To check whether a given name exists in the dictionary')
print('4','\t','Exit')
no=int(input("Choose a Number: "))
if no==1: #Creating a dictionary
d={}
found=0
N=int(input("Enter no. of students: "))
for i in range(N):
name=input("Enter name of student: ")
print(f'{key}\t{d[key]}')
else:
print("No item in dictionary")
elif no==3: #Check if name exists in dictionary
print("Invalid Choice")
print("Try again")
Output:
Case 1
Code:
#Q7
fh=open('exprsn .txt','w') #write in file
fh.writelines('life is an adventure, like a roller coaster. That explains the ups and\n')
fh.writelines('downs in life. Often, when we think we are at the end of something, we are\n')
fh.writelines('actually at the beginning of something else.')
print("Content written in file")
fh.close()
Output:
fh.close()
c='BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz'
s=' '
UC,LC,VC,CC=0,0,0,0
while s:
s=file.read(1)
if s.isupper():
UC+=1
elif s.islower():
LC+=1
if s in v:
VC+=1
elif s in c:
CC+=1
print(f'Total number of uppercase letters = {UC}')
print(f'Total number of lowercase letters = {LC}')
print(f'Total number of vowels = {VC}')
print(f'Total number of consonants = {CC}')
file.close()
isc,itc=0,0
while s:
s=fh.readline()
s=s.split()
for i in range(len(s)):
if 'is'==s[i]:
isc+=1
if 'it'==s[i]:
itc+=1
fh.close()
print("Number of 'is' in text =",isc)
print("Number of 'it' in text =",itc)
Output:
#Q10
import pickle
def write(): #Write in a binary file
d={}
file=open("recored.dat",'wb')
ans='y'
while ans=='Y' or ans=='y':
name=input("Enter name of student: ")
rno=int(input("Enter roll no. of student: "))
d[rno]=name
ans=input('Do you want to continue Y/N: ')
pickle.dump(d,file)
print('Roll no','\t','Name')
for k in rec:
print(f'{k}\t{rec[k]}')
except EOFError:
pass
fh.close()
try:
while(1):
rec=pickle.load(fh)
print('Roll no','\t','Name')
for k in rec:
if k==r:
print(f'{k}\t{rec[k]}')
break
else:
print(r,'not in record')
except EOFError:
pass
fh.close()
while(1):
print('\t','Choose a Number','\t')
print_rec()
elif no==3:
search()
elif no==4:
Output:
#Q11
import pickle
fh=open('result.dat','wb')
for i in range(4):
l=[]
rno=int(input("Enter roll number of student: "))
name=input("Enter name of student: ")
mks=int(input("Enter marks of student: "))
rec=[rno,name,mks]
l.extend(rec)
pickle.dump(l,fh)
print("Record written")
print()
fh.close()
file=open('result.dat','rb+')
rec[2]=nmks
file.seek(pos)
pickle.dump(rec,file)
file.seek(pos)
print(pickle.load(file))
found=True
else:
print('Updated')
file.close()
Output:
iii. Search for a record with a given Employee number and display the Details.
iv. Display record of the employee getting the highest salary.
v. Exit
Code:
#Q12
import csv
def write(): #Write csv file
fh=open('Employee.csv','w',newline='')
writer=csv.writer(fh)
n=int(input("Enter number of employee: "))
for i in range (n):
l=[]
l.extend(rec)
writer.writerow(l)
fh.close()
print('Record written in file')
print('Name:','\t',row[0])
print('Code:','\t',row[1])
print('Salary:','\t',row[2])
break
else:
print('No record found')
fh.close()
def maxsal_emp(): #Employee with highest salary
fh=open('Employee.csv','r')
reader=csv.reader(fh)
mx=0
for row in reader:
if int(row[2])>mx:
mx=int(row[2])
nm=row[0]
cd=row[1]
print('Name:','\t',nm)
print('Code:','\t',cd)
print('Salary:','\t',mx)
fh.close()
while(1):
print('\t','Choose a Number','\t')
print('1','\t','To create a CSV file named Employee.csv')
print('2','\t','To print all the records')
if no==1:
write()
search()
elif no==4:
maxsal_emp()
elif no==5:
print("You choose to exit")
break
else:
print("Invalid Choice\nTry again")
Output:
#Q13
import csv
fh=open('student.csv','w',newline='') #Write in file
writer=csv.writer(fh)
reader=csv.reader(file)
found=0
print('Rno','\t','Name','\t','Marks')
for row in reader:
if int(row[2])>80:
print(f'{row[0]}\t{row[1]}\t{row[2]}')
found=1
if found==0:
def pop():
if stack==[]:
print('Stack is empty')
else:
elt=stack.pop()
print('Element removed: ',elt)
print('Stack:',stack)
def peek():
if stack==[]:
print('Stack is empty')
else:
top=stack[len(stack)-1]
print("Topmost element=",top)
def display():
if stack==[]:
print('Stack is empty')
else:
while(1):
print('\t','Choose a Number','\t')
print('1','\t','To push elements in the stack')
print('2','\t','To remove an element from the stack')
print('3','\t','To display its topmost element')
print('4','\t','To Dispaly all the elements')
print('5','\t','Exit')
pop()
elif no==3:
peek()
elif no==4:
display()
elif no==5:
print("You choose to exit")
break
else:
print("Invalid choice\t Try again")
Output:
4. Exit
Code:
stack=[]
def push():
sid=int(input("Enter student ID "))
snm=input("Enter Student Name: ")
mks=int(input("Enter marks: "))
stack.extend([sid,snm,mks])
print("New stack is",stack)
def pop():
if stack==[]:
print('Stack is empty')
else:
elt=stack.pop()
print('Element removed: ',elt)
print('Stack:',stack)
def display():
if stack==[]:
print('Stack is empty')
else:
print('Elements in stack are: ')
for i in range(len(stack)-1,-1,-1):
print(stack[i])
while(1):
print('\t','Choose a Number','\t')
Output:
def pop():
if stack=={}:
print('Stack is empty')
else:
lst=list(stack.keys())
elt=stack.pop(lst[-1])
print('Stack:',stack)
def display():
if stack=={}:
print("Stack is empty")
else:
print('Elements in stack are: ')
lst=list(stack.keys())
elif no==3:
display()
elif no==4:
print("You choose to exit")
break
else:
print("Invalid choice\nTry again")
Output:
connect=sq.connect(host='localhost',user='root',database='shop',passwd='NimaLi0611')
if connect.is_connected():
print('Connected')
else:
data=cur.fetchall()
for row in data:
print(row[0],'\t',row[1],'\t',row[2],'\t',row[3])
elif no==2:
cur.execute('select * from stationary where Price between 8 and 15')
data=cur.fetchall()
for row in data:
print(f'{row[0]}\t{row[1]}\t{row[2]}\t{row[3]}')
elif no==3:
rec='select ConsumerName,Address,Company,Price,C.S_ID from Consumer C,Stationary S where
S.S_ID=C.S_ID'
cur.execute(rec)
data=cur.fetchall()
for row in data:
print(f'{row[0]}\t{row[1]}\t{row[2]}\t{row[3]}\t{row[4]}')
elif no==4:
rec='select Company,max(Price),min(Price) from stationary group by Company'
cur.execute(rec)
data=cur.fetchall()
for row in data:
print(f'{row[0]}\t{row[1]}\t{row[2]}')
elif no==5:
print('You choose to exit')
break
Output:
Code:
import mysql.connector as sq
connect=sq.connect(host='localhost',user='root',database='student',passwd='NimaLi0611')
if connect.is_connected():
print('Connected')
else:
print('Error while connection')
cur=connect.cursor()
while(1):
print('\tChoose a Number\t')
print(f'1\tAdd a new attributes - Sec char(3) to the table')
print(f'2\tUPDATE table to add Sec as A for all class 3 students')
print(f'3\tORDER By to display data in descending order')
print(f'4\tGROUP BY and find the min, max, sum, count and average mark')
print(f'5\tDELETE to remove tuple(s) belonging to class 4')
print(f'6\texit')
print('Added\n')
if n==2:
cur.execute('Update student set Sec="A" where Class="Three"')
connect.commit()
data=cur.fetchall()
print('ID','\t','Stu Name','\t','Class','\t','Mark','\t','Sex','\t','Sec')
for row in data:
print(row[0],'\t',row[1],'\t',row[2],'\t',row[3],'\t',row[4],'\t',row[5])
print('\n')
if n==4:
cur.execute('Select Class,min(Mark),max(Mark),sum(Mark),Count(Mark),avg(Mark) from student
group by Class')
data=cur.fetchall()
print('Class','\t','Min','\t','Max','\t','Sum','\t','Count','\t','Average')
Output:
(iii) To display the Name of the different cities listed in Table Customers
(iv) Display details of all customers with their name stating with ‘M’
Code:
import mysql.connector as sq
connect=sq.connect(host='localhost',user='root',database='SUPERMARKET',passwd='NimaLi0611')
if connect.is_connected():
print('Connected')
else:
print('Error while connection')
cur=connect.cursor()
while(1):
print('\tChoose a Number\t')
print(f'5\texit')
n=int(input("Choose a Number: "))
if n==1:
cur.execute('Select * from Items where ItemName="Milk"')
data=cur.fetchall()
print('I_ID','\t','I_Name','\t','Supplier','\t','Rate')
elif n==2:
cur.execute('Select * from Items where Supplier="AMUL"')
data=cur.fetchall()
print('I_ID','\t','I_Name','\t','Supplier','\t','Rate')
for row in data:
print(row[0],'\t',row[1],'\t',row[2],'\t',row[3])
print('\n')
elif n==3:
print(row[0])
print('\n')
elif n==4:
cur.execute('Select * from customers where CustomerName like "M%"')
data=cur.fetchall()
print('C_ID','\t','CustomerName','\t','Address','\t','I_ID')
for row in data:
print(row[0],'\t',row[1],'\t',row[2],'\t',row[3])
print('\n')
elif n==5:
print('You choose to exit')
break
Output:
Using Python MySQL Connector, execute the following SQL commands on the above patient table
i. Insert a new patient record with details accepted from the user
ii. Change ‘Hypo Thyroid’ to ‘Hyper Thyroid’ for Dharam Pal
iii. Display all records with descending ORDER of their Bill amount
if connect.is_connected():
print('Connected')
else:
print('Error while connection')
cur=connect.cursor()
cur.execute('create table if not exists Patients(P_ID char(3) not null primary key, P_Name char(30)\
not null, P_History char(20) not null, Bill_Amt int)')
cur.execute('insert into patients(P_ID,P_Name,P_History,Bill_Amt) values ("{}","{}","{}",{})'\
.format('P01','Dilip Singh','Diabetes',52000))
cur.execute('insert into patients(P_ID,P_Name,P_History,Bill_Amt) values ("{}","{}","{}",{})'\
.format('L02','Vidya Ahuja','High BP',47000))
cur.execute('insert into patients(P_ID,P_Name,P_History,Bill_Amt) values ("{}","{}","{}",{})'\
.format('T05','Dharam Pal','Hypo Thyroid',7500))
cur.execute('insert into patients(P_ID,P_Name,P_History,Bill_Amt) values ("{}","{}","{}",{})'\
.format('F03','Ved Prakash','Diabetes',25000))
cur.execute('insert into patients(P_ID,P_Name,P_History,Bill_Amt) values ("{}","{}","{}",{})'\
.format('M04','Adiba Patel','Tonsils',9000))
connect.commit()
while(1):
print('\tChoose a Number\t')
print(f'1\tInsert a new patient record with details accepted from the user')
print(f'2\tChange ‘Hypo Thyroid’ to ‘Hyper Thyroid’ for Dharam Pal')
elif n==2:
cur.execute('Update patients set P_History="Hyper Thyroid" where P_Name="Dharam Pal"')
connect.commit()
print('Updated\n')
elif n==3:
cur.execute('Select * from patients order by Bill_Amt desc')
data=cur.fetchall()
for row in data:
print(row[0],'\t',row[1],'\t',row[2],'\t',row[3])
print('\n')
elif n==4:
cur.execute('Delete from patients where P_History="Tonsils"')
connect.commit()
print('Deleted\n')
elif n==5:
print('You Choose to exit')
break
Output: