0% found this document useful (0 votes)
10 views16 pages

Xii-Cs Practical Programs 7 To 12-1

PRACTICAL PROGRAMS

Uploaded by

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

Xii-Cs Practical Programs 7 To 12-1

PRACTICAL PROGRAMS

Uploaded by

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

PROGRAM 7: WRITE A PYTHON PROGRAM TO CREATE A BINARY FILE WITH ROLL

NUMBER, NAME AND MARKS AND UPDATE THE MARKS USING THEIR ROLL NUMBERS.
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: Create a User defined function write() and open the file in write mode.

2.1 create an empty list inside write() and use while loop with condition as True

2.2 Get the no.of students as an input from the user

2.3 Take the student data as input from user using for loop and

Step 3: Create a list having student data that is inserted using for loop in step:2 and to existing list
using append()

Step 4: Based on the choice of input from the user ,loop either continues to work or stops its
Execution.

Step5: Save this list of students in binary file using dump method

file=open(“Binaryfilename.dat”,”wb”)

pickle.dump(list_of_students, file)

Step 6: Create a user defined function read() to display the information from the file.

Step 7: Create a user defined function update() and open the file in rb+(read and write)mode and
Use load() method to get the list_of_students object that we have stored in step 5

file=open(“Binaryfilename.dat”,”rb+”)

List_of_students=pickle.load(file)

Step 8: Initialize a variable found=0.

8.1 Get the input from the user for rollno.

8.2 Use for loop to iterate the file contents.

8.3 Place an if condition to check for rollno in the file matches with the input provided by the
user,if it matches then current marks can be modified with new marks.

8.4 After updating,reassign variable found=1

Step 9: Use f.seek(0) to place the file pointer at the start of the file and dump() to update the
content in the file.

Step10: call write (), read (), update () and read() functions

Step11:Stop
Program:
Output:

Result:

Thus the Python program to create a binary file with roll number, name and
marks andupdate the marks using their roll numbers is executed successfully and the
output is verified.
PROGRAM:8 Write a Python program to remove all the lines that contain the
character ‘a’in a file and write it to another file.
Aim:

To write a Python program to remove all the lines that contain the character ‘a’ in a 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 () method

Step 8: Stop

Sample Text File

Program
Output
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.
PROGRAM:9 Write a Python program for random number generation that
generatesrandom numbers between 1 to 6 (simulates a dice).
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:
Output

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.
PROGRAM NO:10 Write a Python program to create a CSV file with name and roll number
and perform search for a given roll number and display the name.
Aim:

To write a Python program to create a CSV file with name and roll number and perform
search for a given roll number and display the name.

Algorithm:

STEP 1: Start

STEP 2: Create a user defined function write()and open the file in write mode along with newline
argument.

2.1 Create an empty list inside write() and use while loop with condition as True.

2.2 Get the no.of records to be inserted into file as input from the user.

2.3 Take the Student data as input from user and add it to empty list using append()

STEP 3 : Based on the choice of input from the ,loop either continues to work or stops its Execution.

STEP 4: Save this list of students in csv file using writerows()

STEP 5: Create a user defined functionsearch() and initialize a variable found=0

STEP 6:Open the file in read mode and use reader() method to get the list_of_students object that
we have stored in step 4

f=open(“student.csv”,”w”,newline=””)

Sreader=csv.reader(f)

STEP 7:Search into the list_of_students for given name and read the name from user through input()

STEP 8:To read the contents from the csv file,reader()is used along with file handle object as
parameter.

STEP 9:Use for loop to read the content of csv file if the name entered by the name matches with
the name in the csv file then print the details, increment found=1 and use break statement to end
the execution of loop.

STEP 10: Create a use defined function read() to display the information from the file.

STEP 11: Make a function call to perform read(), write() and search().

STEP 12:STOP
Program:
Output:

Result:

Thus, the Python program to create a CSV file with name and roll number and perform
search for a given roll number and display the name is executed successfully and the output is
verified.
PROGRAM 11: CREATE A CSV FILE BY ENTERING USER-ID AND PASSWORD, READ AND
SEARCH THE PASSWORD FOR GIVEN USERID
Aim:

To create a csv file by entering user-id and password, read and search the password for
given userid.

Algorithm:

Step 1: Start

STEP 2: Create a user defined function write()and open the file in write mode along with newline
argument.

2.1 Create csv writerobject and write the heading userid and password using writerow()
function.

2.2 Use while loop with condition as True.

2.2 Get the no.of records to be inserted into file as input from the user.

2.3 Take the userid and password data as input from user and add it to the variable data.

STEP 3 : Based on the choice of input from the ,loop either continues to work or stops its Execution.

STEP 4: Save this list of userid and password details in csv file using writerow()

STEP 6: Create a use defined function read() to display the information from the file.

STEP 7: Create a user defined function search() and initialize a variable found=0

STEP 8: Open the file in read mode and use reader() method to get the list of userid and password
object that we have stored in step 4

STEP 9: Search into the list for given userid and read the userid from user through input()

STEP 10: To read the contents from the csv file, reader()is used along with file handle object as
parameter.

STEP 11: Use for loop to read the content of csv file, if the userid entered by the user matches with
the userid in the csv file then print the password details, increment found=1 and use break
statement to end the execution of loop.

STEP 12: Make a function call to perform write(),read() and search().

STEP 13: Stop.


Program:
Output:

Result:

Thus,Python program to create a csv file by entering user-id and password, read and search
the password for given userid is executed and verified successfully.
PROGRAM 12: WRITE A PYTHON PROGRAM TO KNOW THE CURSOR POSITION AND PRINT
THE TEXT ACCORDING TO BELOW-GIVEN SPECIFICATIONS:
a. Print the initial position

b. Move the cursor to 4th position


c. Display next 5 characters
d. Move the cursor to the next 10 characters

e. Print the current cursor position


f. Print next 10 characters from the current cursor position
Aim:

To write a Python program to know the cursor position and print the text according to given
specifications.

Algorithm:

Step 1: Start

Step 2: define the function curpos()

2.1 Open the file ‘intro.txt’ in read mode with file handle f.

2.2 Print the initial cursor position by using tell() function.

2.3 Move the cursor to 4th position by using f.seek(4,0)

2.4 Print the next 5 characters by using read() function.

2.5 Move the cursor position to the next 10 characters w.r.t current position by using seek()

Function

2.6 Print the current cursor position by using tell() function


2.7 Print the next 10 characters from the current cursor position using read() function.

Step 3: Stop

Sample Text File:


Program:

Output:

Result:

Thus, Python program to know the cursor position and print the text according to given
specifications are executed and verified successfully.

You might also like