0% found this document useful (0 votes)
23 views33 pages

Python Programs for Computer Science XII

Uploaded by

Sarah Kayiti
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)
23 views33 pages

Python Programs for Computer Science XII

Uploaded by

Sarah Kayiti
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

QUEEN MARY’S SCHOOL

PRACTICAL FILE

Academic Session: 2024-25

Roll No.: _______33_______


Name of Student: ____MUGDHA SINGH______
Class: XII - B
Subject: Computer Science
Subject Code: 083
Q1. Write a python program using a function to print factorial number
series from n to m numbers.
Code :-

Output:-
Q2. Write a python program to accept the username "Admin" as the default
argument and password 123 entered by the user to allow login into the
system.

Code :-

Output:-
Q3. Write a python program to demonstrate the concept of variable length
argument to calculate the product and power of the first 10 numbers.
Code :-

Output:-

Q4. Write a program using a function to find whether the given number is
palindrome or not.
Code :-

Output:-
Q5. Write a program using a function to print a Fibonacci series.
Code :-

Output:-

Q6. Create a text file "[Link]" in python and ask the user to write a single
line of text by user input.

Code :-

Output:-
Q7. Write a program to count a total number of lines and count the total
number of lines starting with 'A', 'B', and 'C' from the file [Link].
Code:-

Output:-
Q8. Write a program to replace all spaces from text with - (dash) from the
file [Link].
Code:-

Output:-
Q9. Write a program to know the cursor position and print the text
according to the below-given specifications:
• Print the initial position
• Move the cursor to 4th position
• Display next 5 characters
• Move the cursor to the next 10 characters
• Print the current cursor position
• Print next 10 characters from the current cursor position
Code:-

Output:-
Q10. Write a program to store customer data into a binary file [Link]
using a dictionary and print them on screen after reading them. The
customer data contains ID as key, and name, city as values.

Code:-

Output:-
Q11. Write a program to update a record from [Link] file by its rollno.
Display the updated record on screen.
Code:-

Output:-
Q12. Write a program to write data into binary file [Link] and display
the records of students who scored more than 95 marks.
Code:-

Output:-
Q13. Read a CSV file [Link] and print the contents in a proper format.
The data for [Link] file are as following:
SNo Batsman Team Runs Highest
1 K L Rahul KXI 670 132*
2 S Dhawan DC 618 106*
3 David Warner SRH 548 85*
4 Shreyas Iyer DC 519 88*
5 Ishan Kishan MI 516 99

Code:-

Output:-
Q14. Read a CSV file [Link] and print them with tab delimiter. Ignore first
row header to print in tabular form.
Code:-

Output:-
Q15. Read a CSV file [Link] and print them with tab delimiter. Ignore
first row header to print in tabular form.
Field 1 Data Type
StudentID Integer
StudentName String
Score Integer

Code:-

Output:-
Q16. Write a program to read byte by byte from a file using seek() and tell().
Code:-

Output:-
Q17. Write a program to read and write operations on to a [Link] file having
fields roll number, name, stream and marks.
Code:-
Output:-
Q18. Write a menu driven program to calculate area of different shapes
using functions.
Code:-
Output:-
Q19. Write a menu driven program to perform basic arithmetic calculations using
functions.
Code:-
Output:-
Q20. Write a menu driven to perform basic operations on text file.
Code:-
Output:-
Q21. Write a menu driven program in python which contain student details
in binary file and should have following facilities:
1. Writing student details
2. Display all students’ details
3. Search particular student details
4. Update any student details
5. Delete any student details
6. Exit
Code:-
Output:-
Q22. Write a menu driven to perform basic operations on CSV file
Code:-
Output:-
Q23. Write a program to create a stack called country and to perform the
basic operations on it.
Code:-

Output:-
Q24. Write a program to create a stack called employee and perform the basic
operations on it.
Code:-

Output:-
Q25. Write a program to generate random number between 1 to 6.
Code:-

Output:-

Q26. Write a program to handle multiple exceptions.


Code:-

Output:-

Common questions

Powered by AI

To implement a Python program that calculates both the product and power using variable length arguments, you use the `*args` feature to accept multiple arguments and iterate over them. For the product, initialize a variable to 1 and multiply it by each element in `args`. For power, raise each number to a specific power using a loop. This allows flexibility in operations performed on numbers without predefined limits on input numbers.

To properly read and update records in a CSV or binary file in Python, first, load data with appropriate modules (`csv` for CSV files and `pickle` for binary files). Convert records into dictionaries or custom objects for easier manipulation. For updates, modify the desired record and overwrite the file with the updated data. Proper structuring includes error handling for file operations and ensuring the consistency of data formats across reads and writes.

Implementing a menu-driven program for binary file operations requires defining separate functions for each operation, like adding, displaying, updating, searching, and deleting student records. Use Python's `pickle` module for serialization and deserialization of file data. This approach is important as it provides a structured, interactive way to manage complex data, ensuring user-friendly operation, data integrity, and ease of maintenance.

To read a CSV file and output its content with a tab delimiter in Python, use the `csv` module. Open the file, skip the header row with `next()`, and iterate over the remaining rows. For each row, join the elements with a tab (`\t`) before printing. This method ensures that the data is well-formatted and the header is excluded in the output.

Python's file cursor is manipulated using the `seek()` and `tell()` methods. `seek()` moves the cursor to a specific byte in the file, while `tell()` returns the current position. Positioning the cursor lets you extract specific text segments without reading the entire file, optimizing memory usage and speed. This ability is useful in scenarios requiring partial data processing, like large file operations or buffers.

Default arguments in Python functions provide baseline values, enhancing function flexibility by allowing omissions of certain arguments. In a login system, `username` can be a default such as 'Admin', requiring only the password for full functionality. This reduces user input, simplifies calls in typical cases, and increases function adaptability with minimal coding overhead.

Handling multiple exceptions is critical in file operations to ensure robustness against a range of runtime errors, such as file not found errors or access violations. Implement this using a try-except block where you can catch specific exceptions like `FileNotFoundError`, `IOError`, and `EOFError` separately. This granularity provides precise control over error handling and allows for meaningful error messages or corrective actions.

To replace spaces with dashes in a text file using Python, read the file's content into memory, use the `str.replace(' ','-')` method to substitute spaces with dashes, and write the altered content back to the file. This method is effective because it leverages Python's powerful string operations to perform text manipulation efficiently and is straightforward to implement.

When creating a Fibonacci sequence function in Python, use recursion or iteration. Iteration is typically more efficient as recursion can lead to a stack overflow with large input sizes. For the iterative approach, initialize two variables with the starting values of the sequence and update them iteratively to generate further numbers. Optimal performance includes avoiding duplicate calculations and managing memory effectively.

The stack data structure is significant for implementing operations that require a last-in, first-out (LIFO) approach, useful in scenarios like undo mechanisms, parsing expressions, and backtracking algorithms. In Python, a list can implement a stack using the `append()` and `pop()` methods. For example, for country data, simulate access patterns where the most recently added country's information needs processing first.

You might also like