0% found this document useful (0 votes)
2 views3 pages

Python File Handling NumPy Matplotlib Full Answers

The document contains various Python programming examples, including file handling, data analysis with NumPy and Matplotlib, exception handling, and CSV file operations. It demonstrates how to copy files, analyze temperature data, handle exceptions with try-except blocks, and create visualizations. Additionally, it explains file modes, user input handling, and the differences between write() and writelines() methods.

Uploaded by

ghemanth23456
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)
2 views3 pages

Python File Handling NumPy Matplotlib Full Answers

The document contains various Python programming examples, including file handling, data analysis with NumPy and Matplotlib, exception handling, and CSV file operations. It demonstrates how to copy files, analyze temperature data, handle exceptions with try-except blocks, and create visualizations. Additionally, it explains file modes, user input handling, and the differences between write() and writelines() methods.

Uploaded by

ghemanth23456
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

1.

Program to copy contents of one text file into another

with open("[Link]", "r") as f1:


with open("[Link]", "w") as f2:
[Link]([Link]())
print("File copied successfully")

2. Temperature analysis using NumPy and Matplotlib

import numpy as np
import [Link] as plt

temps = [Link]([30, 32, 31, 29, 28, 27, 30, 31, 33, 34])
mean_temp = [Link](temps)
print("Mean Temperature:", mean_temp)

days = [Link](1, 11)


[Link](days, temps)
[Link]("Day Number")
[Link]("Temperature")
[Link]("City Temperature Over 10 Days")
[Link]()

3. Purpose of finally block

The finally block always executes whether an exception occurs or not. It is mainly used for cleanup operations.

try:
f = open("[Link]", "r")
print([Link]())
except FileNotFoundError:
print("File not found")
finally:
print("Execution completed")

4. NumPy array slicing

import numpy as np
arr = [Link]([10, 20, 30, 40, 50, 60])

print(arr[1:4])
print(arr[:3])
print(arr[::2])

5. CSV file explanation

CSV stands for Comma Separated Values. Python processes CSV files using the csv module.

import csv
with open("[Link]", "r") as file:
reader = [Link](file)
for row in reader:
print(row)

6. Read file line by line


with open("[Link]", "r") as file:
for line in file:
print(line, end="")

7. Write user input to a file

with open("[Link]", "w") as file:


data = input("Enter some text: ")
[Link](data)

8. File modes

Read mode (r) reads file, Write mode (w) overwrites file, Append mode (a) adds data.

open("[Link]", "r")
open("[Link]", "w")
open("[Link]", "a")

9. Store student details in CSV

import csv
with open("[Link]", "w", newline="") as file:
writer = [Link](file)
[Link](["Roll No", "Name", "Marks"])
[Link]([1, "Alice", 85])
[Link]([2, "Bob", 90])

10. try-except block

try:
x = int(input("Enter a number: "))
print(10 / x)
except ZeroDivisionError:
print("Cannot divide by zero")

11. Creating NumPy arrays

import numpy as np
arr1 = [Link]([1, 2, 3, 4, 5])
arr2 = [Link](1, 11)
print(arr1)
print(arr2)

12. Bar graph using Matplotlib

import [Link] as plt


subjects = ["Math", "Science", "English"]
marks = [85, 90, 78]

[Link](subjects, marks)
[Link]("Subjects")
[Link]("Marks")
[Link]("Student Marks")
[Link]()
13. Histogram plotting

import [Link] as plt


data = [10, 20, 20, 30, 40, 40, 40, 50]

[Link](data)
[Link]("Values")
[Link]("Frequency")
[Link]("Histogram Example")
[Link]()

14. Menu-driven file program

choice = int(input("[Link] [Link] [Link]: "))

if choice == 1:
open("[Link]", "w").close()
print("File created")
elif choice == 2:
with open("[Link]", "r") as f:
print([Link]())
elif choice == 3:
subject = input("Enter subject: ")
with open("[Link]", "a") as f:
[Link](subject + "\n")
with open("[Link]", "r") as f:
print([Link]())
else:
print("Invalid choice")

15. write() vs writelines()

write() writes a single string, writelines() writes multiple strings.

with open("[Link]", "w") as f:


[Link]("Hello\n")
[Link](["Python\n", "File Handling\n"])

You might also like