0% found this document useful (0 votes)
5 views9 pages

Python

The document outlines the design and implementation of three Python projects: a contact management system using lists, dictionaries, and file handling; a program to search for patterns in a text file using regular expressions; and a script for performing matrix operations with NumPy. Each project includes a clear aim, description, and sample code demonstrating the functionality. The contact management system allows users to add, view, update, and delete contacts, while the other two projects focus on pattern matching and matrix calculations.

Uploaded by

iamroopa21
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)
5 views9 pages

Python

The document outlines the design and implementation of three Python projects: a contact management system using lists, dictionaries, and file handling; a program to search for patterns in a text file using regular expressions; and a script for performing matrix operations with NumPy. Each project includes a clear aim, description, and sample code demonstrating the functionality. The contact management system allows users to add, view, update, and delete contacts, while the other two projects focus on pattern matching and matrix calculations.

Uploaded by

iamroopa21
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

11.

Design a Python project that integrates lists, dictionaries, and file


handling to create a simple contact management system that can add,
view, update, and delete contacts, with data stored in a file.

Aim: To create a simple Contact Management System in Python using lists, dictionaries, and
file handling to add, view, update, and delete contacts, without using exception handling.

Description: This program stores contact information (Name, Phone, Email) using: List
→ Holds multiple contacts Dictionary → Stores each contact as key–value pairs File
Handling → Saves and loads contacts from a text file ([Link]) The program performs: 1.
Add a contact 2. View contacts 3. Update a contact 4. Delete a contact

Program:

def load_contacts(filename):
contacts = []
try:
with open(filename, 'r') as file:
lines = [Link]()
for line in lines:
name, phone, email = [Link]().split(',')
[Link]({'Name': name, 'Phone': phone, 'Email': email})
except FileNotFoundError:
pass
return contacts
def save_contacts(filename, contacts):
with open(filename, 'w') as file:
for contact in contacts:
[Link](f"{contact['Name']},{contact['Phone']},{contact['Email']}\n")

def add_contact(contacts):
name = input("Enter Name: ")
phone = input("Enter Phone: ")
email = input("Enter Email: ")
[Link]({'Name': name, 'Phone': phone, 'Email': email})
print("Contact added successfully!")
def view_contacts(contacts):
if not contacts:
print("No contacts found.")
else:
print("\nContacts List:")
for i, contact in enumerate(contacts, start=1):
print(f"{i}. Name: {contact['Name']}, Phone: {contact['Phone']}, Email:
{contact['Email']}")
print()
def update_contact(contacts):
name = input("Enter the name of the contact to update: ")
for contact in contacts:
if contact['Name'].lower() == [Link]():
contact['Phone'] = input("Enter new Phone: ")
contact['Email'] = input("Enter new Email: ")
print("Contact updated successfully!")
return
print("Contact not found.")
def delete_contact(contacts):
name = input("Enter the name of the contact to delete: ")
for i, contact in enumerate(contacts):
if contact['Name'].lower() == [Link]():
[Link](i)
print("Contact deleted successfully!")
return
print("Contact not found.")
def main():
filename = "[Link]"
contacts = load_contacts(filename)

while True:
print("\n--- Contact Management System ---")
print("1. Add Contact")
print("2. View Contacts")
print("3. Update Contact")
print("4. Delete Contact")
print("5. Exit")

choice = input("Enter your choice (1-5): ")

def main(): filename = "[Link]" contacts = load_contacts(filename)

while True:
print("\n--- Contact Management System ---")
print("1. Add Contact")
print("2. View Contacts")
print("3. Update Contact")
print("4. Delete Contact")
print("5. Exit")

choice = input("Enter your choice (1-5): ")

if choice == '1':
add_contact(contacts)
elif choice == '2':
view_contacts(contacts)
elif choice == '3':
update_contact(contacts)
elif choice == '4':
delete_contact(contacts)
elif choice == '5':
save_contacts(filename, contacts)
print("Contacts saved. Exiting program.")
break
else:
print("Invalid choice. Please try again.")

if name == " main ":

main()
Output:
[Link] a Python program that uses functions, loops, and regular
expressions to search through a text file for lines matching a specific
pattern, such as IP addresses or URLs, and output the results.

Aim: To write a Python program that uses functions, loops, and regular expressions (regex)
to search a text file and extract lines that match specific patterns such as IP addresses or
URLs.

Description:
This program demonstrates how to:
Use regular expressions (re module) to match patterns in a text file
Use functions for modular coding
Use loops to read through each line of a file
Detect and extract patterns such as:
o IP addresses (e.g., [Link])
o URLs (e.g., [Link]

Program:

import re

def search_pattern(filename, pattern_type):

results = []

if pattern_type == 'ip':

pattern = r'\b(?:[0-9]{1,3}.){3}[0-9]{1,3}\b'

elif pattern_type == 'url':

pattern = r'https?://[^\s]+'

else:

print("Invalid pattern type. Choose 'ip' or 'url'.") return results

with open(filename, 'r') as file:

for line in file:

if [Link](pattern, line):
[Link]([Link]())

return results

def main():

filename = input("Enter the filename to search: ")

pattern_type = input("Enter pattern type to search (ip/url): ").lower()

matched_lines = search_pattern(filename, pattern_type)

if matched_lines:

print(f"\nLines matching {pattern_type} pattern:")

for line in matched_lines:

print(line)

else:

print(f"No lines matching {pattern_type} pattern found.")

if name == " main ":

main()
Output:
[Link] a Python script that uses NumPy to perform matrix operations.

Aim: To develop a Python script that uses the NumPy library to perform basic matrix
operations such as addition, subtraction, multiplication, and transpose.

Description:

This program demonstrates how to:


 Create matrices using NumPy arrays
 Perform matrix operations:
 Matrix Addition
 Matrix Subtraction
 Matrix Multiplication
 Matrix Transpose

Display the results of each operation

Use NumPy for fast and efficient numerical calculations

Program:
import numpy as np
def main():
# Creating two matrices using NumPy
A = [Link]([[1, 2],
[3, 4]])
B = [Link]([[5, 6],
[7, 8]])
print("Matrix A:\n", A)
print("Matrix B:\n", B)
# Matrix Addition
add = A + B
print("\nMatrix Addition:\n", add)
# Matrix Subtraction
sub = A - B
print("\nMatrix Subtraction:\n", sub)
# Matrix Multiplication
mul = [Link](A, B) # OR A @ B
print("\nMatrix Multiplication:\n", mul)
# Matrix Transpose
trans_A = A.T
print("\nTranspose of Matrix A:\n", trans_A)
main()
Output:

You might also like