PRACTICAL FILE PROGRAMS Class XII Part-1
PRACTICAL FILE PROGRAMS Class XII Part-1
print ("Calculations:")
print("Select 1 for Addition ")
print("Select 2 for Subtraction")
print("Select 3 for Multiplication")
print("Select 4 for division")
while True :
choice = int(input("Enter your choice :"))
if choice in (1,2,3,4):
num1=float(input ("Enter first number"))
num2=float(input("Enter second number"))
if choice==1:
print ("Added value=",add(num1,num2))
elif choice==2:
print("Difference:",sub(num1,num2))
elif choice==3:
print("Multiplication:", multiply(num1,num2))
elif choice==4:
print("Division=", division(num1,num2))
else:
print("Invalid Choice")
OUTPUT:
Calculations:
Select 1 for Addition
Select 2 for Subtraction
Select 3 for Multiplication
Select 4 for division
Enter your choice :1
Enter first number56
Enter second number45
Added value= 101.0
Enter your choice :2
Enter first number67
Enter second number98
Difference: -31.0
Enter your choice :3
Enter first number67
Enter second number78
Multiplication: 5226.0
Enter your choice :4
Enter first number43
Enter second number98
Division= 0.4387755102040816
if len(parts) == 0:
return ""
last_name = parts[-1]
first_names = parts[:-1]
a = ' '.join([name[0] for name in first_names]) + ' ' + last_name[0]
return a
full_name = input("Enter the full name: ")
print(a(full_name))
OUTPUT:
Enter the full name: Abdul Kalam
AK
OUTPUT:
The numbers are divisible by 3 or 6 is [3, 6, 9, 12, 15, 18, 21, 24, 27,
30, 33, 36, 39, 42, 45, 48]
OUTPUT:
5. Write a function in python that takes a list and returns a new list
with unique elements of the first list.
def unique(list):
unique_list=[]
for i in list:
if i not in unique_list:
unique_list.append(i)
for i in unique_list:
print (i)
list1=[10,15,20,10,20,40,50,15,60]
print ("The unique values from 1st list is ")
unique(list1)
list2=[1,2,1,1,3,4,3,3,5,7,1,3]
print ("The unique values from 2nd list is")
unique(list2)
OUTPUT:
The unique values from 1st list is
10
15
20
40
50
60
The unique values from 2nd list is
1
2
3
4
5
7
6) Write in Python to find HCF through function.
def hcf(x, y):
while(y):
x, y = y, x % y
return x
7) Write a program to remove all the lines that contain the character
‘a’ in a file and write it to another file.
f=open ("Abc.txt","w")
f.write ("Computer Science is a subject and Python is a programming
language ")
f.close()
f=open ("Abc.txt","r")
f1=open ("test.txt","w")
l=f.readlines()
for i in l:
if 'a' in i:
i=i.replace('a',' ')
f1.write(i)
f1.close()
f.close()
OUTPUT:
8) Write a Python Program which reads the file ‘Story.txt’ & displays
the number of lines starting with letter ‘A’.
f=open ("Story.txt","w")
f.write ("a boy is playing there in the playground ")
f.close()
count=0
f=open ("Story.txt","r")
l=f.readlines()
for i in l:
if i[0] =="A" or i[0]=="a":
print (i)
count=count+1
print ()
print("Number of sentences having A or a :",count)
f.close()
OUTPUT:
f=open ("Story.txt","w")
f.write ("I object to your remarks the banks has not replied to my
letter yet")
f.close()
f1=open ("Story.txt","r+")
s=f1.split()
count1=0
count2=0
for i in s:
if i =="to":
count1=count1+1
elif i =="the":
count2=count2+1
print ("Number of to in the sentence :", count1,"Number of the in
the sentence:",count2)
f1.close()
OUTPUT:
10) Write a Pyhton Program which reads the file ‘Story.txt’ and finds
and copies the uppercase characters to file ‘Upper.txt’ , all the lower
characters to file ‘Lower.txt’ .
f=open ("Story.txt","w")
f.write ("I object to your Remarks the BAnks has Not replied TO my
letter yet")
f.close()
f=open ("Story.txt","r+")
for i in f.readlines():
u=i.upper()
f1=open("Upper.txt", "w")
f1.write(u)
f.close()
f=open ("Story.txt","r+")
for x in f.readlines():
l=i.lower()
f2=open("Lower.txt","w")
f2.write(l)
f.close()
OUTPUT:
Upper.txt
I OBJECT TO YOUR REMARKS THE BANKS HAS NOT REPLIED TO MY
LETTER YET
Lower.txt
i object to your remarks the banks has not replied to my letter yet