Experiment Python 12018
Experiment Python 12018
AIM: File input/output: Create a program that reads data from a file and writes it to
another file in a diffe format.
THEORY:
CODE:
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, World!\n")
file.write("Welcome to file handling in Python.")
OUTPUT:
EXPERIMENT – 13
AIM: Regular expressions: Create a program that uses regular expressions to
find all instances of a specific pattern in a text file.
THEORY:
Regular expressions (regex) are patterns used to match character
combinations in strings. They provide a flexible and efficient way to search,
replace, and extract text patterns. Regex is widely used in data validation, text
processing, and web scraping.
Common Regex Functions in Python
CODE:
import re
pattern = r"\d{3}-\d{3}-\d{4}"
if match:
else:
OUTPUT:
EXPERIMENT – 14
AIM: Exception handling: Create a program that prompts the user for two numbers
and then divides them, handling any exceptions that may arise.
THEORY:
Exception handling is a crucial feature in programming that allows developers
to handle unexpected errors gracefully, preventing program crashes and
ensuring a smooth user experience. In Python, exceptions occur during
execution when an operation fails, such as dividing by zero or trying to access a
missing file.
CODE:
try:
result = 10 / num
print("Result:", result)
except ZeroDivisionError:
finally:
print("Execution completed.")
OUTPUT:
EXPERIMENT-15
AIM:
GUI programming: Create a program that uses a graphical user interface (GUI) to
allow the user to perform simple calculations.
THEORY:
Graphical User Interface (GUI) programming allows users to interact with
applications using visual elements like buttons, labels, text fields, and menus.
Unlike command-line interfaces, GUIs provide a more intuitive and user-
friendly experience.
CODE:
import tkinter as tk
# Create main window
root = tk.Tk()
root.title("Simple GUI")
# Create a label
label = tk.Label(root, text="Hello, GUI!")
label.pack()
# Run the application
root.mainloop()
OUTPUT:
EXPERIMENT-16
AIM: Web scraping: Create a program that uses a web scraping library to extract data
from a website and then stores it in a database.
THEORY:
Web Scraping in Python
Web scraping is the process of extracting data from websites using automated
scripts. It is commonly used for data analysis, price comparison, news
aggregation, and research purposes. Python provides powerful libraries such as
BeautifulSoup, requests, and Scrapy for web scraping.
CODE:
import requests
url = "https://www.example.com"
response = requests.get(url)
OUTPUT:
EXPERIMENT-17
AIM: Data visualization: Create a program that reads data from a file and then creates
a visualization of that data using a data visualization library.
THEORY:
Data visualization is the graphical representation of data to help identify
patterns, trends, and insights. It is widely used in data analysis, business
intelligence, and scientific research. Python provides several libraries for
visualization, with Matplotlib and Seaborn being the most popular.
Why Use Data Visualization?
CODE:
x = [1, 2, 3, 4, 5]
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
OUTPUT:
EXPERIMENT-18
AIM:
MACHINE LEARNING:Create a program that uses a machine learning library to classify
images based on their content.
THEORY:
CODE:
import numpy as np
# Sample dataset
X = np.array([[1], [2], [3], [4], [5]]) # Independent variable
model = LinearRegression()
model.fit(X, y)
# Make a prediction
predicted_value = model.predict([[6]])
# Visualization
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()
OUTPUT: