0% found this document useful (0 votes)
2 views

Python assignment J3_ex-6 to 10

The document is a Python assignment from A.C. Patil College of Engineering, detailing various experiments and questions related to file handling, exception handling, regular expressions, data visualization with Matplotlib, and GUI development with Tkinter. It includes both short and long questions with answers provided in Python code format. Group members and batch information are also listed at the beginning.

Uploaded by

aimstudy565
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python assignment J3_ex-6 to 10

The document is a Python assignment from A.C. Patil College of Engineering, detailing various experiments and questions related to file handling, exception handling, regular expressions, data visualization with Matplotlib, and GUI development with Tkinter. It includes both short and long questions with answers provided in Python code format. Group members and batch information are also listed at the beginning.

Uploaded by

aimstudy565
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

A.C.

Patil College of Engineering,


Kharghar, Navi Mumbai – 410210

PYTHON ASSIGNMENT
Group members are:
1. Maisa Praneetkumar Shankar
2. Mane Pra kesh Sulakshan
3. Mardhekar Pratham Vijay
4. Matkar Pankaj Rajaram
5. Mavale Sanket Fakkad
6. Mhatre Shital Budhaji
7. Mirpagar Vikas Anil
8. Mohammad Affan Aijaz
9. Mokashi Atharva Sachin

DIVISION- A
BATCH – J3
Experiment 6
Short Ques ons:
1. What are the different file modes in Python?
Answer- 'r' (Read), 'w' (Write), 'a' (Append), 'x' (Exclusive crea on), 'b' (Binary mode), 't'
(Text mode).
2. How do you open a file in Python?
Answer- python
CopyEdit
file = open("filename.txt", "r") # Opens file in read mode
3. What is the purpose of the read() func on in file handling?
Answer- It reads the content of a file as a string.
python
CopyEdit
content = file.read()
4. How do you write data to a file in Python?
Answer- python

CopyEdit
file = open("file.txt", "w")
file.write("Hello, World!")
file.close()
5. What is the advantage of using with open() in Python?
Answer-It ensures the file is automa cally closed a er execu on, preven ng memory
leaks.
python
CopyEdit
with open("file.txt", "r") as file:
content = file.read()
Long Ques ons :
1. Difference between read(), readline(), and readlines()
Answer-
o read(): Reads the en re file as a string.
o readline(): Reads one line at a me.

o readlines(): Reads all lines into a list.


python
CopyEdit
with open("sample.txt", "r") as file:
print(file.read()) # Reads the en re file
print(file.readline()) # Reads the first line
print(file.readlines()) # Reads all lines into a list
2. Python program to count the number of words in a file
Answer- python
CopyEdit
with open("sample.txt", "r") as file:
content = file.read()
words = content.split()
print("Number of words:", len(words))

Experiment 7
Short Ques ons:
1. What is an excep on in Python?
Answer- An error that occurs during program execu on, disrup ng normal flow.
2. Difference between try-except and try-finally
Answer-
o try-except: Handles specific excep ons.
o try-finally: Ensures execu on of the finally block regardless of excep ons.
3. Purpose of raise keyword
Answer- Used to manually raise an excep on.
python
CopyEdit
raise ValueError("This is an error message")
4. Example of a built-in excep on

Answer- python
CopyEdit
try:
x=1/0
except ZeroDivisionError:
print("Cannot divide by zero")
5. Handling mul ple excep ons
Answer- python
CopyEdit
try:
x = int("hello")
except (ValueError, TypeError) as e:
print("Error:", e)
Long Ques ons:
1. Types of Excep ons in Python
Answer-
o ZeroDivisionError: Division by zero.
o FileNotFoundError: File not found.
o ValueError: Invalid value for an opera on.
o TypeError: Unsupported opera on between different data types.
python
CopyEdit
try:
x = int("abc") # Raises ValueError
except ValueError as e:
print("ValueError occurred:", e)
2. Program to handle division by zero and file not found
Answer- python
CopyEdit

try:
num = int(input("Enter a number: "))
result = 10 / num
except ZeroDivisionError:
print("Cannot divide by zero")
except FileNotFoundError:
print("File not found")

Experiment 8
Short Ques ons:
1. What is a regular expression in Python?
Answer- A sequence of characters that define a search pa ern.
2. Which module handles regular expressions in Python?
Answer- re module.
3. What does re.match() do?
Answer- Checks if the pa ern matches only at the beginning of a string.
4. Finding all occurrences of a pa ern
Answer- python
CopyEdit
import re
print(re.findall(r"\d+", "My number is 12345"))
5. Purpose of \d and \s
Answer-
o \d: Matches digits (0-9).
o \s: Matches whitespace.
Long Ques ons:
1. Difference between match(), search(), and findall()
Answer-
o match(): Checks if the string starts with a pa ern.

o search(): Searches for the first occurrence.


o findall(): Finds all matches.
python
CopyEdit
import re
text = "Hello 123, welcome to 456 Python 789"
print(re.match(r"\d+", text)) # None
print(re.search(r"\d+", text)) # Matches 123
print(re.findall(r"\d+", text)) # ['123', '456', '789']
2. Program to extract email addresses from a file
Answer- python
CopyEdit
import re
with open("emails.txt", "r") as file:
content = file.read()
emails = re.findall(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", content)
print("Extracted Emails:", emails)

Experiment 9
Short Ques ons :
1. What is Matplotlib?
Answer- A Python library for data visualiza on.
2. Func on to create a simple plot
Answer- python
CopyEdit
plt.plot(x, y)
3. Labeling axes
Answer- python
CopyEdit

plt.xlabel("X-axis")
plt.ylabel("Y-axis")
4. Func on to display a graph
Answer- python
CopyEdit
plt.show()
5. Adding a tle to a graph
Answer-
python
CopyEdit
plt. tle("My Graph")
Long Ques ons:
1. Types of Plots in Matplotlib
Answer-
o Line Plot: plt.plot(x, y)
o Bar Chart: plt.bar(x, y)
o Histogram: plt.hist(data)
o Sca er Plot: plt.sca er(x, y)
2. Python Program for Line Plot
Answer- python
CopyEdit
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]

plt.plot(x, y, marker='o', linestyle='--', color='r')


plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt. tle("Line Plot Example")

plt.grid()
plt.show()

Experiment 10
Short Ques ons:
1. What is Tkinter?
Answer- A built-in Python library for crea ng GUI applica ons.
2. Func on to create a root window
Answer- python
CopyEdit
root = Tk()
3. Adding a bu on in Tkinter
Answer- python
CopyEdit
Bu on(root, text="Click Me")
4. Func on to start the GUI loop
Answer- python
CopyEdit
root.mainloop()
5. Use of grid() in Tkinter
Answer- Arranges widgets in a grid format.
Long Ques ons:
1. Different Widgets in Tkinter
Answer-
o Label: Label(root, text="Hello")
o Bu on: Bu on(root, text="Click Me")
o Entry: Entry(root)
2. Python Program for a Simple Login Form
Answer- python

CopyEdit
from tkinter import *
root = Tk()
root.geometry("300x200")
Label(root, text="Username").grid(row=0)
Entry(root).grid(row=0, column=1)
Label(root, text="Password").grid(row=1)
Entry(root, show="*").grid(row=1, column=1)
Bu on(root, text="Login").grid(row=2, column=1)
root.mainloop()

You might also like