0% found this document useful (0 votes)
7 views6 pages

Lab Sheet 1

The document contains a lab sheet for a course on Data Structure and Web Development with Python, featuring six programming problems. Each problem includes a specific task, such as managing a shopping list, storing employee information, managing student grades, creating a bank account class, processing a list of numbers, and developing an inventory management system. The document provides Python code examples for each problem to illustrate the required functionality.

Uploaded by

bhrighugupta
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)
7 views6 pages

Lab Sheet 1

The document contains a lab sheet for a course on Data Structure and Web Development with Python, featuring six programming problems. Each problem includes a specific task, such as managing a shopping list, storing employee information, managing student grades, creating a bank account class, processing a list of numbers, and developing an inventory management system. The document provides Python code examples for each problem to illustrate the required functionality.

Uploaded by

bhrighugupta
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
You are on page 1/ 6

Course Code: CSE3217

Course Title: Data Structure and Web Development with Python


Lab sheet- 1

Problem Statement 1: You are creating a program to manage a shopping


list. Write a Python script to:
 Add items to the shopping list.
 Remove an item.
 Print the total number of items in the list.
Program:
# Shopping list program
shopping_list = []

# Add items to the list


shopping_list.append("Apples")
shopping_list.append("Bananas")
shopping_list.append("Milk")

# Remove an item
shopping_list.remove("Bananas")

# Print the total number of items


print("Shopping List:", shopping_list)
print("Total items:", len(shopping_list))

Problem Statement 2: You are tasked with storing employee information


for a company. Each employee has the following details:
 Employee ID (integer)
 Name (string)
 Department (string).
The data should remain immutable to ensure it is not accidentally modified.
Write a Python script to:
1. Create a tuple for each employee.
2. Store multiple employee tuples in a list.
Display the details of employees in the "Finance" department.
Program:
# Step 1: Create employee tuples
employee1 = (101, "Alice", "Finance")
employee2 = (102, "Bob", "Engineering")
employee3 = (103, "Charlie", "Finance")
employee4 = (104, "Diana", "HR")

# Step 2: Store employee tuples in a list


employees = [employee1, employee2, employee3, employee4]

# Step 3: Display employees in the "Finance" department


print("Employees in the Finance department:")
for emp in employees:
if emp[2] == "Finance": # Check department
print(f"ID: {emp[0]}, Name: {emp[1]}")

Problem Statement 3: Create a program to manage student grades using a


dictionary. Perform the following:
 Add a student and their grade.
 Update the grade of a student.
 Print all students with their grades.
Program:
# Initialize an empty dictionary to store student grades
student_grades = {}

while True:
# Display menu options
print("\nMenu:")
print("1. Add a student and their grade")
print("2. Update the grade of a student")
print("3. Print all students with their grades")
print("4. Exit")

# Get the user's choice


choice = input("Enter your choice: ")

if choice == "1":
# Add a student and their grade
name = input("Enter the student's name: ")
grade = input("Enter the student's grade: ")
if name in student_grades:
print(f"{name} already exists with grade {student_grades[name]}.")
else:
student_grades[name] = grade
print(f"Added {name} with grade {grade}.")

elif choice == "2":


# Update the grade of a student
name = input("Enter the student's name: ")
if name in student_grades:
new_grade = input(f"Enter the new grade for {name}: ")
student_grades[name] = new_grade
print(f"Updated {name}'s grade to {new_grade}.")
else:
print(f"{name} not found in the records.")

elif choice == "3":


# Print all students with their grades
if student_grades:
print("\nStudent Grades:")
for name, grade in student_grades.items():
print(f"{name}: {grade}")
else:
print("No student records found.")

elif choice == "4":


# Exit the program
print("Exiting the program. Goodbye!")
break

else:
print("Invalid choice. Please select a valid option.")

Problem Statement 4: Create a BankAccount class to:


 Deposit money.
 Withdraw money (ensure the balance doesn’t go negative).
 Print the current balance.
Program:
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance

def deposit(self, amount):


self.balance += amount
print(f"Deposited {amount}. New balance: {self.balance}")

def withdraw(self, amount):


if amount > self.balance:
print("Insufficient balance!")
else:
self.balance -= amount
print(f"Withdrew {amount}. New balance: {self.balance}")
def display_balance(self):
print(f"Account owner: {self.owner}, Balance: {self.balance}")

# Usage
account = BankAccount("John Doe", 1000)
account.deposit(500)
account.withdraw(200)
account.display_balance()

Problem Statement 5: Write a function that takes a list of numbers and


returns a tuple containing the minimum, maximum, and average of the
numbers.
Program:
def process_numbers(numbers):
if not numbers:
return None, None, None
min_num = min(numbers)
max_num = max(numbers)
avg = sum(numbers) / len(numbers)
return min_num, max_num, avg

# Usage
numbers = [10, 20, 30, 40, 50]
result = process_numbers(numbers)
print(f"Minimum: {result[0]}, Maximum: {result[1]}, Average: {result[2]:.2f}")

Problem Statement 6: Create an inventory management system using a


dictionary and classes where:
 Each item in the inventory has a name and quantity.
 You can add stock, remove stock, and check the total inventory.
Program:
class Inventory:
def __init__(self):
self.items = {}

def add_item(self, item_name, quantity):


if item_name in self.items:
self.items[item_name] += quantity
else:
self.items[item_name] = quantity
print(f"Added {quantity} {item_name}(s). Total: {self.items[item_name]}")

def remove_item(self, item_name, quantity):


if item_name in self.items and self.items[item_name] >= quantity:
self.items[item_name] -= quantity
print(f"Removed {quantity} {item_name}(s). Remaining:
{self.items[item_name]}")
else:
print(f"Not enough {item_name} in inventory or item does not exist!")

def display_inventory(self):
print("Current Inventory:")
for item, qty in self.items.items():
print(f"{item}: {qty}")

# Usage
inventory = Inventory()
inventory.add_item("Laptop", 10)
inventory.add_item("Mouse", 25)
inventory.remove_item("Laptop", 3)
inventory.display_inventory()

You might also like