0% found this document useful (0 votes)
9 views

PRACTICAL FILE PROGRAMS Class XII Part-1

The document contains a series of Python programming tasks, including creating a basic calculator, generating abbreviated names, finding numbers divisible by 3 or 6, checking for palindromes, and identifying unique elements in a list. It also includes file manipulation tasks such as removing lines with a specific character, counting occurrences of words, and separating uppercase and lowercase letters into different files. Each task is accompanied by code snippets and sample outputs.

Uploaded by

rajkumar34287
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PRACTICAL FILE PROGRAMS Class XII Part-1

The document contains a series of Python programming tasks, including creating a basic calculator, generating abbreviated names, finding numbers divisible by 3 or 6, checking for palindromes, and identifying unique elements in a list. It also includes file manipulation tasks such as removing lines with a specific character, counting occurrences of words, and separating uppercase and lowercase letters into different files. Each task is accompanied by code snippets and sample outputs.

Uploaded by

rajkumar34287
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Programs to be written in PRACTICAL FILE

1.Write to show functionality of a basic calculator using functions.


def add(x,y):
return x+y
def sub(x,y):
return x-y
def multiply(x,y):
return x*y
def division(x,y):
return x/y

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

2. Write a function to read a name and then display it in abbreviated


form, like Abdul Kalam be displayed as AK.
def a(full_name):
parts = full_name.split()

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

3. Write a function that creates a list of numbers from 1 to 50 that is


either divisible by 3 or divisible by 6 , using list comprehension.
n=[]
s=[]
for i in range (1,51):
n.append(i)
for i in range (1,51):
if (i%3==0) or (i%6==0):
s.append(i)
print ("The numbers are divisible by 3 or 6 is",s)

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]

4. Write a function in Python to check whether a string is palindrome


or not using functions.
str=input("Enter a string :")
def ispalindrome(str):
return str==str[::-1]
ans=ispalindrome(str)
if ans:
print("Yes, It is Palindrome ")
else:
print("No it is not Palindrome ")

OUTPUT:

Enter a string :MAD


No it is not Palindrome
Enter a string :MADAM
Yes, It is Palindrome

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

a= int(input("Enter 1st no:"))


b= int(input("Enter 2nd no:"))
print ("The HCF is", hcf(a,b))a= int(input("Enter 1st no:"))
b= int(input("Enter 2nd no:"))
print ("The HCF is", hcf(a,b))
OUTPUT:
Enter 1st no:85
Enter 2nd no:45
The HCF is 5

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:

Computer Science is subject nd Python is progr mming l ngu ge

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:

a boy is playing there in the playground


Number of sentences having A or a : 1
9) Write a Python Program that reads a file “Story.txt” and counts the
occurrence of words ‘to’ & ‘the’ in the file.

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:

Number of to in the sentence : 2 Number of the in the sentence: 1

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

You might also like