





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























https://pythonschoolkvs.wordpress.com/ CLASS-XII SUBJECT ā COMPUTER SCIENCE (083) PRACTICAL FILE SOLUTION (Session 2021-22)
PRACTIC AL NO.
1. Write a program in python to check a number whether it is prime or not.
num=int(input("Enter the number: ")) for i in range(2,num): if num%i==0: print(num, "is not prime number") break; else: print(num,"is prime number")
2. Write a program to check a number whether it is palindrome or not.
num=int(input("Enter a number : ")) n=num res= while num>0: rem=num% res=rem+res* num=num// if res==n: print("Number is Palindrome") else: print("Number is not Palindrome")
3. Write a program to display ASCII code of a character and vice versa.
var=True while var: choice=int(input("Press-1 to find the ordinal value of a character \nPress-2 to find a character of a value\n")) if choice==1:
Press-1 to find the ordinal value of a character Press-2 to find a character of a value 2 Enter an integer value: 65 A Do you want to continue? Y/N
Write a function SwapNumbers( ) to swap two numbers and display the numbers before swapping and after swapping.
def SwapNumbers(x,y): a, b = y, x print("After Swap:") print("Num1: ", a) print("Num2: ", b)
Num1=int(input("Enter Number-1: ")) Num2=int(input("Enter Number-2: ")) print("Before Swap: ") print("Num1: ", Num1) print("Num2: ", Num2)
SwapNumbers(Num1, Num2)
Enter Number-1: 10 Enter Number-2: 20 Before Swap: Num1: 10 Num2: 20 After Swap: Num1: 20 Num2: 10
5. Write a program to find the sum of all elements of a list using recursion.
def SumAll(L): n=len(L) if n==1: return L[0] else: return L[0]+SumAll(L[1:])
LS=eval(input("Enter the elements of list: ")) total=SumAll(LS)
7. Write a program to print fibonacci series using recursion.
def fibonacci(n): if n<=1: return n else: return(fibonacci(n-1)+fibonacci(n-2))
num=int(input("How many terms you want to display: ")) for i in range(num): print(fibonacci(i)," ", end=" ")
How many terms you want to display: 8 0 1 1 2 3 5 8 13
8. Write a recursive python program to test if a string is palindrome or not.
def isStringPalindrome(str): if len(str)<=1: return True else: if str[0]==str[-1]: return isStringPalindrome(str[1:-1])
else: return False
#main
s=input("Enter the string : ") y=isStringPalindrome(s)
if y==True: print("String is Palindrome") else: print("String is Not Palindrome")
Enter the string : madam String is Palindrome
9. Write a program to generate random numbers between 1 to 6 and check whether a user won a lottery or not.
SOURCE CODE:
import random n=random.randint(1,6) guess=int(input("Enter a number between 1 to 6 :")) if n==guess:
fin=open("E:\book.txt","r") fout=open("E:\story.txt","a") s=fin.readlines( ) for j in s: if 'p' in j: fout.write(j)
fin.close() fout.close()
OUTPUT: **Write contents of book.txt and story.txt
12. Write a program to count number of words in a file.
fin=open("D:\python programs\Book.txt",'r') str=fin.read( ) L=str.split() count_words= for i in L: count_words=count_words+ print(count_words)
OUTPUT: 16
13. Write a python program to write student data in a binary file.
import pickle list =[ ] # empty list while True: roll = input("Enter student Roll No:") sname = input("Enter student Name :") student = {"roll":roll,"name":sname} list.append(student) choice= input("Want to add more record(y/n) :") if(choice=='n'): break file = open("student.dat","wb") pickle.dump(list, file) file.close( )
OUTPUT: Enter student Roll No:
list = pickle.load(file) found = 0 lst = [ ] for x in list: if roll in x['roll']: found = 1 x['name'] = input('Enter new name: ') lst.append(x) if found == 1: file.seek(0) pickle.dump(lst, file) print("Record Updated") else: print('roll number does not exist')
file.close( )
Enter roll number whose name you want to update in binary file : Enter new name: Harish Record Updated
16. Write a python program to delete student data from a binary file.
import pickle roll = input('Enter roll number whose record you want to delete:') file = open("student.dat", "rb+") list = pickle.load(file) found = 0 lst = [] for x in list: if roll not in x['roll']: lst.append(x) else: found = 1
if found == 1: file.seek(0) pickle.dump(lst, file) print("Record Deleted ") else: print('Roll Number does not exist') file.close( )
OUTPUT: Enter roll number whose record you want to delete:
with open('C:\ data.csv','rt')as f: data = csv.reader(f) for row in data: print(row)
def writecsv( ): with open('C:\data.csv', mode='a', newline='') as file: writer = csv.writer(file, delimiter=',', quotechar='"')
#write new record in file writer.writerow(['4', 'Devansh', 'Arts', '404'])
print("Press-1 to Read Data and Press-2 to Write data: ") a=int(input()) if a==1: readcsv() elif a==2: writecsv() else: print("Invalid value")
Press-1 to Read Data and Press-2 to Write data: 1 ['Roll No.', 'Name of student', 'stream', 'Marks'] ['1', 'Anil', 'Arts', '426'] ['2', 'Sujata', 'Science', '412'] ['3', 'Shivani', 'Commerce', '448'] ['4', 'Devansh', 'Arts', '404']
19. Write a program to create a library in python and import it in a program.
#Rect.py class Rectangle: def init(self): print("Rectangle")
def Area(self, length, width): self.l=length self.w=width print("Area of Rectangle is : ", self.l*self.w)
#Sq.py
from Shape import Sq from Shape import Tri
r=Rect.Rectangle( ) #Create an object r for Rectangle class r.Area(10,20) # Call the module Area( ) of Rectangle class by passing argument
s=Sq.Square( ) #Create an object s for Square class s.Area(10) # Call the module Area( ) of Square class by passing argument
t=Tri.Triangle( ) #Create an object t for Triangle class t.Area(6,8) # Call the module Area( ) of Triangle class by passing argument
Rectangle Area of Rectangle is : 200 Square Area of square is : 100 Trinagle
Area of Triangle is : 24.
20. Write a program for linear search.
L=eval(input("Enter the elements: ")) n=len(L) item=eval(input("Enter the element that you want to search : ")) for i in range(n): if L[i]==item: print("Element found at the position :", i+1) break else: print("Element not Found")
Enter the elements: 23,67,44,99,65,33,78, Enter the element that you want to search : 33 Element found at the position : 6
21. Write a program for bubble sort.
SOURCE CODE:
L=eval(input("Enter the elements:")) n=len(L) for p in range(0,n-1): for i in range(0,n-1):