CLASS XII – COMPUTER SCIENCE (083)
Python Practical File
Session: 2024–25
Name: ___________________________
Class: XII
Section: _______
Roll No: _______
School: _________________________
1. Read file and display words separated by #
Aim: To read a text file line by line and display each word separated by # symbol.
Algorithm:
1. Open the text file in read mode.
2. Read file line by line.
3. Split each line into words.
4. Display words separated by #.
5. Close the file.
Program:
f=open("[Link]","r")
for line in f:
for w in [Link]():
print(w,end="#")
print()
[Link]()
Output:
[Paste output screenshot here]
2. Count vowels, consonants, uppercase and lowercase letters
Aim: To count vowels, consonants, uppercase and lowercase letters in a text file.
Algorithm:
1. Open file in read mode.
2. Read each character.
3. Check alphabet.
4. Increment counters.
5. Display result.
Program:
f=open("[Link]","r")
v=c=u=l=0
for ch in [Link]():
if [Link]():
if ch in "AEIOUaeiou":
v+=1
else:
c+=1
if [Link]():
u+=1
if [Link]():
l+=1
print(v,c,u,l)
[Link]()
Output:
[Paste output screenshot here]
3. Remove lines containing 'a'
Aim: To remove all lines containing character 'a' and write remaining lines to another file.
Algorithm:
1. Open source file in read mode.
2. Open target file in write mode.
3. Read each line.
4. Write line if 'a' not present.
5. Close files.
Program:
f1=open("[Link]","r")
f2=open("[Link]","w")
for line in f1:
if 'a' not in line:
[Link](line)
[Link]()
[Link]()
Output:
[Paste output screenshot here]
4. Count lines, words and characters
Aim: To count number of lines, words and characters in a text file.
Algorithm:
1. Open file in read mode.
2. Read each line.
3. Count lines, words and characters.
4. Display counts.
Program:
f=open("[Link]","r")
l=w=c=0
for line in f:
l+=1
w+=len([Link]())
c+=len(line)
print(l,w,c)
[Link]()
Output:
[Paste output screenshot here]
5. Replace spaces with #
Aim: To replace all spaces in a text file with # symbol.
Algorithm:
1. Open file and read data.
2. Replace spaces with #.
3. Write back to file.
4. Close file.
Program:
f=open("[Link]","r")
data=[Link]()
[Link]()
f=open("[Link]","w")
[Link]([Link](" ","#"))
[Link]()
Output:
[Paste output screenshot here]