Download as pdf
Download as pdf
You are on page 1of 11
PRACTICAL FILE 2023-24 CLASS XII- COMPUTER SCIENCE In Index Page, it shold be like DiceThrow( ) Random number 1-6 ChangeNum( ) List elelement Even Half Odd Double ConverStr() Convert a to @ and s to $ VowelCount( ) to count Vowel, Upper and Lowercase CountMeHe{ ) to count word He and Me (without case) Data file “story.txt” and count the occurrence of word “to” Backup text file “school.txt” to “schbkp.txt" Backup text file biodata.txt to biobkp.txt except 'm” Binary file student.rec to write and read name 10. Binary file “Center.|st” to search center 11. Csv file to show items list - item.csv 12. Csv file search items list - items.csv 13. Csv file to display sport records - sports.csv 14. Stack S ~ PUSH Operation 15. Stack S - POP Operation 16. Stack employe Push Operation 17. Stack employee POP Operation 18. Stack BOOK - PUSH and POP operation LIST OF PRACTICALS IN PYTHON FOR CLASS XII FUNCTIONS 1, Write a Python program to create a function DiceThrow( } that generate generates random numbers between 1 and 6 (simulates a dice) using a user defined function. 2. Write a python program to create a function ChangeNum( ) to pass a list to a function and double the odd values and half even values of a list and display list elements after changing Given list is L1=[5, 17, 22, 12, 13, 44, 6,7] Converted lis L1=[10, 34, 11, 6, 26, 22, 3, 14] 3. Write a Python program to create a function ConvertStr( ) that receives an string and convert ‘a' with ‘@’ and ‘s’ with '§’ sign and print the converted string, 4, Write a Python program to create a function VowelCount( ) and pass a string to a function and count how many vowels, upper case characters and lower case characters present in the string, 5. Write a python program to create a function CountMeHe( ) to count the number of occurance of word He and Me present in the given string SEN AT eON DATA FILE 6. Write a python program to create data file “story.txt” and enter the following text And then count the number occurrence of word “to” in the data file “Early to bed and early to rise” 7. Write a python program to create data file “school.txt”, Then take the backup to another text file “schbkp.txt”. Show the content of both the data file after taking backup 8, Write a program in python to create a data file “Biodata.txt” and Remove all the lines that start with the character 'm' in a file and write it to another data file “biobkp.txt” BINARY FILE 9. Write a python program to create binary file student.rec which store the name of 5 students and then read and print the name of students 10. Write a python program to create a binary file “Center.Jst” to enter exam cities and then search for exam center inputted by the user. CSV FILE 11. Write a program in Python to create a csv file name “item.csv” and write Itemname and price to csv file and then display all records. 12. Write a program in Python to create a csv file name “items.csv” and write Itemname and price to csv file and then search for the price of item name inputted by the user. 13. Write a program in Python to create a csv file name “sports.csv” and write sports code, sport name, number of player and type of sports (indoor or outdoor) to csv file and then display all records. STACKS 14, Write a program in Python to create a stack Stack. Push elements in stack $ and then show all the lements in the newly created stack $ (PUSH Operation) 15. Write a program in Python to create a stack Stack. Insert few elements in stack. Then ask the user to remove (POP) the element in stack $ and then show all the lements in the newly created stack $ (POP Operation) 16. Write a program in Python to create stack name “employee” having employee code and name of 03 employees, Then Insert the details of one more employee to the stack. (PUSH Operation) 17. Write a program in Python to create stack name “employee” having employee code and name of 03 employees. Then remove the details of one more employee to the stack. (POP Operation) 18. Write a program in python to create stack BOOK having details of 02 books. Write the code to insert a new book (PUSH) and delete a book (POP) and show the details of book at the end. Manish Kumar Gupta, PGT CS Kendriya Vidyalaya No. 2 Sagar Region : Jabalpur SOLUTION 1. Write a Python program to create a function DiceThrow( ) that generate generates random numbers between 1 and 6 (simulates a dice) using a user defined function. # DiceThrow( ) Function import random Dice digit is: 4 def DiceThrow(): - x= mum = random.randrange(6) Dice digit is : 2 return x Dice digit is : 6 num = DiceThrow() print("Dice digit is :",»num) Dice digits: 2 2. Write a python program to create a function ChangeNum( ) to pass a list to a function and double the odd values and half even values of a list and display list elements after changing. #Even Half Odd Double MyList = [5, 17, 22, 12, 13, 44, 6, 7] print("Original List is : \t", MyList) riginal List is: [5, 17, 22, 12, 13, 44, 6, 7] converted List is : [10, 34, 11, 6, 26, 22, 3, 14] RESTART: D:\F| def changenum(L1): t=len(L1) riginal List is [5, 17, 22, 12, 13, 44, 6, 7] for x in range(0,t): converted List is: [10, 34, 11, 6, 26, 22, 3, 14] if(L1Pd % ): RESTART: D:\FL Ufx] = LAfx] // riginal List is: [5, 17, 22, 12, 13, 44, 6, 7] else: converted List is: [10, 34, 11, 6, 26, 22, 3, 14] LA[x] = Lifx] * 2 changenum(MyList) print("Converted List is :\t",MyList) 3. Write a Python program to create a function ConvertStr( ) that receives an string and convert ‘a’ with *@” and ‘s’ with ‘S’ sign and print the converted string. #Convert String Character replace ‘a’ with ‘@’ ands’ with ‘S’ def ConvertStr(S1): Enter your name : Manish After Conversion #for x in range(0, len(S1)) : m@nish S1=S1replace("a", "@") S1=S1.replace("s", "$") REE return S1 Enter your name : Vikraant nm = input("Enter your name :") After Conversion Vikr@@nt nm= ConvertStr(nm) print("After Conversion") REE print(nm) Enter your name : Sumitra Nandan After Conversion ‘Sumitr@ N@nd@n 4, Write a Python program to create a function VowelCount( ) and pass a string to a function and count how many vowels, upper case characters and lower case characters present in the string. #Function to count no. of vowels, upper and lower case = def VowelCount(str) Enter your name : educATIOn 0 v Total Vowel : 5 REP Lower Case : 6 U=0 Upper Case : 3 for min Str: if(m.islower()): LeL+1 Enter your name : KriShnAkAnT if(misupper()) Total Vowel : 3 U=U+1 Lower Case : 6 m= mlower() Upper Case : 5 if(m "i" or m= Enter your name : SaBARMAti return ve, L, U MyStr = input("Enter your name :") ee 6 VowC, Le, Uc = VowelCount(Mystr) Upper Case print("\nTotal Vowel :", VowC) print("Lower Case :", Le) print("Upper Case :", Uc) 5. Write a python program to create a function CountMeHe( ) to count the number of occurance of work He and Me present in the given string #Function to count the occurrence of word He and Me def CountMeHe(Str): cme=0 Che=0 Total Me: 1 word = Str.split() Total He : 1 for min word: if(m.lower() == "me") Cme =Cme +1 Enter String Line : me he RESTART: D:\PythonProg\P| Enter String Line : me him her he HE he me if(m.lower() == "he"): Total Me : 2 Che = Che +1 Total He : 3 return Cme, Che RESTART: D:\PythonProg\P} MyStr = input("Enter String Line Enter String Line : he and me came. He enjoyed and HE left M, H = CountMeHe(MyStr) sedate 4 print(""\nTotal Me : ", M) Total He : 3 print("Total He : ", H) 6. Write a python program to create data file “story.txt” and enter the following text And then count the number occurrence of word “to” in the data “Early to bed and early to rise” #Count of occurrence of work my in text file cto=0 f= open("story.txt", "w") str = input("Enter String f.write(str) print("File Created successfully") f.close() ) 2 = open("story.txt","r") D1=f2.read() S1= D1 split() for x in SL if(x == "to" cto=cto+1 print("Word my occured ",cto," times") RESTART: D:/PythonProg/ Enter String : early to bed and early to rise File Created successfully Word my occured 2 times RESTART: D:/PythonProg/ Enter String : to give anc to take is to good to al File Created successfully Word my occured 4 times f2.close() 7. Write a python program to create data file “schbkp.txt”. SI #t create school.txt and schbkp.txt except starting with character fw= open("'school.txt", "w for m in range(0,x): myst = input("Enter String :") sl =mystr+"\n! fw.write(s1) fw.close() fr = open("school.txt", "r") fb = open("schbkp.txt’, "w" for min range(0,3): D1 = fr.readline() d2=D1 print(d2) fb.write(d2) fr.close() fb.close() print("Data in School.txt") print(" £1 = open("school.txt", "r") for m in range(0,x): a file “school.txt”. Then take the backup to another text show the content of both the data file after taking backup a int(input("Enter no. of lines you want to add : ")) D1 = fl.readline() Enter String : | am aditya patel Enter String : My class is Xl Science Enter String : Favourite subject is CS lam aditya patel My class is XII Science Favourite subject is CS Data in School. txt | am aditya patel My class is XII Science Favourite subject is CS Data in schbkp.txt | am aditya patel My class is XII Science Favourite subject is CS print(D1) fi.close() print("Data in schbkp.txt") print(" £2 = open("schbkp.txt", "r") for m in range(0,x): D1 = f2.readline() print(D1) f2.close() 8. Writea program in python to create a data file “Biodata.txt” and Remove all the lines that start with the character 'm’ in a file and write it to another data file “biobkp.txt”. # create school.txt and biodata.txt except starting with character "m' fw = open("biodata.txt", "w" int(input("Enter no. of lines you want to add :")) for m in range(0,x): mystr = input("Enter String : ") = RESTART: D:\f sl = mystr + "\n’ Enter no. of lines you want to add : 3 fw.write(s1) Enter String : iam student fw.close() Enter String : my subject is science Enter String : aim is to successful fr = open("biodata.txt", "r") iam student fb = open("biobkp.txt", "w" for m in range(0,x): aim is to successful d2 = fr.readline() if(d2{0] != "m"): Data in biobkp.txt print(d2) 7 fb.write(d2) iam student fr.close() fo-close() Jaim is to successful print("Data in biobkp.txt") print(” £2 = open("biobkp.txt", "«") for m in range(0,x): D1 = f2.readline() print(D1) f2.close() 9. Write a python program to create binary file student.rec which store the name of 5 students and then read and print the name of students #Binary file write & read name of 5 students import pickle . Stud = 'aman','vimal''raman’, ‘arun’, ‘shiv inary file successfully created f= open("student.rec","wb") Data in Binary File pickle.dump(Stud, f) print("Binary file successfully created") f.close() ‘aman’, 'vimal', 'raman’, ‘arun’, 'shiv'] import pickle = open("student.rec","rb") ickle.load(f) print("Data in Binary File") print(D) f.close() 10, Write a python program to create a binary file *Center.{st” to enter exam cities and then search for exam center inputted by the user. #Binary file write & read name of 5 students import pickle i} num = int(input("Enter number of centers to add : ")) for x in range(0,num): center = input("Enter Center name :") CenterLst.append(center) cf = open("‘center.Ist","wb") pickle.dump(Centertst, cf) print("Center List Binary file successfully created") cf.close() f= open("center.{st","rb") mycent = input("Enter center to be searched : ") for x in D: if(mycent == x): print("Your Center is available : print("Center you are seeking is NOT AVAILABLE") f.close() 11. Write a program in Python to create a csv file name “item.csv” and write Itemname and price to csv file and then display all records. #Read and write in a csv file -item.csv import csv F = open("item.csv", " W=csv.writer(F) N= int(input("How many no. of records to enter : ")) for iin range(N): iname= input{"Enter Item Name :") price = int(input("Enter price of item : ")) ItemLst = [iname, price] W.writerow(Itemtst) print("Records of Items successfully added\n") F.close() 'w", newline =") F = open("item.csv","r") rec = csv.reader(F) print("\nRecords Items in file") print("\\nitem Name\tPrice") print("-- print(L{o],"\t", L{1]) F.close() [Python 3.10.5 (tags'v3.10.5:377153, Jun 6] ls2 [Type "help", “copyright”, “credits” or “licensé RESTART: D:\Python! lEnter number of centers to add : 3 JEnter Center name : damoh lEnter Center name : sagar lEnter Center name : jabalpur |Center List Binary file successfully created lEnter center to be searched : sagar lYour Center is avallable : sagar Enter number of centers to add : 3 lEnter Center name : damoh [Enter Genter name : sagar lEnter Center name : jabalpur ICenter List Binary file successfully created [Enter center to be searched : katni [Center you are seeking is NOT AVAILABLE ESTART: DP] How many no. of records to enter: 3 Enter Item Name : pen Enter price of item : 5 Records of Items successfully added Enter Iter Name : peneil Enter price of item : 2 Records of tems successfully added Enter ters Name : copy Enter price of tem : 12 Records of tems successfully added Records items in file Price ltem Name 12. Write a program in Python to create a csv file name “items.csv” and write Itemname and price to csv file and then search for the price of item name inputted by the user. #Read and write in a csv file -items.csv import csv “E = open("items.csv", " W =csv.writer(F) N= int(input("How many no. of records to enter :")) for iin range(N) iname= input{"Enter Item Name :") price = int(input("Enter price of item : ")) ItemLst = [iname, price] W.writerow(Itemtst) print("Records of Items successfully added\n") F.close()"" 'w", newline F = open("items.csv","r") flag=0 rec = csv.reader(F) inm = input("Enter the items name : ") for iin rec: if(L{0] == inm): print("Price of ",L(0}," is", L(t) flag = 1 iffflag == 0): print("Searched item not available") F.close() How many no. of records to enter RESTA How many no. of records to enter : 2 Enter tem Name : bag Enter price of tem : 300 Records of Items successfully added Enter tem Name : bottle Enter price of item : 75 Records of Items successfully added Enter the items name : bag Price of bag is 300 Enter the items name : pen Searched item not available 13, Write a program in Python to create a csv file name “sports.csv” and write sports code, sport name, number of player and type of sports (indoor or outdoor) to csv file and then display all records. #Read and write in a single csv file import csv F = open("sports.csv", " W=csv.writer(F) N= int(input("How many no. of records to enter : ")) 'w", newline =") for iin range(N): scode = int{input("Enter Sports Code No. :")) sname= input("Enter Sport Name : ") nop styp int{input("Enter number of player :")) input( "Type of Sport (Indoor / Outdoor) : ") scode, sname, nop,stype] W.writerow(L) print("Records of sports successfully added\n") F.close() F = open("sports.csv","r") rec = csv.reader(F) print(""\nRecords in file") print("\nCode\tName\tPlayer\tType of sport") print(L(O],"\t", L(1],"\t", L(2],"\t", L{3]) F.close() How many no. of records to enter : 2 Enter Sports Code No. 2001 Enter Sport Name : Chess Enter numberof player: 2 Type ofSpot (Indoor / Outdoor): Indoor Records of sports successfully added Enter Sports Code No. : 2005 Enter Sport Name : Cricket Enter numberof player: 11 Type ofSpor (Indoor / Outdoor) : Outdoor Records of sports successfully added Records in file Code Name Player Type of spor 2001 Chess 2 Indoor 2005 Cricket 11 Outdoor 14, Write a program in Python to create a stack Stack. Push elem the lements in the newly created stack S (PUSH Operation) #PUSH and SHOW Stack Enter element to push in stack $ : 100 S=(]__ #empty Stack ‘Stack after PUSH [100] ans want to continue (yin): y Enter element to push in stack S : 22 print("Insert element in Stack and Show all elements") _ | Stack after PUSH [100, 22] a want to continue (y/n) : y while(ans =="y"): ele= int(input("Enter element to push in stack $:")) | Stuck sperPUSY [100 22,59) Sappend(ele) want to continue (yin): print("Stack after PUSH ",S) Enter element to push in stack S : 85 ‘Stack after PUSH [100, 22, 99, 55] want to continue (yin) n ans = input("want to continue (y/n) : print(""\n\nAll stack elements") All stack elements for x in range(len(S)-1,-1,-1): 55.99.22 100 print(S[x],end ="") 15, Write a program in Python to create a stack Stack. Insert 05 elements it user to remove (POP) the clement in stack S and then show alll the lements stack S (POP Operation) #POP and SHOW Stack = [25, 75, 33,85] _#Stack with 5 elements Elements in Stack S ans="y" (25, 75, 33, 85] Popped item is 85 print("Elements in Stack $") Stack after POP [25, 75, 33] print(s) want to delete more item (yin) : y while(ans == "y"): Popped item is 33 Stack after POP [25, 75] if(s == []): want to delete more item (y/n) : y print("\n\tEmpty Stack Underflow!!!!!!") break Popped item is_75 else: Stack after POP [25] print("\nPopped item is ",S.pop()) want to delete more item (y/n) : y print("Stack after POP ",S) Popped item is 25 Stack after POP [] ans = input("want to delete more item (y/n):") | want to delete more item (y/n) : y Empty Stack Underflow!!!!1! ("Al stack elements" in euoptn All stack elements for x in range(len(S)-1,-1,-1) print(S[x],end =") ts in stack S and then show all stack. Then ask the the newly created 16.Write a program in Python to create stack name “employee” having employee code and name of 03 employees. Then Insert the details of one more employee to the stack, (PUSH Operation). #Stack - PUSH Employee employee = [] def push_emp(): employee = {('101', 'Kamal’), ('102', 'Rajesh’),('103','kush’)] print("Original Stack\n",employee) [Original Stack empid = input("Enter employee id: ") [(101", ‘Kamat’, (102, Enter employee id : 555 ename = input("Enter employee name : ") mre Enter employee name : anil emp = (empid, Employee successfully added to stack employee.append(emp) print("Employee successfully added to stack") |stack after adding employee Rajesh’), ('103', ‘kush’)] print() ('555', ‘anil’) ('103', ‘kush') print("Stack after adding employee") ('102", 'Rajesh’) x= len(employee) (101, "Kamal') while(x>0): print(employee[x-1]) =x-l push_emp() 17, Write a program in Python to create stack name “employee” having employee code and name of 03 employees. Then remove the details of one employee from the stack. (POP Operation) #Stack - POP Employee (Delete employee) employee =[] def pop_emp(): employee = [('101', Kamal’), ('102’, 'Rajesh’),('103','kush’)] print("Original Stack\n" employee) [Original Stack {('101', 'Kamal’), ('102', 'Rajesh’), ('103", 'kush’)] iffemployee == (I): Deleted items : 103 kush print("No employee Exists - Empty Stack") else: Employee after deletion empid, ename = employee.pop() 102", Rajesh’) print("Deleted items : ",empid, ename) [('101", 'Kamal') print() print("Employee after deletion") x= len{employee) while(0): print(employeefx-1}) a pop_emp() after PUSH and POP operation. #Stack - PUSH and POP BOOK BOOK = [('CS','99}, (Phy’, '84°)] def push_book() print("Original Stack\n", BOOK) Bname = input("Enter Book Name Bpri jput("Enter Book Price : " newbook = (Bname, Bprice) BOOK append(newbook) print("New Book successfully added to stack") print) print("Book after PUSH") x =len(BOOK) while(x>0) print(BOOK[x-1]) x=Xx-l def pop_book() print(*\nPresent Stack elements \n", BOOK) if(BOOK == []) print(”No Book exists - Empty Stack") else: Bname, Bprice = BOOK .pop() print("Deleted items : ",Bname, Bprice) print() print("Book after POP") x =len(BOOK) while(x>0) print(BOOK[x-1]) x=x-l push_book() pop_book() 18, Write a program in python to create stack BOOK having details of 02 books. Write the code to insert a new book (PUSH) and delete a book (POP) and show the details of book Original Stack ((CS', '99"), (‘Phy’, '84')] Enter Book Name : HINDI Enter Book Price : 999 New Book successfully added to stack Book after PUSH (HINDI, 999") Phy’, '84') (cs',"99') Present Stack elements [(CS', '99'), ‘Phy’, '84'), (HINDI, '999")] Deleted items : HIND! 999 Book after POP (Phy’, '84') ((CS','99')

You might also like