PRACTICLfile
PRACTICLfile
FILE
NAME:- NISHANT
CLASS:- XII-A
ROLL NO.;- 23
SUBJECT:- COMPUTER SCIENCE
Page 1 of 45
INDEX
S.No Topic
1. Write a program to find the maximum and
minimum numbers in the list.
2. Write a program to input a string and display:
(i). The number of lowercase letters.
(ii). The number of uppercase letters.
(iii). The number of digits.
(iv). The number of special characters.
3. Write a program to input a string and check
whether it’s a palindrome string or not.
4. Write a python program to create a dictionary
containing the names of the students as keys and
the marks secured by them in CS as the values,
respectively. Display the names and marks of the
students who have secured 80 and above, also
display the number of students securing 80 marks
and above.
5. Write a program to input the name and phone
numbers of n number of employees. Create a
dictionary to contain key and values as name and
phone number of the employee. Display the key
value pairs in ascending order of the names.
6. Write a function in python to count the number of
lines in a text file ‘story.txt’ which are starting
with an alphabet ‘A’.
7. Write a function Displayword() in python to read
lines from a text file ‘story.txt’ and display those
words which are less than 4.
8. Write a function in python to read lines from file
‘poem.txt’ and count how many times the word
‘India’ exists in file.
9. Take a sample text file and find the most
commonly occurring word. Also, list the
frequencies of word in the text file.
10. 10 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 second index moves to the third
index, etc., and the element in the last index moves to the
Page 2 of 45
first index.
11. Write a python code to accept a set of integers
number in a tuple. Find and display the numbers
that are prime.
12. Write a function Lshift(Arr,n) in python which
accepts a list Arr of numbers and n is a numeric
value by which all elements of the list are shifted
to left.
13. Create a binary file which should contain the
student details and to search the particular.
14. Create the binary file which should contains the
students details and to update the particular
student based on rollno and display the details.
15. Create the binary file which should contains the
details and to delete the particular student based
on roll no and display the details.
16. Write a program to implement a stack for this
book details (book no., book name). That is now
each item node of this stack contains two type of
information a bookno. and its name just
implement puch, pop and display operations.
17. Create a CSV file which should contains the
employee details and to search the particular
employee based on emp_no and display the
details.
18. Create a CSV file which should contains the
employee details and to update their salary based
on empno.
19. Create a CSV file which should contains the
employee details and to delete the particular
record based on emp no.
20. Write a python program to create the table and
insert the record into the table by getting the
data from the user and to store in the mysql.
21. Write a python program to search the records
from the table based on the exam number and
display the details of the student.
22. Write a python program to update the student
mark on table based on the examno given by the
user. If record not found display thee appropriate
message.
23. Write the query to create the above table and set
Page 3 of 45
the constraints fot the attributes.
Examno-primarykey, Name-notnull, Mark shoud
not exceed 500.
24. Write the query to insert the mentioned records into the
table.
25. Write the query to display all the student
records who are studying in A1.
26. Write the query to display the records whose marks are not
assigned.
27. Write the query to display whose marks are in the range 400
to 450(both values are inclusive).
28. Write the query to display the student name, marks of
those who secured more 400 but less than 487.
29. Write the query to display the details of those whose name
starts with ‘P’ or ‘B’.
30. Write the query to display the student name and section
whose name contains ‘priya’.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
Page 4 of 45
Q.1 Write a program to find the maximum and minimum numbers in the list.
list1=eval(input("Enter a list: "))
maximum=list1[0]
minimum=list1[0]
for i in list1:
if i>maximum:
maximum=i
if i<minimum:
minimum=i
print("Minimum Number: ",minimum)
print("Maximum Number: ",maximum)
Page 5 of 45
Q.2 Write a program to input a string and display:
(i). The number of lowercase letters.
(ii). The number of uppercase letters.
(iii). The number of digits.
(iv). The number of special characters.
Page 6 of 45
Q.3 Write a program to input a string and check whether it’s a palindrome
string or not.
Page 7 of 45
Q.4 Write a python program to create a dictionary containing the names of
the students as keys and the marks secured by them in CS as the values,
respectively. Display the names and marks of the students who have secured
80 and above, also display the number of students securing 80 marks and
above.
dict1={}
ans='Y'
while ans=='Y' or ans=='y':
name=input("Enter Name of Student: ")
marks=int(input("Enter marks obtained: "))
dict1[name]=marks
ans=input("Enter more records(Y/N): ")
n=0
print("Details of students who scored 80 and above :")
for i in dict1:
if dict1[i]>=80:
print(i,dict1[i])
n+=1
print('Number of students who scored 80 and above: ',n)
Page 8 of 45
Q.5 Write a program to input the name and phone numbers of n number of
employees. Create a dictionary to contain key and values as name and phone
number of the employee. Display the key value pairs in ascending order of
the names.
dict1={}
n=int(input("No. of records: "))
for i in range(n):
name=input("Enter the name of employee: ")
phoneno=int(input("Enter phone no.: "))
dict1[name]=phoneno
print('Original dictionary: \n',dict1)
list1=list(dict1.items())
list1.sort()
dict1=dict(list1)
Page 9 of 45
print("Dictionary sorted in ascending order by name: \n",dict1)
Q.6 Write a function in python to count the number of lines in a text file
‘story.txt’ which are starting with an alphabet ‘A’.
def countlines(filename):
count=0
with open(filename) as file:
list1=file.readlines()
for i in list1:
if i[0]=='A' or i[0]=='a':
count+=1
print("Number of lines starting with A are: ",count)
countlines('story.txt')
Page 10 of 45
Q.7 Write a function Displayword() in python to read lines from a text file
‘story.txt’ and display those words which are less than 4.
def Displayword():
with open('story.txt','r') as file:
list1=[]
print("Word that are less than 4 alphabets:")
for i in file.readlines():
for j in i.split():
if len(j)<4:
list1+=[j]
print(list1)
Displayword()
Page 11 of 45
Q.8 Write a function in python to read lines from file ‘poem.txt’ and count
how many times the word ‘India’ exists in file.
def CountIndia():
with open('poem.txt','r') as file:
count=0
for i in file.readlines():
count+=i.count('India')
print('Count of the word "India": ',count)
CountIndia()
Page 12 of 45
Q.9 Take a sample text file and find the most commonly occurring word.
Also, list the frequencies of word in the text file.
with open('sample.txt','r') as file:
list1=file.readlines()
eachword=[]
for i in list1:
for j in i.split():
eachword+=[j]
wordcount=[]
for i in range(0,len(eachword)):
str1=''
for j in eachword[i]:
if j.isalnum():
str1+=j
eachword[i]=str1.lower()
Page 13 of 45
if [eachword[i]] not in wordcount:
wordcount+=[[eachword[i]]]
maximumword=wordcount[0]
for i in range(0,len(wordcount)):
count=eachword.count(wordcount[i][0])
wordcount[i].append(count)
if maximumword[1]<wordcount[i][1]:
maximumword=maximumword[wordcount[i]]
print("Most commonly used word: ",maximumword)
print("Frequencies of word used: ")
print(dict(wordcount))
Page 14 of 45
Page 15 of 45
Q.10 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 second index
moves to the third index, etc., and the element in the last index moves to the
first index.
list1=eval(input("Enter a List:"))
print('Original List: ',list1)
for i in range(1,len(list1)):
list1[0],list1[i]=list1[i],list1[0]
print('List after rotation: ',list1)
Page 16 of 45
Q.11 Write a python code to accept a set of integers number in a tuple. Find
and display the numbers that are prime.
tuple1=eval(input("Enter a Tuple: "))
list1=[]
for i in tuple1:
for j in range(2,i//2+1):
if i%j==0:
break
else:
list1+=[i]
print('Original tuple: ',tuple1)
print('List of prime numbers: ',list1)
Page 17 of 45
Q.12 Write a function Lshift(Arr,n) in python which accepts a list Arr of
numbers and n is a numeric value by which all elements of the list are shifted
to left.
def Lshift(Arr,n):
for j in range(n):
for i in range(-1,-len(Arr)-1,-1):
Arr[i],Arr[0]=Arr[0],Arr[i]
print("List after being shifted by {}: ".format(n),Arr)
list1=eval(input("Enter a list of numbers: "))
shift=int(input("Shift left by... "))
Lshift(list1,shift)
Page 18 of 45
Q.13 Create a binary file which should contain the student details and to
search the particular.
import pickle
file=open('studentdetail.dat','rb')
search=int(input("Enter rollno of student you want to search: "))
try:
while True:
x=pickle.load(file)
if x[1]==search:
print('The details of the student are: ',x)
file.close()
break
except EOFError:
print('No records found')
file.close()
Page 19 of 45
Q.14 Create the binary file which should contains the students details and to
update the particular student based on rollno and display the details.
import pickle
search=int(input("Enter rollno of student you want to update: "))
found=False
file=open('student.dat','rb+')
records=pickle.load(file)
for row in range(len(records)):
if records[row][1]==search:
print('Details of student with {} rollno: '.format(search),records[row])
name=input("Enter Name of student: ")
rollno=int(input("Enter rollno of student: "))
class_=input("Enter class of student: ")
marks=int(input("Enter marks of students: "))
records[row]=[name,rollno,class_,marks]
found=True
break
if found==True:
file.seek(0)
pickle.dump(records,file)
else:
print("No records found")
file.close()
if found==True:
print("Records stored in file after updating:")
for row in records:
print(row)
Page 20 of 45
Q.15 Create the binary file which should contains the details and to delete
the particular student based on roll no and display the details.
import pickle
search=int(input("Enter rollno of student you want to delete: "))
found=False
file=open('student.dat','rb+')
records=pickle.load(file)
for row in range(len(records)):
if records[row][1]==search:
print('Details of student with {} rollno: '.format(search),records[row])
del records[row]
found=True
break
if found==True:
file.seek(0)
pickle.dump(records,file)
print("Record Removed")
else:
print("No Record Found")
file.close()
if found==True:
print("Records after deleting:\n",records)
Page 21 of 45
Q.16 Write a program to implement a stack for this book details (book no.,
book name). That is now each item node of this stack contains two type of
information a bookno. and its name just implement push, pop and display
operations.
def push(Stk,Item):
Stk.append(Item)
def pop(Stk):
Top=len(Stk)-1
if Top==-1:
return 'underflow'
else:
Item=Stk.pop()
return Item
def Display(Stk):
Top=len(Stk)-1
if Top==-1:
print('Stack is empty')
else:
for i in range(Top,-1,-1):
print(Stk[i])
print('1.push')
print('2.pop')
print('3.display')
print('4.Exit')
Stk=[]
Top=None
while True:
Page 22 of 45
choice=int(input("Enter your Choice: "))
if choice==1:
bookname=input('Enter book name: ')
bookno=int(input('Enter booknumber: '))
Item=[bookname,bookno]
push(Stk,Item)
elif choice==2:
Item=pop(Stk)
if Item=='underflow':
print('Stack is Empty')
else:
print('Deleted Item is',Item)
elif choice==3:
Display(Stk)
else:
break
Page 23 of 45
Page 24 of 45
Q.17 Create a CSV file which should contains the employee details and to
search the particular employee based on emp_no and display the details.
import csv
empno=int(input("Enter empno of employee you are searching: "))
file=open('empdetail.csv','r')
reader=csv.reader(file)
for i in reader:
if int(i[1])==empno:
print('The employee details: ',i)
break
else:
print("Data not found")
file.close()
Page 25 of 45
Q.18 Create a CSV file which should contains the employee details and to
update their salary based on empno.
import csv
file=open('empdetail.csv','r+',newline='')
search=int(input("Enter empno of employee you want to search: "))
newrecords=[]
found=False
reader=csv.reader(file)
writer=csv.writer(file)
for i in reader:
if int(i[1])==search:
print("Details of employee before updating: ",i)
salary=int(input("updated salary: "))
i[2]=salary
found=True
newrecords+=[i]
if found==True:
file.seek(0)
writer.writerows(newrecords)
print("Record Updated")
else:
print("No Data Found")
file.close()
Page 26 of 45
Q.19 Create a CSV file which should contains the employee details and to
delete the particular record based on emp no.
import csv
file=open('empdetail.csv','r',newline='')
search=int(input("Enter empno of employee you want to delete: "))
newrecord=[]
found=False
reader=csv.reader(file)
for i in reader:
if int(i[1])==search:
print("Details of employee: ",i)
found=True
continue
newrecord+=[i]
file.close()
if found==True:
file=open('updatesalary.csv','w',newline='')
writer=csv.writer(file)
writer.writerows(newrecord)
file.close()
print("Record Deleted")
else:
print("No Data Found")
Page 27 of 45
Q.20 Write a python program to create the table and insert the record into
the table by getting the data from the user and to store in the mysql.
import mysql.connector as ms
conn =ms.connect(user='root', password='xyz', host='localhost', database='startingmysql')
cursor = conn.cursor()
tablename=input("Enter name of table for creating: ")
cursor.execute("DROP TABLE IF EXISTS {}".format(tablename))
ans='Y'
column=[]
while ans=='y' or ans=='Y':
x=eval(input("Enter coloum detail in given format['name','datatype']: "))
column+=[x]
ans=input("Enter more column?(Y/N): ")
constraints=[]
ans=input("Add constraints on columns?(Y/N): ")
while ans=='y' or ans=='Y':
constraint=eval(input("Enter constraints for column in given format['constraintname
(column(s)name/condition)']: "))
constraints+=[constraint]
ans=input("Enter more constraint?(Y/N): ")
columnandconstrains=column+constraints
str1='create table {}('.format(tablename)
for i in range(0,len(columnandconstrains)):
for j in columnandconstrains[i]:
str1+=j+' '
if i+1==len(columnandconstrains):
str1+=')'
else:
str1+=','
cursor.execute(str1)
record="insert into {} values("
record+=(len(column)-1)*"'{}',"+"'{}')"
print("Table {} created".format(tablename))
ans='y'
while ans=='y' or ans=='Y':
list1=[]
for i in column:
list1+=[input("Enter {} :".format(i[0]))]
print(list1)
cursor.execute(record.format(tablename,*list1))
conn.commit()
print("Data Inserted")
ans=input("Insert more data?(Y/N)")
conn.close()
Page 28 of 45
Page 29 of 45
Q.21 Write a python program to search the records from the table based on
the exam number and display the details of the student.
import mysql.connector as ms
conn=ms.connect(host='localhost',user='root',password='xyz',database='startingmysql')
cursor=conn.cursor()
try:
tablename=input("Enter Tablename: ")
examno=int(input("Enter Exam number of student you want to search: "))
cursor.execute("select * from {} where examnumber={}".format(tablename,examno))
details=cursor.fetchone()
if details!=None:
print("The details of student with {} examnumber are: ".format(examno),details)
else:
print("No records found")
except Exception:
print("Table Does Not Exist")
conn.close()
Page 30 of 45
Q.22 Write a python program to update the student mark on table based on
the examno given by the user. If record not found display the appropriate
message.
import mysql.connector as ms
conn=ms.connect(host='localhost',user='root',password='xyz',database='startingmysql')
cursor=conn.cursor()
try:
tablename=input("Enter name of table: ")
examno=int(input("Enter examnumber of student whose marks you want to update: "))
cursor.execute('select * from {} where examnumber={}'.format(tablename,examno))
detail=cursor.fetchone()
if detail!=None:
print("Details of student: ",detail)
newmarks=input("Enter new marks: ")+'%'
statement='update {} set percentage="{}" where
examnumber={};'.format(tablename,newmarks,examno)
cursor.execute(statement)
conn.commit()
print("Update Complete")
else:
print("No records found")
except Exception:
print("Table does not exist")
conn.close()
Page 31 of 45
EXAMN NAM CLAS SEC MAR
O E S K
1201 PAVINDHAN XII A1 489
1202 ESHWAR XII A1 465
1203 BHARKAVI XII A2 498
1204 HARIPRIYA XII A2 400
1205 JAI PRADHAP XII NULL 398
1206 KAVIPRIYA XII A2 NUL
L
TABLE: STUDENTS
Q.23 Write the query to create the above table and set the constraints
exceed 500.
Page 32 of 45
Q.24 Write the query to insert the mentioned records into the table.
Q.25 Write the query to display all the student records who
are studying in A1.
Q.26 Write the query to display the records whose marks are not assigned.
Page 33 of 45
Q. 27 Write the query to display whose marks are in the range 400 to
450(both values are inclusive).
Q.28 Write the query to display the student name, marks of those who
secured more 400 but less than 487.
Page 34 of 45
Q.29 Write the query to display the details of those whose name starts
with ‘P’ or ‘B’.
Q.30 Write the query to display the student name and section whose name
contains ‘priya’.
Page 35 of 45
Q.31 Write the query query to display all the details sorted by name in
descending order.
Page 36 of 45
Q.32 Write the query to add 10 marks to the existing marks as ‘Updated
marks’ and display the details.
Q.33 Write the query to add a new column named as CITY with the data
type VARCHAR(30) and apply the default constraint ‘NOT MENTIONED’ in
the students table.
Q.34 Write the query to change the order of the column in the students
table as:
EXAMNO, NAME, CITY, CLASS, SEC, MARK
Page 37 of 45
Q.35 Write the query to redefine the NAME field size into VARCHAR(40) in
the students table.
Q.36 Write the query to update the marks as 350 whose marks are null and
update the section as A3 whose section is null.
Page 38 of 45
Q.37 Write the query to update the city of all records with the following
cities [CHENNAI, BENGALURU]
Page 39 of 45
TABLE NAME : DOCTOR
DOCID DNAME DEPT CITY SALARY
D01 ASHWATHAN ONCOLOGY CHENNAI 150000
D02 RAMYA GYNAECOLOGY COIMBATORE 140000
D03 SRIRAM DENTAL BHOPAL 180000
D04 SANJAY CARDIOLOGY HYDERABAD 160000
D05 SRINITHI PEDIATRICS DELHI 120000
D06 SANTHOSH PEDIATRICS BENGALURU 140000
D07 KARTHICK CARDIOLOGY JAIPUR 180000
D08 YASHIK GYNAECOLOGY AHMEDABAD 195000
Q.38 Write the query to create the patient table and keep DOCID to be the
foreign key with update and delete cascade.
Q.39 Write the query to display the count of male and female patients in the
age between 40 and 50.
Page 40 of 45
Q.40 Write the query to display the patient id, name and age who have fixed
an appointment on the September month.
Q.41 Write the query to display the number of doctors in each dept.
Page 41 of 45
Q.42 Write the query to display the sum of the salary of the doctors
departmentwise.
Q.43 Write the query to display patient name, doctor name, patient age and
Page 42 of 45
their appointment date from the tables.
Q.44 Write the query to display the doctor id, doctor name and number of
patients need to visit.
Q.45 Write the query to display the average salary of each dept from doctor
Page 43 of 45
table.
Q.46 Write the query to display the maximum and minimum salary of each
department.
Q.47 (i) Write the query to delete record of the doctor with the id ‘D08’ and
Page 44 of 45
‘D06’ from the table.
(ii) Write the output of the following queries after executing the above
question query.
SELECT * FROM DOCTOR;
SELECT * FROM PATIENT;
Page 45 of 45