Professional Documents
Culture Documents
def showGrades(S):
grade = "A"
grade = "B"
else:
grade = "C"
print(f"{name} {grade}")
S = {"AMIT": [92, 86, 64], "NAGMA": [65, 42, 43], "DAVID": [92, 90, 85]}
showGrades(S)
This function iterates through the dictionary `S`, calculates the average score for each student, and
assigns a grade based on the given rules. Finally, it prints the student's name along with their
corresponding grade.
result = ''
if (i + 1) % N == 0:
result += '_'
else:
result += char
return result
# Test the function
word = "TELEVISION"
N=3
N=4
This function iterates through each character in the word and replaces every Nth character with an
underscore. Then it returns the modified string.
23(a)Here are the Python commands to convert the tuple to a list and delete the last element:
# Given tuple
subject_list = list(subject)
del subject_list[-1]
print(subject_list)
Output:
Explanation:
1. `list(subject)` converts the tuple `subject` to a list and assigns it to the variable `subject_list`.
try:
content = file.read()
# Split the content into sentences based on '.', '?', and '!'
sentences = content.split(".")
sentences = [sentence.strip() for sentence in sentences if sentence.strip()] # Remove empty string
s
# If there are no sentences ending with '.', try splitting based on '?'
if not sentences:
sentences = content.split("?")
if not sentences:
sentences = content.split("!")
except FileNotFoundError:
# Call the function to display the content of the file in separate lines
showInLines()
This function reads the content of the file "STORY.TXT", splits it into sentences based on '.', '?', and '!',
and then displays each sentence on a separate line. If the file is not found, it prints a message indicating
the same.
28(b) here's a Python function `c_words()` that counts and displays the number of uppercase and
lowercase alphabets in a text file named "Words.txt":
def c_words():
try:
content = file.read()
uppercase_count = 0
lowercase_count = 0
if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1
except FileNotFoundError:
# Call the function to count and display the number of uppercase and lowercase alphabets
c_words()
This function reads the content of the file "Words.txt", counts the number of uppercase and lowercase
alphabets separately, and then displays the counts. If the file is not found, it prints a message indicating
the same.
class Stack:
def __init__(self):
self.items = []
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
if len(str(num)) >= 5:
BigNums.push(num)
def PopBig(BigNums):
print(BigNums.pop())
else:
print("Stack Empty")
# Example usage:
Nums = [213, 10025, 167, 254923, 14, 1297653, 31498, 386, 92765]
BigNums = Stack()
PushBig(Nums, BigNums)
PopBig(BigNums)
This code defines a `Stack` class to implement a stack data structure. Then, it defines `PushBig()` and
`PopBig()` functions as described. Finally, it demonstrates the usage of these functions with an example
list `Nums`.
import csv
def Add_Device():
try:
writer = csv.writer(file)
except FileNotFoundError:
try:
count = 0
reader = csv.reader(file)
count += 1
except FileNotFoundError:
# Example usage:
Add_Device()
Count_Device()
Explanation:
- `Add_Device()` function prompts the user to enter details for a peripheral device (ID, name, and price)
and appends this record to the "Peripheral.csv" file using the `csv.writer` module.
- `Count_Device()` function opens the "Peripheral.csv" file, reads each record using `csv.reader`, and
counts the number of peripheral devices with a price less than 1000.
- In both functions, exception handling is used to catch the `FileNotFoundError` if the "Peripheral.csv" file
is not found.
34.(A) Here's how you can implement the `Copy_new()` function to achieve the described task:
import pickle
def Copy_new():
try:
# Open items.dat for reading in binary mode
while True:
try:
record = pickle.load(file)
pickle.dump(record, new_file)
except EOFError:
except FileNotFoundError:
# Example usage:
Copy_new()
Explanation:
- The `Copy_new()` function opens the binary file "items.dat" for reading and "new_items.dat" for
writing in binary mode.
- For each record, it unpickles the record, checks if the amount is greater than 1000, and if so, pickles
and writes the record to "new_items.dat".
- The process continues until the end of the file is reached (EOFError).
34(a) here how you can implement the `disp_Detail()` function to achieve the described task:
import struct
def disp_Detail():
try:
while True:
record = file.read(16)
if not record:
break
# Unpack the binary data into individual fields (Emp_Id, Name, Salary)
name = name.decode().strip()
except FileNotFoundError:
# Example usage:
disp_Detail()
Explanation:
- The `disp_Detail()` function opens the binary file "EMP.DAT" for reading in binary mode.
- The employee name is decoded from bytes to string and trailing spaces are stripped.
- If the salary is below 25000, the details of the employee (ID, name, and salary) are displayed.