XII Practical Program File Term1 2021 Zeeshan
XII Practical Program File Term1 2021 Zeeshan
Program File
Roll number: 8
Index
Sr no. Name of the Program Page Signature
number
1 Read a text file line by line and display each word separated by a
#
3 Remove all the lines that contain the character 'a' in a file and
write it to another file
4 Create a binary file with name and roll number. Search for a
given roll number and display the name, if not found display
appropriate message.
7 Program to create CSV file and store empno, name, salary and
search any empno and display name, salary and if not found,
display appropriate message
SOURCE CODE:
file_object= open("C:\\Users\\Zeeshan\\Documents\\Laughnowcrylater.txt","r")
my_file=file_object.read(10)
for i in my_file:
words=i.split()
for n in words:
print(n+"#")
file_object.close()
Output:
W#
o#
a#
h#
,#
w#
o#
a#
h#
Program 2: Read a text file and display the number of vowels/consonants/uppercase/lowercase
characters in the file.
SOURCE Code:
file=open("C:\\Users\\Zeeshan\\Documents\\Laughnowcrylater.txt","r")
file_object=file.read()
word=(file_object.splitlines())
print(word)
n=len(word)
vowel=consonent=uppercase=lowercase=0
vowels=('a','e','i','o','u','A','E','U','I','O')
cons=("b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z",
"B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","W","X","Y","Z")
for n in range(0,n):
for i in word[n]:
for x in i:
if x.isupper():
uppercase+=1
elif x.islower():
lowercase+=1
if x in vowels:
vowel+=1
elif x in cons:
consonent+=1
Output
['Woah, woah', 'Yeah', 'Sometimes we laugh and sometimes we cry, but I guess you know now, baby', 'I took
a half and she took the whole thing, slow down, baby (baby)', "We took a trip, now we on your block and it's
like a ghost town, baby", "Where do these niggas be at when they say they doin' all this and all that?"]
SOURCE Code:
file=open("C:\\Users\\Zeeshan\\Documents\\Laughnowcrylater.txt","r")
object1=file.read()
words=(object1.splitlines())
print(words)
p=len(words)
print(p)
for n in words:
if "a" in n:
file1=open("C:\\Users\\Zeeshan\\Documents\\Laughnowcrylater.txt","a")
file1.write(n)
file1.close()
file.close()
Output:
['Woah, woah', 'Yeah', 'Sometimes we laugh and sometimes we cry, but I guess you know now, baby', 'I took
a half and she took the whole thing, slow down, baby (baby)', "We took a trip, now we on your block and it's
like a ghost town, baby", "Where do these niggas be at when they say they doin' all this and all that?"]
6
Program 4: Create a binary file with name and roll number. Search for a given roll number and display
the name, if not found display appropriate message.
SOURCE Code:
import pickle
stu={}
def to_write():
Bfile=open("data.dat","ab")
for i in range(num):
pickle.dump(stu,Bfile)
Bfile.close()
def search_data():
Bfile=open("data.dat","rb")
found=0
try:
while True:
edata=pickle.load(Bfile)
if edata["roll"]==Z:
print(edata)
found=1
break
except EOFError:
pass
if found==0:
print("The rollno =",Z," record is not found")
Bfile.close()
while True:
if ch==1:
to_write()
if ch==2:
search_data()
break
Output:
1-Write in a file
2-search
ENTER NO OF STUDENTS: 2
1-Write in a file
2-search
SOURCE Code:
import random
i=(random.randint(1,6))
Output:
Source Code:
import pickle
data={}
def write():
file=open("name.dat","ab")
for i in range(no):
data["recordno"]=i+1
pickle.dump(data,file)
file.close()
def search():
file=open("name.dat","rb")
found=0
try:
while True:
dz=pickle.load(file)
if dz["name"]==n:
print(dz)
found=1
break
except EOFError:
pass
if found==0:
file.close()
while True:
if ch==1:
write()
if ch==2:
search()
break
Output:
ENU
1-Write in a file
2-search
NO. of records?: 2
MENU
1-Write in a file
2-search
Source Code:
import csv
writer=csv.writer(csvfile,delimiter=',')
anss='y'
while anss.lower()=='y':
writer.writerow([eno,name,salary])
anss="y"
reader=csv.reader(csvfile,delimiter=',')
while anss=="y":
found=False
if len(row)!=0:
if int(row[0])==e:
print("====================")
print("NAME: ",row[1])
print("SALARY: ",row[2])
found=True
break
if not found:
print("================")
print("================")
Output:
## Data Saved...#
ADD MOREE?? n
====================
NAME: were
SALARY: 23330.0
SOURCE Code:
def cyl(h,r):
area_cyl=2*3.14*h*r
return area_cyl
def con(l,r):
area_con=3.14*r*l
return area_con
def tax_price(Cost):
tax=0.18*Cost
net_tax_price=Cost +tax
return net_tax_price
area1=cyl(h,rad)
print(area1)
area2=con(l,rad)
print(area2)
canvas_area=area1+area2
price=unit_price*canvas_area
net_price=tax_price(price)
print("the payable amount is: ",net_price)
Output:
314.0
235.50000000000003
SOURCE CODE:
def sumsquare(num):
sum=0
for i in range(1,num+1):
sum= sum+i
sumsquare(num)
Output:
SOURCE Code:
def fullname(first,last):
print("Hello",fullname)
fullname(first,last)
Output
Hello XOXOXOLOLOLO
Program 11: Write a program using user defined function that accepts length and breadth of a rectangle
and returns the area and perimeter of the rectangle
SOURCE Code:
def perimeter_area(L,B):
area = L*B
perimeter = 2 * (L + B)
return (area,perimeter)
area,perimeter =perimeter_area(l,b)
Output:
1. A user defined function trafficLight( ) that accepts input from the user, displays an error message if the
user enters anything other than red, yellow, and green. Function light() is called and following is
displayed depending upon return value from light().
2. A user defined function light() that accepts a string as input and returns 0 when the input is red, 1
when the input is yellow and 2 when the input is green. The input should be passed as an argument.
3. Display “ SPEED THRILLS BUT KILLS” after the function trafficLight( ) is executed.
SOURCE Code:
def trafficlight():
else:
value=light(signal)
if value==0:
print('stop')
elif value==1:
print('go slow')
else:
print('go')
def light(colour):
if colour==('red'):
return(0)
elif colour==('yellow'):
return(1)
else:
return(2)
trafficlight()
Output:
stop
Employee number:
Employee name:
Employee salary:
SOURCE Code:
import csv
writer=csv.writer(csvfile,delimiter=',')
anss='y'
while anss.lower()=='y':
writer.writerow([eno,name,salary])
Output:
## Data Saved...#
## Data Saved...#
WANT TO ADD MOREE DATA (y/n)?? n