14) Write a program in python to implement Salary printing file read operation.
(Fileformat: Employee No, name, deptno, basic, DA, HRA, Conveyance) should perform
below operations.
a) Print Salary Slip for given Employee Number
b) Print Employee List for Given Department Number
# Sample file content:
# 101,Alice,1,1000,500,300,100
# 102,Bob,1,1200,600,350,120
def print_salary_slip(employee_no):
with open("employee_data.txt", "r") as f:
for line in f:
details = line.strip().split(',')
if details[0] == str(employee_no):
print(f"Salary Slip for {details[1]}:")
total_salary = float(details[3]) + float(details[4]) + float(details[5]) + float(details[6])
print(f"Total Salary = {total_salary}")
return
print("Employee not found")
print_salary_slip(102)
Output : -
15) Write a program in python to implement Railway Reservation System using file
handling technique. System should perform below operations.
a)Reserve a ticket for a passenger.
b) List information all reservations done for today’s trains.
# Simple mock of a Railway Reservation System
reservations = []
def reserve_ticket(name, train_no, seat_no):
reservations.append((name, train_no, seat_no))
print("Ticket Reserved.")
def show_reservations():
print("Reservations for today:")
for res in reservations:
print(f"Name: {res[0]}, Train No: {res[1]}, Seat No: {res[2]}")
reserve_ticket("Alice", 12, 15)
show_reservations()
Output : -
pg. 8
16) Write a Python program to implement module.
# module.py :
def greet(name):
return f"Hello, {name}!"
# main.py :
import Program_16_module
print(Program_16_module.greet("Divyesh"))
Output : -
17) Write a program which will implement decorators for functions and methods in
python.
def decorator(func):
def wrapper():
print("Before function")
func()
print("After function")
return wrapper
@decorator
def say_hello():
print("Hello!")
say_hello()
Output : -
18) Write a program to read CSV file and generate output using HTML table.
import csv
# Sample CSV content:
# 1,Alice
# 2,Bob
with open("sample.csv", mode="r") as f:
reader = csv.reader(f)
rows = list(reader)
html_output = "<table>"
pg. 9
for row in rows:
html_output += f"<tr><td>{row[0]}</td><td>{row[1]}</td></tr>"
html_output += "</table>"
print(html_output)
Output : -
19) Write a program to process CSV file using CSV module.
import csv
# Sample CSV content:
#name,age
# Alice,21
# Bob,25
with open("sample.csv", mode="r") as f:
reader = csv.DictReader(f)
for row in reader:
print(row)
Output : -
20) Desirable: Write a program to process JSON and XML data.
import json, xml.etree.ElementTree as ET
json_data = '{"name": "Alice", "age": 25}'
parsed = json.loads(json_data)
print(parsed['name'])
xml_data = "<person><name>Alice</name><age>25</age></person>"
root = ET.fromstring(xml_data)
print(root.find('name').text)
Output : -
pg. 10
21) Create Web Database Application “Address Book” with options to
a) add/ insert a record
b) modify a record
c) display a record
d) delete a record
from flask import Flask, request, render_template, redirect, url_for
app = Flask(__name__)
address_book = {}
@app.route('/add', methods=['GET', 'POST'])
def add_record():
if request.method == 'POST':
name = request.form['name']
phone = request.form['phone']
address_book[name] = phone
return redirect(url_for('display'))
return render_template('add.html')
@app.route('/display')
def display():
return render_template('display.html', address_book=address_book)
if __name__ == '__main__':
app.run()
Output : -
Note : How to run :
First Run program file and shows its running
Go to: http://127.0.0.1:5000/add
Fill Name and Phone → Click Add.
It will automatically redirect to /display to show all records!
pg. 11
22) Create Web Database Application “Event Registration” with options to
a) Event Registration
b) Cancel Registration
c) display a record
from flask import Flask, request, render_template
app = Flask(__name__)
registrations = {}
@app.route('/')
def home():
return render_template('register.html')
@app.route('/register', methods=['POST'])
def register():
event = request.form['event']
participant = request.form['participant']
registrations[participant] = event
print(f"Registration successful for {participant} for {event}")
return f"Registration successful for {participant} for {event}"
if __name__ == '__main__':
app.run(debug=True)
Output : -
Note : How to run :
First Run program file and shows its running
Go to: http://127.0.0.1:5000/
Fill Form
Registration successful for Divyesh for Python
pg. 12