12 Practical File 2024-25
12 Practical File 2024-25
Class12CSPracticalExercises2024-2025
COMPUTERSCIENCE
PRACTICALPROGRAMS
FOR GRADE –
XII[2024-
2025]
Preparedby:
1
Downloaded by DISHON raj ([email protected])
lOMoARcPSD|40449340
TABLEOFCONTENTS
6. Creatingapythonprogramtogeneraterandomnumberbetween1to6 ll
7. Creatingapythonprogramtoreadatextfilelinebylineanddisplayeachword l2
separatedby'#'.
8. Creatinga python program to read a text file and display the number of l3
vowels/consonants/lowercase/uppercasecharacters.
9. Read a text file and display the number of vowels/ consonants/ uppercase/ l5
lowercase characters and other than character and digit in the file.
lO. Creatinga pythonprogramtocopyparticularlinesof a textfileintoanothertext l6
file.
ll. Creatingapythonprogramtocreateandsearchrecordsinbinaryfile. l7
l2. Creatingapythonprogramtocreateandupdate/modifyrecordsinbinaryfile. l9
l3. Creatingapythonprogramtocreateandsearchemployee’srecordincsvfile. 2l
l4. Creatingapythonprogramtoimplementstackoperations(List). 23
l5. Creatingapythonprogramtoimplementstackoperations(Dictionary). 26
Python–SQLconnectivityprograms
l6. CreatingapythonprogramtointegrateMYSQLwithPython(Creatingdatabase 28
andtable)
l7. Creatinga python program to integrate MYSQL with Python (Inserting records 3O
anddisplayingrecords)
l8. CreatingapythonprogramtointegrateMYSQLwithPython(Searchingand 32
displayingrecords)
l9. CreatingapythonprogramtointegrateMYSQLwithPython(Updatingrecords) 33
SQLQueries
2O. SQLCOMMANDSEXERCISE–1 35
2l. SQLCOMMANDSEXERCISE–2 36
22. SQLCOMMANDSEXERCISE–3 38
23. SQLCOMMANDSEXERCISE–4 4O
24. SQLCOMMANDSEXERCISE–5 42
EX.NO:1
DATE:
TowriteamenudrivenPythonProgramtoperformArithmeticoperations(+,-*,/)
basedontheuser’schoice.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SAMPLEOUTPUT:
PythonProgramExecutedOutput:
***************************************************************************************
EX.NO:
2DATE:
CREATINGAPYTHONPROGRAMTODISPLAYFIBONACCISERIES
AIM:
TowriteaPythonProgramtodisplayFibonacciSeriesupto‘n’numbers.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SAMPLEOUTPUT:
PythonExecutedProgramOutput:
*****************************************************************************************
EX.NO:3
DATE:
CREATINGAMENUDRIVENPROGRAMTOFINDFACTORIALANDSUMOFLISTOFNUMBERSUSI
NGFUNCTION.
AIM:
TowriteamenudrivenPythonProgramtofindFactorialandsumoflistofnumbersusingfunction.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
7
SAMPLEOUTPUT:
PythonExecutedProgramOutput:
************************************************************************************************
EX.NO:4
DATE:
AIM:
ToWriteaPythonprogramtodefinethefunctionCheck(no1,no2)thattaketwonumbersandReturnsthe
numberthathasminimumonesdigit.
SourceCode:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SampleOutput:
**********************************************************************************
EX.NO:5
DATE:
CREATING Acsv FILE BY ENTERING USER-ID AND PASSWORD READ AND SEARCH
THE PASSWORD FOR GIVEN USERID
AIM:
To create a csv file by entering user-id and password,read and search the password for given
userid.
SOURCECODE:
import csv
with open("7.csv", "w") as obj:
fileobj = csv.writer(obj)
fileobj.writerow(["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
fileobj.writerow(record)
x = input("press Y/y to continue and N/n to terminate the program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("7.csv", "r") as obj2:
fileobj2 = csv.reader(obj2)
given = input("enter the user id to be searched\n")
for i in fileobj2:
next(fileobj2)
# print(i,given)
if i[0] == given:
print(i[1])
break
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SAMPLEOUTPUT:
***************************************************************************
10
EX.NO:6
DATE:
CREATINGAPYTHONPROGRAMTOGENERATERANDOMNUMBERBETWEEN1
TO6
AIM:
TowriteaPythonprogramtogeneraterandomnumberbetween1to6tosimulatethedice.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SAMPLEOUTPUT:
PythonExecutedOutput Program:
**********************************************************************************
11
EX.NO:
7DATE:
CREATING
APYTHONPROGRAMTOREADATEXTFILELINEBYLINEAN
DDISPLAYEACHWORDSEPARATEDBY '#'
AIM:
TowriteaPythonProgramtoReadatextfile"Story.txt"linebylineanddisplayeachword
separated by'#'.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SAMPLEOUTPUT:
Story.txt:
12
EX.NO:8
DATE:
To write a Python Program to read a text file "Story.txt" and displays the number
ofVowels/Consonants/ Lowercase /Uppercase/characters inthefile.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
13
SAMPLEOUTPUT:
Story.txt:
***************************************************************************************
14
EX.NO:9
DATE:
CREATING PYTHONPROGRAMTODISPLAYSHORTWORDSFROMATEXTFILE
AIM:
To Write a method Disp() in Python, to read the lines from poem.txt and display
thosewords whicharelessthan5characters.
SourceCode:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
Sample
Output:Poem.
txt:
PythonExecutedProgramOutput:
**********************************************************************************
15
EX.NO:10
DATE:
CREATINGAPYTHONPROGRAMTOCOPYPARTICULARLINESOFATEXTFILEINTOAN
ANOTHERTEXT FILE
AIM:
Towriteapythonprogramtoreadlinesfromatextfile"Sample.txt"andcopythoselinesintoano
therfilewhicharestartingwithanalphabet'a'or'A'.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SAMPLEOUTPUT:
PythonExecutedProgramoutput:S
ample.txt:
PythonExecutedProgramOutput:
New.txt:
*****************************************************************************************
16
EX.NO:
11DATE:
CREATINGAPYTHONPROGRAMTOCREATEANDSEARCHRECORDSIN
BINARYFILE
AIM:
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
17
SAMPLEOUPUT:
PYTHONPROGRAMEXECUTEDOUTPUT:
*******************************************************************************************
18
EX.NO:
12DATE:
CREATINGAPYTHONPROGRAMTOCREATEANDUPDATE/
MODIFYRECORDSINBINARY FILE
AIM:
TowriteaPythonProgramtoCreateabinaryfilewithrollnumber,name,markandupdate/
modifythemarkforagivenrollnumber.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
19
SAMPLEOUTPUT:
PYTHONPROGRAMEXECUTEDOUTPUT:
**********************************************************************************
20
EX.NO:
13DATE:
CREATINGAPYTHONPROGRAMTOCREATEANDSEARCHEMPLOYEE’SRECORD
INCSVFILE.
AIM:
TowriteaPythonprogramCreateaCSVfiletostoreEmpno,Name,SalaryandsearchanyEmpnoanddisplayN
ame,Salaryandifnot found displayappropriatemessage.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
21
SAMPLEOUTPUT:
PYTHONPROGRAMEXECUTEDOUTPUT:
******************************************************************************
22
EX.NO:
14DATE:
CREATINGAPYTHONPROGRAMTOIMPLEMENTSTACKOPERATIONS(LIST)
AIM:
TowriteaPythonprogramtoimplementStackusingalistdata-
structure,toperformthefollowingoperations:
(i) ToPushanobjectcontainingDoc_IDandDoc_nameofdoctorswhospecializein"E
NT"tothestack.
(ii) (ii) ToPoptheobjectsfromthestackanddisplaythem.(iii)
(iv) (iii) Todisplaytheelementsofthestack(afterperformingPUSHorPOP)
SOURCECODE:
23
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SAMPLEOUTPUT:
PythonProgramExecutedOutput:
24
****************************************************************************************
25
EX.NO:
15DATE:
AIM:
ToWrite a program, with separate user-defined functions to perform the following
operations:
(i) To Create a function Push(Stk,D) Where Stack is an empty list and D is Dictionary of
Items.fromthisDictionary Push the keys (name of the student) into a stack,
wherethecorrespondingvalue (marks)isgreaterthan70.
(ii) ToCreateaFunctionPop(Stk),whereStkisaStackimplementedbyalistofstudentnames.
Thefunctionreturnstheitems deletedfrom thestack.
(iii) Todisplaytheelementsofthestack(afterperformingPUSHorPOP).
SourceCode:
26
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SampleOutput:
*********************************************************************************
27
EX.NO:
16DATE:
CREATINGAPYTHONPROGRAMTOINTEGRATEMYSQLWITHPYTHON(CRE
ATINGDATABASEANDTABLE)
AIM:
To writea Python Program to integrate MYSQL with Python to create Database and Tableto
storethe detailsofemployees.
SourceCode:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
28
SampleOutput:
*******************************************************************************************************
29
EX.NO:
17DATE:
CREATINGAPYTHONPROGRAMTOINTEGRATEMYSQLWITHPYTHON
(INSERTING RECORDSANDDISPLAYINGRECORDS)
AIM:
TowriteaPythonProgramtointegrateMYSQLwithPythonbyinsertingrecordstoEmptableand
displaythe records.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
30
SAMPLEOUTPUT:
PythonExecutedProgramOutput:
******************************************************************************************************************************************************************************
31
EX.NO:
188DATE:
CREATINGAPYTHONPROGRAMTOINTEGRATEMYSQLWITHPYTHON
(SEARCHINGANDDISPLAYINGRECORDS)
AIM:
TowriteaPythonProgramtointegrateMYSQLwithPythontosearchanEmployeeusingEMPI
D and display the record if present in already existing table EMP, if not display
theappropriatemessage.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
SAMPLEOUTPUT:
PythonExecutedProgramOutput:
32
EX.NO:
19DATE:
CREATINGAPYTHONPROGRAMTOINTEGRATEMYSQLWITHPYTHON(UPDATI
NGRECORDS)
AIM:
TowriteaPythonProgramtointegrateMYSQLwithPythontosearchanEmployeeusingEMPI
DandupdatetheSalaryof an employee if present in already existing
tableEMP,ifnotdisplaytheappropriatemessage.
SOURCECODE:
Result:
Thus, the above Python program has been executed and the output is
verifiedsuccessfully.
33
SAMPLEOUTPUT:
PythonExecutedProgramOutput:
**************************************************************************************************************************************************
34
SQLCOMMANDSEXERCISE-l
Ex.No:
2ODATE:
AIM:
TowriteQueriesforthefollowingQuestionsbasedonthegiventable:
(a) WriteaQuerytoCreateanewdatabaseinthenameof"STUDENTS".
CREATEDATABASESTUDENTS;
(b) WriteaQuerytoOpenthedatabase"STUDENTS".
USESTUDENTS;
(c) WriteaQuerytocreatetheabovetablecalled:"STU"
CREATETABLESTU(ROLLNOINTPRIMARYKEY,NAMEVARCHAR(lO),GENDERV
ARCHAR(3),AGEINT,DEPTVARCHAR(l5),DOADATE,FEESI
NT);
(d) WriteaQuerytolistalltheexistingdatabasenames.
SHOWDATABASES;
(e) WriteaQuerytoListallthetablesthatexistsinthecurrentdatabase.
SHOWTABLES;
Output:
35
SQLCOMMANDSEXERCISE-2
Ex.No:
2lDATE:
AIM:
TowriteQueriesforthefollowingQuestionsbasedonthegiventable:
(a) WriteaQuerytoinsertalltherowsofabovetableintoInfotable.
INSERTINTOSTUVALUES(l,'Arun','M',24,'COMPUTER','l997-Ol-
lO',l2O);INSERTINTOSTUVALUES(2,'Ankit','M',2l,'HISTORY','l998-O3-
24',2OO);INSERTINTOSTUVALUES(3,'Anu','F',2O,'HINDI','l996-l2-
l2',3OO);INSERTINTOSTUVALUES(4,'Bala','M',l9,NULL,'l999-O7-
Ol',4OO);INSERTINTOSTUVALUES(5,'Charan','M',l8,'HINDI','l997-O6-
27',25O);INSERTINTOSTUVALUES(6,'Deepa','F',l9,'HISTORY','l997-O6-
27',3OO);
INSERTINTOSTUVALUES(7,'Dinesh','M',22,'COMPUTER','l997-O2-
25',2lO);INSERTINTOSTUVALUES(8,'Usha','F',23,NULL,'l997-O7-3l',2OO);
(b) WriteaQuerytodisplayallthedetailsoftheEmployeesfromtheabovetable'STU'.
SELECT*FROMSTU;
Output:
36
SELECTROLLNO,NAME,DEPTFROMSTU;
Output:
SELECTDISTICT(DEPT)FROMSTU;
Output:
(e) ToshowallinformationaboutstudentsofHistorydepartment.
SELECT*FROMSTUWHEREDEPT='HISTORY';
Output:
********************************************************************************************
37
Ex.No:22 SQLCOMMANDSEXERCISE-
3DATE:
AIM:
TowriteQueriesforthefollowingQuestionsbasedonthegiventable:
(a) WriteaQuerytolistnameoffemalestudentsinHindiDepartment.
SELECTNAMEFROMSTUWHEREDEPT='HINDI'ANDGENDER='F';
Output:
(b) WriteaQuerytolistnameofthestudentswhoseagesarebetween18to20.
SELECTNAMEFROMSTUWHEREAGEBETWEENl8AND2O;
Output:
38
SELECTNAMEFROMSTUWHERENAMELIKE'A%';
Output:
SELECTNAMEFROMSTUWHERENAMELIKE'_N%';
Output:
**********************************************************************************************************
39
Ex.No:23 SQLCOMMANDSEXERCISE-4
DATE:
AIM:
TowriteQueriesforthefollowingQuestionsbasedonthegiventable:
DELETEFROMSTUWHEREROLLNO=8;
Output(AfterDeletion):
(b) WriteaQuerytochangethefessofStudentto170whoseRollnumberis1,iftheexistingfessislessthan
130.
UPDATESTUSETFEES=l7OWHEREROLLNO=lANDFEES<l3O;
Output(AfterUpdate):
40
ALTERTABLESTUADDAREAVARCHAR(2O);
Output:
(d) WriteaQuerytoDisplayNameofallstudentswhoseAreaContainsNULL.
SELECTNAMEFROMSTUWHEREAREAISNULL;
Output:
(e) WriteaQuerytodeleteAreaColumnfromthetableSTU.
ALTERTABLESTUDROPAREA;
Output:
DROPTABLESTU;
Output:
*******************************************************************************************
41
Ex.No:24 SQLCOMMANDSEXERCISE-
5DATE:
AIM:
TowriteQueriesforthefollowingQuestionsbasedonthegiventable:
TABLE:UNIFORM
TABLE:COST
SELECTAVG(PRICE)FROMCOSTWHERECOMPANY='RAYMOND';
Output:
SELECT*FROMUNIFORMORDERBYSTOCKDATEDESC;
Output:
42
(c) ToDisplaymaxpriceandminpriceofeachcompany.
SELECTCOMPANY,MAX(PRICE),MIN(PRICE)FROMCOSTGROUPBYCOMPANY;
Output:
SELECTCOMPANY,COUNT(*)FROMCOSTGROUPBYCOMPANYHAVINGCOUNT(*)>2;
Output:
(e) TodisplaytheUcode,Uname,Ucolor,SizeandCompanyoftablesuniformandcost.
SELECTU.UCODE,UNAME,UCOLOR,SIZE,COMPANYFROMUNIFORMU,COSTCWHEREU.UCODE
=C.UCODE;
Output:
*****************************************************************************************
43