SlideShare a Scribd company logo
GRADE : XII
COMPUTER SCIENCE
PRACTICAL FILE
Index
No. Practical Name Date Signature
1 Program to read and display file content line by
line with eachword separated by “ #”
2 Program to read the content of file and display the total
numberof consonants, uppercase, vowels and lower
case characters.
3 Program to read the content of file line by line
and write it toanother file except for the lines
contains „a‟ letter in it.
4 Program to create binary file to store Rollno and
Name, Searchany Rollno and display name if
Rollno found otherwise “Rollno not found”
5 Program to create binary file to store Rollno,
Name and Marksand update marks of entered
Rollno.
6 Program to generate random number 1-6, simulating a
dice.
7 Program to implement Stack in Python using List.
8 Create a CSV file by entering user-id and password, read and
search the password for given user- id.
Program 1: Program to read and display file content line by line with each
word separated by “ #”
#Program to read content of file line by
line#and display each word separated by
'#'
f = open("file1.txt")
for line in f:
words =
line.split()
for w in
words:
print(w+'#',end='')
print()
f.close()
NOTE : if the original content of file is:
India is my
countryI love
python
Python learning is fun
OUTPUT
India#is#my#country#
I#love#python#
Python#learning#is#fun
#
Program 2: Program to read the content of file and display the total numberof consonants,
uppercase, vowels and lower case characters.
#Program to read content of file
#and display total number of vowels, consonants, lowercase and uppercase characters
f = open("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()
NOTE : if the original content of file is:
India is my countryI
love python
Python learning is fun123@
OUTPUT
Total Vowels in file : 16
Total Consonants in file n : 30
Total Capital letters in
file
: 2
Total Small letters in file : 44
Total Other than letters : 4
Program 3: Program to read the content of file line by line and write it toanother
file except for the lines contains „a‟ letter in it.
#Program to read line from file and write it to another line
#Except for those line which contains letter 'a'
f1 = open("file2.txt")
f2 = open("file2copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print(“## File Copied Successfully! ##”)
f1.close()
f2.close()
NOTE: Content of file2.txt
a quick brown fox
one two three four
five six seven
India is my
countryeight nine
ten
bye!
OUTPUT
## File Copied Successfully! ##
NOTE: After copy content of file2copy.txt
one two three four
five six seven
eight nine ten
bye!
Program 4: Program to create binary file to store Rollno and Name, Searchany Rollno
and display name if Rollno found otherwise “Rollno not found”
#Program to create a binary file to store Rollno and name#Search
for Rollno and display record if found #otherwise "Roll no. not
found"
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))name
= input("Enter Name :")
student.append([roll,name]) ans=input("Add
More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to search :"))for s
in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Search more ?(Y) :")
f.close()
OUTPUT
Enter Roll Number
:1Enter Name :Amit
Add More ?(Y)y
Enter Roll Number
:2Enter Name :Jasbir
Add More ?(Y)y
Enter Roll Number
:3Enter Name :Vikral
Add More ?(Y)n
Enter Roll number to search
:2## Name is : Jasbir ##
Search more ?(Y) :y
Enter Roll number to search
:1## Name is : Amit ##
Search more ?(Y) :y
Enter Roll number to search :4
####Sorry! Roll number not found ####
Search more ?(Y) :n
Program 5: Program to create binary file to store Rollno,Name and Marksand update
marks of entered Rollno.
#Program to create a binary file to store Rollno and name#Search
for Rollno and display record if found #otherwise "Roll no. not
found"
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))name
= input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f) f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))for s in
student:
if s[0]==r:
print("## Name is :",s[1], " ##")
print("## Current Marks is :",s[2]," ##")m =
int(input("Enter new marks :")) s[2]=m
print("## Record Updated ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")ans=input("Update more ?(Y)
:")
f.close()
OUTPUT
Enter Roll Number
:1Enter Name :Amit
Enter Marks :99
Add More ?(Y)y
Enter Roll Number
:2Enter Name
:Vikrant Enter
Marks :88
Add More ?(Y)y
Enter Roll Number
:3Enter Name :Nitin
Enter Marks :66
Add More ?(Y)n
Enter Roll number to update
:2## Name is : Vikrant ##
## Current Marks is : 88 ##
Enter new marks :90
## Record Updated ##
Update more ?(Y) :y
Enter Roll number to update
:2## Name is : Vikrant ##
## Current Marks is : 90 ##
Enter new marks :95
## Record Updated ##
Update more ?(Y) :n
Program 6: Program to generate random number 1-6, simulating a dice.
# Program to generate random number between 1 - 6# To
simulate the dice
import randomimport
time
print("Press CTRL+C to stop the dice ")
play='y'
while play=='y':
try:
while True:
for i in range(10):
print()
n = random.randint(1,6)
print(n,end='')
time.sleep(.00001)
except KeyboardInterrupt: print("Your
Number is :",n) ans=input("Play
More? (Y) :")
if ans.lower()!='y':
play='n'
break
OUTPUT
4Your Number is : 4
Play More? (Y) :y Your
Number is : 3Play More?
(Y) :y Your Number is : 2
Play More? (Y) :n
Program 7: Program to implement Stack in Python using List.
def isEmpty(S):
if len(S)==0:
return True
else:
return False
def Push(S,item):
S.append(item)
top=len(S)-1
def Pop(S):
if isEmpty(S):
return "Underflow"
else:
val = S.pop()if
len(S)==0:
top=None
else:
top=len(S)-1
return val
def Peek(S):
if isEmpty(S):
return "Underflow"
else:
top=len(S)-1
return S[top]
def Show(S):
if isEmpty(S):
print("Sorry No items in Stack ")
else:
t = len(S)-1
print("(Top)",end=' ')
while(t>=0):
print(S[t],"<==",end=' ')t-
=1
print()
# main
begins hereS=[] #Stack
top=None
while True:
print("**** STACK DEMONSTRATION ******")
print("1. PUSH ")
print("2. POP")
print("3. PEEK")
print("4. SHOW STACK ")
print("0. EXIT")
ch = int(input("Enter your choice :"))if
ch==1:
val = int(input("Enter Item to Push :"))
Push(S,val)
elif ch==2:
val = Pop(S)
if val=="Underflow":
print("Stack is Empty")
else:
elif ch==3:
print("nDeleted Item was :",val)
val = Peek(S)
if val=="Underflow":
print("Stack Empty")
else:
elif ch==4:
print("Top Item :",val)
Show(S)
elif ch==0:
print("Bye")
break
OUTPUT
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :10
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :20
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :30
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :4
(Top) 30 <== 20 <== 10 <==
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :3Top
Item : 30
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :2 Deleted
Item was : 30
****STACKDEMONSTRATI*****
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :4(Top) 20
<== 10 <==
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :0
Bye
Program 8: Create a CSV file by entering user-id and password, read and search the password for
given user- id.
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 programn")
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 searchedn")
for i in fileobj2:
next(fileobj2)
# print(i,given)
if i[0] == given:
print(i[1])
break
Practical File Grade 12.pdf

More Related Content

Similar to Practical File Grade 12.pdf

Python легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачиPython легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачи
Maxim Kulsha
 
Python: легко и просто. Красиво решаем повседневные задачи.
Python: легко и просто. Красиво решаем повседневные задачи.Python: легко и просто. Красиво решаем повседневные задачи.
Python: легко и просто. Красиво решаем повседневные задачи.
Python Meetup
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptx
HongAnhNguyn285885
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
srxerox
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
Sunjid Hasan
 
Python
PythonPython
Python
대갑 김
 
Linux Lab Manual.doc
Linux Lab Manual.docLinux Lab Manual.doc
Linux Lab Manual.doc
Dr.M.Karthika parthasarathy
 
intro_to_python_20150825
intro_to_python_20150825intro_to_python_20150825
intro_to_python_20150825Shung-Hsi Yu
 
Python Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdfPython Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdf
Rahul Jain
 
Python crush course
Python crush coursePython crush course
Python crush course
Mohammed El Rafie Tarabay
 
Cs hangman
Cs   hangmanCs   hangman
Cs hangmaniamkim
 
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdfC# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
fazalenterprises
 
Dts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinDts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlin
Ahmad Arif Faizin
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
Iccha Sethi
 
Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & lists
Marc Gouw
 
Python
PythonPython
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programs
Prof. Dr. K. Adisesha
 
CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL
Rishabh-Rawat
 
Python real time tutorial
Python real time tutorialPython real time tutorial
Python real time tutorial
Rajeev Kumar
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programmingkayalkarnan
 

Similar to Practical File Grade 12.pdf (20)

Python легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачиPython легко и просто. Красиво решаем повседневные задачи
Python легко и просто. Красиво решаем повседневные задачи
 
Python: легко и просто. Красиво решаем повседневные задачи.
Python: легко и просто. Красиво решаем повседневные задачи.Python: легко и просто. Красиво решаем повседневные задачи.
Python: легко и просто. Красиво решаем повседневные задачи.
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptx
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
 
Python
PythonPython
Python
 
Linux Lab Manual.doc
Linux Lab Manual.docLinux Lab Manual.doc
Linux Lab Manual.doc
 
intro_to_python_20150825
intro_to_python_20150825intro_to_python_20150825
intro_to_python_20150825
 
Python Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdfPython Cheat Sheet 2.0.pdf
Python Cheat Sheet 2.0.pdf
 
Python crush course
Python crush coursePython crush course
Python crush course
 
Cs hangman
Cs   hangmanCs   hangman
Cs hangman
 
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdfC# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
 
Dts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlinDts x dicoding #2 memulai pemrograman kotlin
Dts x dicoding #2 memulai pemrograman kotlin
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
 
Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & lists
 
Python
PythonPython
Python
 
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programs
 
CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL CBSE Class 12 Computer practical Python Programs and MYSQL
CBSE Class 12 Computer practical Python Programs and MYSQL
 
Python real time tutorial
Python real time tutorialPython real time tutorial
Python real time tutorial
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
 

Recently uploaded

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 

Practical File Grade 12.pdf

  • 1. GRADE : XII COMPUTER SCIENCE PRACTICAL FILE Index No. Practical Name Date Signature 1 Program to read and display file content line by line with eachword separated by “ #” 2 Program to read the content of file and display the total numberof consonants, uppercase, vowels and lower case characters. 3 Program to read the content of file line by line and write it toanother file except for the lines contains „a‟ letter in it. 4 Program to create binary file to store Rollno and Name, Searchany Rollno and display name if Rollno found otherwise “Rollno not found” 5 Program to create binary file to store Rollno, Name and Marksand update marks of entered Rollno. 6 Program to generate random number 1-6, simulating a dice. 7 Program to implement Stack in Python using List. 8 Create a CSV file by entering user-id and password, read and search the password for given user- id.
  • 2. Program 1: Program to read and display file content line by line with each word separated by “ #” #Program to read content of file line by line#and display each word separated by '#' f = open("file1.txt") for line in f: words = line.split() for w in words: print(w+'#',end='') print() f.close() NOTE : if the original content of file is: India is my countryI love python Python learning is fun OUTPUT India#is#my#country# I#love#python# Python#learning#is#fun #
  • 3. Program 2: Program to read the content of file and display the total numberof consonants, uppercase, vowels and lower case characters. #Program to read content of file #and display total number of vowels, consonants, lowercase and uppercase characters f = open("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() NOTE : if the original content of file is: India is my countryI love python Python learning is fun123@ OUTPUT Total Vowels in file : 16 Total Consonants in file n : 30 Total Capital letters in file : 2 Total Small letters in file : 44 Total Other than letters : 4
  • 4. Program 3: Program to read the content of file line by line and write it toanother file except for the lines contains „a‟ letter in it. #Program to read line from file and write it to another line #Except for those line which contains letter 'a' f1 = open("file2.txt") f2 = open("file2copy.txt","w") for line in f1: if 'a' not in line: f2.write(line) print(“## File Copied Successfully! ##”) f1.close() f2.close() NOTE: Content of file2.txt a quick brown fox one two three four five six seven India is my countryeight nine ten bye! OUTPUT ## File Copied Successfully! ## NOTE: After copy content of file2copy.txt one two three four five six seven eight nine ten bye!
  • 5. Program 4: Program to create binary file to store Rollno and Name, Searchany Rollno and display name if Rollno found otherwise “Rollno not found” #Program to create a binary file to store Rollno and name#Search for Rollno and display record if found #otherwise "Roll no. not found" import pickle student=[] f=open('student.dat','wb') ans='y' while ans.lower()=='y': roll = int(input("Enter Roll Number :"))name = input("Enter Name :") student.append([roll,name]) ans=input("Add More ?(Y)") pickle.dump(student,f) f.close() f=open('student.dat','rb') student=[] while True: try: student = pickle.load(f) except EOFError: break ans='y' while ans.lower()=='y': found=False r = int(input("Enter Roll number to search :"))for s in student: if s[0]==r: print("## Name is :",s[1], " ##") found=True break if not found: print("####Sorry! Roll number not found ####") ans=input("Search more ?(Y) :") f.close()
  • 6. OUTPUT Enter Roll Number :1Enter Name :Amit Add More ?(Y)y Enter Roll Number :2Enter Name :Jasbir Add More ?(Y)y Enter Roll Number :3Enter Name :Vikral Add More ?(Y)n Enter Roll number to search :2## Name is : Jasbir ## Search more ?(Y) :y Enter Roll number to search :1## Name is : Amit ## Search more ?(Y) :y Enter Roll number to search :4 ####Sorry! Roll number not found #### Search more ?(Y) :n Program 5: Program to create binary file to store Rollno,Name and Marksand update marks of entered Rollno. #Program to create a binary file to store Rollno and name#Search for Rollno and display record if found #otherwise "Roll no. not found" import pickle student=[] f=open('student.dat','wb') ans='y' while ans.lower()=='y': roll = int(input("Enter Roll Number :"))name = input("Enter Name :") marks = int(input("Enter Marks :")) student.append([roll,name,marks]) ans=input("Add More ?(Y)") pickle.dump(student,f) f.close() f=open('student.dat','rb+') student=[] while True: try: student = pickle.load(f) except EOFError:
  • 7. break ans='y' while ans.lower()=='y': found=False r = int(input("Enter Roll number to update :"))for s in student: if s[0]==r: print("## Name is :",s[1], " ##") print("## Current Marks is :",s[2]," ##")m = int(input("Enter new marks :")) s[2]=m print("## Record Updated ##") found=True break if not found: print("####Sorry! Roll number not found ####")ans=input("Update more ?(Y) :") f.close() OUTPUT Enter Roll Number :1Enter Name :Amit Enter Marks :99 Add More ?(Y)y Enter Roll Number :2Enter Name :Vikrant Enter Marks :88 Add More ?(Y)y Enter Roll Number :3Enter Name :Nitin Enter Marks :66 Add More ?(Y)n Enter Roll number to update :2## Name is : Vikrant ## ## Current Marks is : 88 ## Enter new marks :90 ## Record Updated ## Update more ?(Y) :y Enter Roll number to update :2## Name is : Vikrant ## ## Current Marks is : 90 ## Enter new marks :95 ## Record Updated ## Update more ?(Y) :n
  • 8. Program 6: Program to generate random number 1-6, simulating a dice. # Program to generate random number between 1 - 6# To simulate the dice import randomimport time print("Press CTRL+C to stop the dice ") play='y' while play=='y': try: while True: for i in range(10): print() n = random.randint(1,6) print(n,end='') time.sleep(.00001) except KeyboardInterrupt: print("Your Number is :",n) ans=input("Play More? (Y) :") if ans.lower()!='y': play='n' break OUTPUT 4Your Number is : 4 Play More? (Y) :y Your Number is : 3Play More? (Y) :y Your Number is : 2 Play More? (Y) :n Program 7: Program to implement Stack in Python using List. def isEmpty(S): if len(S)==0: return True else: return False def Push(S,item): S.append(item) top=len(S)-1 def Pop(S): if isEmpty(S): return "Underflow" else: val = S.pop()if
  • 9. len(S)==0: top=None else: top=len(S)-1 return val def Peek(S): if isEmpty(S): return "Underflow" else: top=len(S)-1 return S[top] def Show(S): if isEmpty(S): print("Sorry No items in Stack ") else: t = len(S)-1 print("(Top)",end=' ') while(t>=0): print(S[t],"<==",end=' ')t- =1 print() # main begins hereS=[] #Stack top=None while True: print("**** STACK DEMONSTRATION ******") print("1. PUSH ") print("2. POP") print("3. PEEK") print("4. SHOW STACK ") print("0. EXIT") ch = int(input("Enter your choice :"))if ch==1: val = int(input("Enter Item to Push :")) Push(S,val) elif ch==2: val = Pop(S) if val=="Underflow": print("Stack is Empty") else: elif ch==3: print("nDeleted Item was :",val) val = Peek(S) if val=="Underflow": print("Stack Empty") else:
  • 10. elif ch==4: print("Top Item :",val) Show(S) elif ch==0: print("Bye") break OUTPUT **** STACK DEMONSTRATION ****** 1. PUSH 2. POP 3. PEEK 4. SHOW STACK 0. EXIT Enter your choice :1 Enter Item to Push :10 **** STACK DEMONSTRATION ****** 1. PUSH 2. POP 3. PEEK 4. SHOW STACK 0. EXIT Enter your choice :1 Enter Item to Push :20 **** STACK DEMONSTRATION ****** 1. PUSH 2. POP 3. PEEK 4. SHOW STACK 0. EXIT Enter your choice :1 Enter Item to Push :30 **** STACK DEMONSTRATION ****** 1. PUSH 2. POP 3. PEEK 4. SHOW STACK 0. EXIT Enter your choice :4 (Top) 30 <== 20 <== 10 <== **** STACK DEMONSTRATION ****** 1. PUSH 2. POP 3. PEEK
  • 11. 4. SHOW STACK 0. EXIT Enter your choice :3Top Item : 30 **** STACK DEMONSTRATION ****** 1. PUSH 2. POP 3. PEEK 4. SHOW STACK 0. EXIT Enter your choice :2 Deleted Item was : 30 ****STACKDEMONSTRATI***** 1. PUSH 2. POP 3. PEEK 4. SHOW STACK 0. EXIT Enter your choice :4(Top) 20 <== 10 <== **** STACK DEMONSTRATION ****** 1. PUSH 2. POP 3. PEEK 4. SHOW STACK 0. EXIT Enter your choice :0 Bye Program 8: Create a CSV file by entering user-id and password, read and search the password for given user- id. 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 programn")
  • 12. 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 searchedn") for i in fileobj2: next(fileobj2) # print(i,given) if i[0] == given: print(i[1]) break