0% found this document useful (0 votes)
67 views11 pages

Computer Science PDF CC

Computer science class 12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
67 views11 pages

Computer Science PDF CC

Computer science class 12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
1. Write a Python program to read a text file and display the number of vowels/consonants/uppercase/lowercase characters in the file Aim: To write Python program to read a text file and display the number of vowels/ consonants/ uppercase/ lowercase characters in the file. Algorithm: ‘Step 1: Start Step 2: Open text file in read mode using openQ) method Step 3: Read the content of the file using read() method ‘Step 4:Write the logic for counting the Vowels,Consonants,Upper case letters,Lower case letters using islower(),isupper(),ch in ['a’/e’/i'/0'/u'], chin ['b''c'/d''f,'2''h', 7k ,'m' npg’ r's)'t,Vi'w' xy) 2'] Step 5: Close the file using close() method Step 6: Print the Counting data. Step 7: Stop Program: file=open("AI.TXT","r") content=file.read() vowels=0 consonants=0 lower_case_letters=0 upper_case_letters=0 for ch in content: if(ch.islower()): lower_case_letters+=1 elif(ch.isupper()): upper_case_letters+=1 ch=ch.lower() if (ch in ['a‘'e',' vowels+=1 elif(ch in ['b consonants+=1 file.close(Q) print("Vowels are :",vowels) print("Consonants :",consonants) print("Lower_case_letters :",lower_case_letters) print("Upper_case_letters :",upper_case_letters) Output: Sample Text File — a Al - Notepad File Edit Format View Help AI refers to Artificial Intelligence. It is the intelligence of machines. With AI, machines can perform reasoning and problem solving. Al is the simulation of human intelligence by machines. It is the fastest growing development in the world of technology. Python Program Executed Output: In [50]: runfile('C:/Users/ Administrator/.spyder-py3/untitledl.py', wdir='C: /Users/Administrator/.spyder-py3') Vowels are : 83 Consonants : 128 Lower_case_letters : 200 Upper_case_letters : 11 Result: Thus the Python program to read a text file and display the number of vowels/consonants/uppercase/lowercase characters in the file is executed successfully and the output is verified. 2. Write a Pyth binary file with roll numb and perform search based on roll number. Aim: To write Python Program to create a binary file with name and roll number and perform search based on roll number. Algorithm: Step 1: Start Step 2; Take the Student data in input from user using for loop Step 3: Prepare a list of dictionary having student data that is inserted using for loop in step:2 list-of_students Step4: Save this list of students in binary file using dump method file=open(“pathoffile’,"wb"),pickle.dump/(list_of_students,file) Step 5; Read the rollno from user to search Step 6: open the file in read mode and use load () method to get the list_of_students object that we have stored in step4 file=open (“pathfile”,"rb”) list_of_students =pickle.load(file) Step 7: Search into the list_of_students for given rollno Step 8: Stop Program: #Create a binary file with name and roll number import pickle stud_data={} list_of_students=[] no_of_students=int(input("Enter no of Students:")) for iin range(no_of_students): stud_data["roll_no”}=int(input("Enter roll no:")) stud_data["name"]=input("Enter name: ") list_of_students.append(stud_data) stud_data={} file=open(“StudDtl.dat","wb") pickle.dump(list_of_students,file) print("Data added successfully”) file.close() #Search for a given roll number and display the name, if not found display appropriate message.” import pickle file=open("StudDtl.dat","rb") list_of_students=pickle.load(file) roll_no=int(input("Enter roll no.of student to search:")) found=False for stud_data in list_of_students: if(stud_data["roll_no"} found=True roll_no): print(stud_data["name"],"found in file.") if (found==False): print("No student data found. please try again") file.close() Python Program Executed Output: an uwege owe REL Se Cee oy uma er ueery sopyurr py Rec3.py', wdir='C:/Users/Administrator/.spyder-py3') Enter no of Students:3 Enter roll no:101 Enter name: Anu Enter roll no:102 Enter name: Bharath Enter roll no:103 Enter name: Charu Data added successfully Enter roll no.of student to search:102 Bharath found in file. Result: Thus the Python program to create a binary file with name and roll number and perform search based on roll number is executed successfully and the output is verified. 3. Write a Python program to create a binary file with roll number, name and marks jate the marks using their roll Aim: To write Python Program to create a binary file with roll number, name and marks and update the marks using their roll numbers. Algorithm: Step 1: Start Step 2: Take the Student data in input from user using for loop Step 3: Prepare a list of dictionary having student data that is inserted using for loop in step:2 list-of students Step4: Save this list of students in binary file using dump method file=open(“pathoffile”,"wb") ,pickle.dump(list_of_students,file) Step 5: Read the rollno from user to search Step 6: Open the file in read mode and use load () method to get the list_of_students object that we have stored in step4 file=open (“pathfile’,’rb") list_of students =pickle.load(file) Step 7: Search into the list_of_studentsfor given rollno and update the marks. Step 8: Print the updated student_data Step 9: Stop Program: #Create a binary file with roll number, name and marks. import pickle student_data={} no_of_students=int(input("Enter no of Students to insert in file :")) file=open("StudData","wb") for iin range(no_of students): student_dataf"RollINo"]=int(input("Enter roll no :")) student_data["Name"]=input("Enter Student Name :") student_data["Marks"]=float(input("Enter Students Marks :")) pickle.dump(student_data,file) student_data={} file.close() print(“data inserted Successfully") import pickle student_data={} found=False roll_no=int(input("Enter the roll no to update marks :")) file=open("StudData","rb+") try: while True: pos=file.tell() student_data=pickle.load(file) if(student_data["RollNo"]==roll_no): student_data["Marks"]=float(input("Enter marks to update: ")) file.seek(pos) pickle.dump(student_data, file) found=True except EOFError: if(found==False): print("Roll no not found") else: print("Students marks updated Successfully") file.close() # print the updated data import pickle student_data={} file=open("StudData","rb") try: while True: student_data=pickle.load(file) print(student_data) except EOFError: file.close() Output DO) Console 3/4 ‘Enter no of Students to insert in file : 3 Enter roll no :101 Enter Student Name :Anitha Enter Students Marks :90 Enter roll no :102 Enter Student Name :Beena Enter Students Marks :85 Enter roll no :103 Enter Student Name : Vidhya Enter Students Marks :75 data inserted Successfully Enter the roll no to update marks :102 Enter marks to update: 88 Students marks updated Successfully {'RollNo': 101, 'Name': ‘Anitha', 'Marks': 90.0} {'RollNo': 102, 'Name': 'Beena', 'Marks': 88.0} {'RollNo': 103, 'Name': 'Vidhya', 'Marks': 75.0} Result: Thus the Python program to create a binary file with roll number, name and marks and update the marks using their roll numbers is executed successfully and the output is verified. oe Lwrite i her fil Aim: To write a Python program to remove all the lines that contain the character ‘a’ ina file and write it to another file. Algorithm: Step 1: Start Step 2: Open text file in read mode using open() method Step 3: Read the content of the file using readlines() method Step 4: Close the file using close() method Step 5: Open 2 different text files in write mode using open() method Step 6: Remove all the lines from the first file that contains character ‘a’ and removed lines copied into another file. Step 7: Close all the files using close 9 method Step 8: Stop Program: file=open("C:\\Users\\Rec\\format.txt","r") lines=file.readlinesQ) file.closeQ) file=open("C:\\Users\\Rec\\format.txt","w") file1=open("C:\\Users\\Rec\\second.txt","w") for line in lines: if‘a' in line or ‘A’ in line: file1.write(line) else: file.write(line) print("Lines containing char ‘a’ has been removed from format.txt file") print("Lines containing char ‘a' has been saved in second.txt file") file.closeQ) file1.close() Output: a format - Notepad = |e) Ex File Edit Format View Help Confidence is the most beautiful thing you can possess. * You must look within yourself to build yourself . A friend in need is a friend indeed. To be cool is to be nice. The secret of getting ahead is getting started. Impressive thoughts are to be pondered. In [4]: runfile('C:/Users/Administrator/.spyder-py3/ untitledl.py', wdir='C:/Users/Administrator/.spyder-py3') Lines containing char 'a' has been removed from format.txt file Lines containing char 'a' has been saved in second.txt file >" =e vi | format - Notepad _* Loh! 98e 8) File Edit Format View Help ] You must look within yourself to build yourself . a To be cool is to be nice. a second - Notepad a =|8| & File Edit Format View Help Confidence is the most beautiful thing you can possess. * A friend in need is a friend indeed. The secret of getting ahead is getting started. Impressive thoughts are to be pondered. Result: Thus the Python program to remove all the lines that contain the character ‘a’ in a file and write it to another file is executed successfully and the output is verified. Aim: To write a Python program for random number generation that generates random numbers between 1 to 6 (simulates a dice). Algorithm: Step 1: Start Step 2: Import random module Step 3: Under while loop write a random number generate that will generate number from 1 to 6 using randint() method. Step 4: Print the random generate number Step 5: Stop Program: import random Output: In [6]: runfile('C:/Users/Administrator/, spyder-py3/untitled2.py', wdir='C:/Users/Administrator/.spyder-py3' ) Enter 'r' to roll dice or press any other key to quit: r 3 Enter 'r' to roll dice or press any other key to quit: r 3 Enter ‘r' to roll dice or press any other key to quit: r 4 Enter 'r' to roll dice or press any other key to quit: r 5 Enter 'r' to roll dice or press any other key to quit: r 4 Enter 'r' to roll dice or press any other key to quit: r 2 Enter 'r' to roll dice or press any other key to quit: q In [7]: Result: Thus the Python program for random number generation that generates random numbers between 1 to 6 (simulates a dice) is executed successfully and the output is verified.

You might also like