0% found this document useful (0 votes)
19 views2 pages

Quiz 5

The document outlines a series of tasks related to file handling in Python, including writing and reading from text files containing fruits, words, cities, and student scores. Each task demonstrates basic file operations such as writing data, reading lines, and processing content to extract information. The final task includes calculating the average score and identifying the top student from the student data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Quiz 5

The document outlines a series of tasks related to file handling in Python, including writing and reading from text files containing fruits, words, cities, and student scores. Each task demonstrates basic file operations such as writing data, reading lines, and processing content to extract information. The final task includes calculating the average score and identifying the top student from the student data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ITC Quiz 5

File handling

Provide the outputs of following tasks.


Task 1:
with open("fruits.txt", "w") as file:
file.write("Mango Orange\nApple Banana\
nGrapes")

with open("fruits.txt", "r") as file: Task 3:


content = file.readlines() with open("cities.txt", "w") as file:
file.write(" New York \n London \n
for line in content: Paris \n")

words = line.split()
print(words[0]) with open("cities.txt", "r") as file:
lines = file.readlines()

for city in lines:


print(city.strip() + "!")

Task 2:
with open("words.txt", "w") as file:
file.write(" Apple \n Banana\n Cherry ")

with open("words.txt", "r") as file:


Task 4:
lines = file.readlines()
with open("students.txt", "w") as file:
file.write("Alice,90\nBob,85\nCharlie,88\
for word in lines: n")
print(word.strip()) with open("students.txt", "r") as file:
lines = file.readlines()

for line in lines:


data = line.strip().split(",")
print(f"Name: {data[0]}, Score: {data[1]}")
ITC Quiz 5
File handling

if score > highest_score:


highest_score = score
top_student = name

# Displaying student details


print("Student Details:")
Task 5:
for student in students:
# Writing data to the file
print(f"Name: {student[0]}, Score:
with open("students.txt", "w") as file: {student[1]}, Subject: {student[2]}")
file.write("Alice,90,Math\nBob,85,Science\
nCharlie,88,English\nDavid,92,History\n")
# Calculating and displaying the average score
average_score = total_score / len(students)
# Reading data from the file
print(f"\nAverage Score: {average_score:.2f}")
with open("students.txt", "r") as file:
lines = file.readlines()
# Displaying the student with the highest score
print(f"Top Student: {top_student} with Score:
students = [] {highest_score}")
total_score = 0
highest_score = 0
top_student = ""

# Processing each line


for line in lines:
data = line.strip().split(",") # Splitting by
comma
name, score, subject = data[0], int(data[1]),
data[2] # Convert score to int
students.append((name, score, subject)) #
Store as tuple

total_score += score # Accumulate total


score

# Check for highest score

You might also like