Professional Documents
Culture Documents
Shreyash - Tiwari 2022-23 Report - File
Shreyash - Tiwari 2022-23 Report - File
Shreyash - Tiwari 2022-23 Report - File
COMPUTER SCIENCE
REPORT FILE
REPORT FILE
Subject teacher:
Mrs. Santwana Srivastava
External Examiner:
_______________________
Acknowledgement
It would be my utmost pleasure to express my
gratitude and sincere thanks to my principal Dr
Jawaid Alam Khan who helped me a lot.
I’m highly indebted to my subject teacher Mrs.
Santwana Srivastava for her guidance and constant
supervision as well as for providing necessary
information regarding this file and also for her help
in completing this Report file.
-Shreyash Tiwari
LIST OF PROGRAMS
1. Write a Python function to find maximum number among given 4 numbers using
list.
2. Write a function to Input any number from user and calculate its factorial.
3. Write a Python function that takes a number as a parameter and check the
number is prime or not.
4. Write a Python function that checks whether a passed string is palindrome or not.
5. Write a function that computes the sum of number 1…...n, get the value of last
number n from the user.
6. Write a function to search an element from a list of integer numbers using binary
search technique.
7. Write a function to search an element from a list of integer numbers using serial
search technique.
8. Write a menu-driven python program using different functions for the following
menu:
1 Check no. is Palindrome or not
2 Check no. is Armstrong or not
3 Exit
9. Write a Program to count the number of words from a text file.
10. Write a program to read content of a text file and display total number of vowels,
consonants, lowercase and uppercase characters
11. Write a program to take details of book from the user and write record in text file.
12. Write a python code to find the size of the file in bytes, number of lines and
number of words.
13. Write a program to read contents of file line by line and display each word
separated by symbol #
14. Write a user-defined function in Python that displays the number of lines starting
with the letter ‘H’ in the file student.txt, if the file contains.
15. Write a program to create a binary file to store Roll no and name, also search for
Roll no and display record if found otherwise display a message "Roll no.
not found"
16. Write a program to perform read and write operation with student .csv file.
17. Write a menu based program to perform operations on stack / implement a stack
using list.
18. WAP to create a stack called Student to perform basic operations on stack using
list. The list contains two data values roll number and name of the student.
Write the following functions:
22. Write a program to connect Python with MySQL using database connectivity
and perform following operations on data in the database:
a) Create a table
b) insert the data
c) Fetch
d) Update
e) delete the data
def find_max():
l=[ ]
max1=0
for i in range(4):
n=int(input("Enter number into list:"))
l.append(n)
print("The list is:",l)
for i in l:
if i>max1:
max1=i
print("Max:",max1)
#main
find_max()
OUTPUT
PROGRAM- 2
def factorial(num):
fact = 1
n = num # storing num in n for printing
while num>1: # loop to iterate from num to 2
fact = fact * num
num-=1
#main
num = int(input("Enter any number :"))
factorial(num)
OUTPUT
PROGRAM- 3
# Python function that takes a number as a parameter and check the number is prime
or not.
def test_prime(n):
if (n==1):
return False
elif (n==2):
return True
else:
for x in range(2,n):
if(n % x==0):
return False
return True
# main
print(test_prime(11))
OUTPUT
PROGRAM- 4
left_pos = 0
right_pos = len(string) - 1
return False
left_pos += 1
right_pos -= 1
return True
print(isPalindrome('madam'))
OUTPUT
PROGRAM- 5
def compute(num):
s=0
s=s+i
return(s)
#main
sum=compute(n)
start = 0
end = len(alist)
end = mid-1
start = mid + 1
else:
return mid
return -1
alist=[12,14,16,25,78,89]
print(alist)
if index < 0:
else:
L = [2, 4, 0, 1, 9]
x = int(input("Enter element to be searched"))
n = len(L)
result = linearSearch(L, n, x)
if(result == -1):
print("Element not found")
else:
print("Element found at index: ", result)
OUTPUT
PROGRAM-8
# Menu-driven python program using different functions
# 3 Exit
# Palindrome
def checkPalin(n):
temp=n
rem=0
rev=0
while(n>0):
rem=n%10
rev=rev*10+rem
n=n//10
if(temp==rev):
else:
# Armstrong
def checkArmstrong(n):
temp=n
rem=0
arm=0
while(n>0):
rem=n%10
arm+=rem**3
n=n//10
if(temp==arm):
else:
def menu():
print("3.Exit")
opt=int(input("Enter option:"))
if opt==1:
checkPalin(no)
elif opt==2:
checkArmstrong(no)
elif opt==3:
sys.exit()
else:
print("Invalid option")
# main
menu()
OUTPUT
PROGRAM- 9
fin=open(r"C:\Users\my\Desktop\new.txt",'r')
str=fin.read( )
L=str.split()
count_words=0
for i in L:
count_words=count_words+1
f = open(r"C:\Users\Administrator\Desktop\file1.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1
print("Total Vowels in file:",v)
print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters :",o)
f.close()
file1.txt :
OUTPUT
PROGRAM-11
# Program to take the details of book from the user and
fout =open("D:\\Book.txt",'w')
for i in range(n):
record=title+" , "+str(price)+'\n'
fout.write(record)
fout.close( )
OUTPUT
PROGRAM-12
# Python code to find the size of the file in bytes, number of lines and number of
words.
f=open(r"C:\Users\my\Desktop\project.txt","r")
word=0
ln=0
str=f.read( )
size=len(str)
f.seek(0)
while True:
if not line:
else:
string=line.split()
length=len(string)
word=word+length
f.close( )
File: project.txt
OUTPUT
PROGRAM-13
# Program to read content of file line by line
f = open(r"C:\Users\Administrator\Desktop\w_display.txt")
for line in f:
words = line.split()
for w in words:
print(w+'#',end='')
print()
f.close()
India is my country
We are Indians
I love my country
PROGRAM-14
# A user-defined function in Python that displays the number of lines starting with
the letter ‘H’ in the file student.txt, if the file contains.
def count_H():
data=f.readlines( )
count_H=0
for i in data:
if i[0]=='H':
count_H += 1
#main
count_H( )
File : student.txt
OUTPUT
PROGRAM-15
# Program to create a binary file to store Rollno and name
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
student.append([roll,name])
pickle.dump(student,f)
f.close()
# to read
f=open('student.dat','rb')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
for s in student:
if s[0]==r:
found=True
break
if not found:
f.close()
OUTPUT
PROGRAM-16
# Write a program to perform read and write operation with student.csv file.
import csv
def readcsv():
print(row)
def writecsv( ):
a=int(input())
if a==1:
readcsv()
elif a==2:
writecsv()
else:
print("Invalid value")
OUTPUT
PROGRAM-17
# Menu based program to perform operations on stack / implement a stack using
list.
def isEmpty(stk):
if stk==[]:
return True
else:
return False
def push(stk,item):
stk.append(item)
top=len(stk)-1
def pop(stk):
if isEmpty(stk):
return "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def topvalue(stk):
if isEmpty(stk):
return "underflow"
else:
top=len(stk)-1
return stk[top]
def display(stk):
if isEmpty(stk):
print("stack empty")
else:
top=len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
#main
stack=[]
top=None
while True:
print("STACK OPERATION")
print(" 4. Display")
if ch==1:
push(stack,item)
elif ch==2:
item=pop(stack)
if item=="underflow":
print("Stack is empty")
else:
elif ch==3:
item=topvalue(stack)
if item=="underflow":
print("underflow!stack is empty!")
else:
elif ch==4:
display(stack)
elif ch==5:
break
print("invalid choice")
OUTPUT
PROGRAM-18
""" Program to create a stack called Student to perform basic operation on stack
using list. The list contains two data values roll number and name of student.
following functions:
PUSH()- To add data values into stack
POP()-To remove data values from stack
SHOW()-To display data values from stack"""
stack = [ ]
while True :
print()
print()
if user == "insert" :
stack.append(data)
if stack == [ ]:
print("Under Flow")
else :
print(stack.pop())
else :
break
OUTPUT
PROGRAM-20
# Program to implement a stack for the employee details ( empno, name)
def push():
empno=int(input("Enter the employee number to push:"))
ename=input("Enter the employee name to push:")
stk.append([empno,ename])
global top
top=len(stk)-1
def display():
global top
if top==-1:
print("Stack is Empty")
else:
top=len(stk)-1
print(stk[top],"<-top")
for i in range(top-1,-1,-1):
print(stk[i])
def pop_ele():
global top
if top==-1:
print("Stack is Empty")
else:
print(stk.pop())
top=top-1
#main
stk=[]
top=-1
while True:
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")
ch=int(input("Enter your choice:"))
if ch==1:
push()
print("Element Pushed")
elif ch==2:
pop_ele()
elif ch==3:
display()
elif:
break
else:
print("Invalid Choice")
OUTPUT
Question 21
a) Consider the following table named ‘EMPLOYEES’ and write SQL Queries:
viii. To display the jobs where the number of EMPLOYEESs is less than 3.
Ans:-SELECT JOB,COUNT(*) FROM EMPLOYEES GROUP BY JOB HAVING
COUNT(*) < 3;
ix. To display the difference of highest and lowest salary of each department
having maximum salary more than 2500.
Ans:-SELECT MAX(SAL) - MIN(SAL)'DIFFERENCE' FROM EMPLOYEES
GROUP BY DEPTNO HAVING MAX(SAL) > 2500;
x. To display the records of the table EMPLOYEES as per the ascending
order of salary.
ITEM
-------------------------------------------------------------------------------------------------------------------
I_ID ITEMNAME MANUFACTURER PRICE
------------------------------------------------------------------------------------------------------- -----------------
PC01 PERSONAL COMPUTER ABC 35000
LC05 LAPTOP ABC 55000
PC03 PERSONAL COMPUTER XYZ 32000
PC06 PERSONAL COMPUTER COMP 37000
LC03 LAPTOP PQR 57000
-------------------------------------------------------------------------------------------------------------------
CUSTOMER
------------------------------------------------------------------------------------------------------------------------
C_ID CUSTOMERNAME CITY I_ID
------------------------------------------------------------------------------------------------------------------------
01 N ROY DELHI LC03
06 H SINGH MUMBAI PC03
12 R PANDEY DELHI PC06
15 C SHARMA DELHI LC03
16 K AGARWAL BANGALORE PC01
-------------------------------------------------------------------------------------------------------------------
Questions :
3) Display the customer name, city from table CUSTOMER and Item Name and Price from
ASSIGNMENT 1
Ans 1) SELECT * FROM CUSTOMER WHERE CITY=’DELHI’;
C_ID CUSTOMERNAME CITY I_ID
------- --------------------------------------------- ---------- -----
1 N ROY DELHI LC03
12 R PANDEY DELHI PC06
15 C SHARMA DELHI LC03
Ans 2) SELECT * FROM ITEM WHERE PRICE BETWEEN 35000 AND 55000;
I_ID ITEMNAME MANUFACTURER PRICE
---------- ----------------------------------- ---------------------------- -----------
PC01 PERSONAL COMPUTER ABC 35000
LC05 LAPTOP ABC 55000
PC06 PERSONAL COMPUTER COMP 37000
Ans 3) SELECT A.CUSTOMERNAME, A.CITY, B.ITEMNAME, B.PRICE FROM
CUSTOMER A, ITEM B WHERE A.I_ID= B.I_ID;
CITY
----------
MUMBAI
BANGLORE
DELHI
⮚ Assignment 2. TABLE: EMPLOYEE, TABLE: SALGRADE
EMPLOYEE
SALGRADE
SGRADE SALARY HRA
-------------- ------------ -----------
S02 32000 12000
S03 24000 8000
S01 56000 18000
Questions:
4) Display ECODE and NAME of those employees whose SGRADE is more than or
equal to ‘S03’.
5) Display sgrade from table SALGRADE of those employees whose SALARY +HRA
is more than 40000.
ASSIGNMENT : 2
NAME DESIG
----------------------------------- ----------------------
ABDUL AHMAD EXECUTIVE
RAVI CHANDER HEAD-IT
JOHN KEN RECEPIONIST
NAZAR AMEEN GM
MIN(DOB) MAX(DOJ)
--------------- ---------------
13-JAN-1980 23-MAR-2010
ECODE NAME
--------------- ---------------------------------
101 ABDUL AHMAD
102 RAVI CHANDER
103 JOHN KEN
STOCK
DEALERS
DCODE DNAME
------------- --------------------
Questions-
(1) Display details of all Items in the STOCK table where LASTBUY date is after 14-FEB-10.
(2) Display ITEMNO and Item Name of those ITEM from STOCK Table whose RATE is more than
Rupees 14 and ITEMNO is either 5005 or 5006.
(3) Display ITEMNO, ITEM of those items whose dealer code (DCODE) is 101 or Quantity in Stock
(QTY) is more than 160 from the table STOCK.
(4) Display Maximum RATE of Items for each dealer individually as per DCODE from the table
Stock.
Ans 2) SELECT ITEMNO, ITEM FROM STOCK WHERE RATE >14 AND ITEMNO =5005
OR ITEMNO=5006;
ITEMNO ITEM
------------------- --------------------
5005 BALL PEN 0.5
5006 GEL PEN CLASSIC
Ans 3) SELECT ITEMNO, ITEM FROM STOCK WHERE DCODE = 101 OR QTY > 160;
ITEMNO ITEM
--------------- --------------------
5002 GEL PEN PREMIUM
5006 GEL PEN CLASSIC
5001 ERASER SMALL
DCODE MAX(RATE)
-------------- -------------------
102 20
101 22
103 8
COUNT(DISTINCT DCODE)
-------------------------------
3
Assignment 4. TABLE: WORKER,TABLE: PAYLEVEL
WORKER
PAYLEVEL
Questions-
2.) Display NAME and DESIG of those WORKERs, whose PLEVEL is either P001 or P002.
3.) Display the content of all the Workers, where DESIG is Operator.
4.) To display maximum DOB and Minimum DOJ from the table WORKER.
19,’Daya Kishore’,’Operator’,’P003’,19-Jun-2008’,’11-Jul-1984’.
ASSIGNMENT 4:
Ans 1) SELECT * FROM WORKER ORDER BY DOB DESC;
NAME DESIGN
------------- --------------
Radhey Shyam Supervisor
Ameen Ahmed Mechanic
Sanya Clerk
MAX(DOB) MIN(DOJ)
--------------- --------------
12-JUL-1987 13-SEP-2004
RECIPIENT
---------------------------------------------------------- ----------------------------------------------------------------
REC_ID SENDER_ID REC_NAME REC_ADDRESS REC_CITY
-----------------------------------------------------------------------------------------------------------------
KO05 ND01 R BAJPAYEE 5,CENTRAL AVENUE KOLKATA
ND08 MU02 S MAHAJAN 116,A VIHAR NEW DELHI
MU19 ND01 H SINGH 2A,ANDHERI EAST MUMBAI
MU32 MU15 P K SWAMY B5,C S TERMINUS MUMBAI
ND48 ND50 S TRIPATHI 13,B1 D,MAYURVIHAR NEW DELHI
----------------------------------------------------------------------------------------------------------------------------
Questions-
a) Display the names of all Senders from Mumbai.
‘MU02’ or ‘ND50’
SENDER_ID = 'ND50';
REC_ID REC_NAME
------------- --------------------
ND08 S MAHAJAN
ND48 STRIPATHI
REC_CITY COUNT(*)
--------------- -----------------
KOLKATA 1
MUMBAI 2
NEW DELHI 2
SENDER_CITY
--------------------
MUMBAI
NEW DELHI
PROGRAM- 22
# Write a Python program to connect with MySQL using database connectivity and
perform the following operations on data in the database:
a) Create a table
b) insert the data
c) Fetch
d) Update
e) delete the data
a) Create a table
OUTPUT
b) insert the data
import mysql.connector
path=mysql.connector.connect(host="localhost",user="root",passwd="admin",
database="project")
cursor=path.cursor()
sql="INSERT INTO
STUDENTS(ROLL,SNAME,CLASS,CITY)VALUES({},'{}',{},'{}')".format(4,"ROHIT",11,
"DELHI")
cursor.execute(sql)
path.commit()
cursor.execute("SELECT* FROM STUDENTS")
print("The inserted data is :")
data=cursor.fetchall()
for x in data:
print(x)
cursor.close()
path.close()
OUTPUT
C) Fetch
import mysql.connector
path=mysql.connector.connect(host="localhost",user="root",passwd="admin",
database="project")
cursor=path.cursor()
data=cursor.fetchall()
for x in data:
print(x)
path.close()
cursor.close()
OUTPUT
d) Update
import mysql.connector
path=mysql.connector.connect(host="localhost",user="root",passwd="admin",
database="project")
cursor=path.cursor()
cursor.execute(sql)
path.commit()
data=cursor.fetchall()
for x in data:
print(x)
path.close()
cursor.close()
OUTPUT
e) delete
import mysql.connector
path=mysql.connector.connect(host="localhost",user="root",passwd="admin",
database="project")
cursor=path.cursor()
sql="DELETE FROM STUDENTS WHERE ROLL=2"
cursor.execute(sql)
path.commit()
cursor.execute("SELECT *FROM STUDENTS")
data=cursor.fetchall()
print("Data left in the table-:")
for x in data:
print(x)
path.close()
cursor()
OUTPUT
QUESTION -23
ii) Create a table “Student” in the database” Information” with following schema/specification:
(rno integer, sname varchar(20), city varchar(10), marks integer and grade char )
VALUES(1,'RAKESH','MUMBAI',35,'C'),(2,'ROHIT','DELHI',40,'B'),(3,'YOGENDRA','BANARAS',35,'C'),
(4,'MUDIT','LUCKNOW',50,'A');
OUTPUT
b)
import mysql.connector
path=mysql.connector.connect(host="localhost",user="root",passwd="admin",
database="information")
cursor=path.cursor()
sql="INSERT INTO
students(RNO,SNAME,CITY,MARKS,GRADE)values({},'{}','{}',{},'{}')".format(5,'Mohit
Agrawal','Pune',40,'C')
cursor.execute(sql)
path.commit()
# to check records
cursor.execute(" SELECT* FROM students")
print("The inserted data is :")
data=cursor.fetchall()
for a in data:
print(a)
path.close()
cursor.close()
OUTPUT
ii) Write a python program to send SQL command/query to display maximum
marks of the table.
import mysql.connector
path=mysql.connector.connect(host="localhost",user="root",passwd="admin",dat
abase="information")
cursor=path.cursor()
for d in cursor:
print(d)
path.close()
cursor.close()
OUTPUT
iii) Write a python program send SQL command/query to display number of
students in each grade.
import mysql.connector
path=mysql.connector.connect(host="localhost",user="root",passwd="admin",
database="information")
cursor=path.cursor()
cursor.execute(sql)
data=cursor.fetchall()
for a in data:
print(a)
path.close()
cursor.close()
OUTPUT
iv) To display the records of the table students as per ascending order of marks.
import mysql.connector
path=mysql.connector.connect(host="localhost",user="root",passwd="admin",dat
abase="information")
cursor=path.cursor()
cursor.execute(sql)
data=cursor.fetchall()
for a in data:
print(a)
path.close()
cursor.close()
OUTPUT
Bibliography