Python Lab 1
Python Lab 1
ipynb - Colab
keyboard_arrow_down Labsheet 1,
Roll no-20221CAI0108
Name: B M Vineel Eshwar
Section: 6CAI-02
Introduction to lists,tuples,dictionaries,classes
Problem Statement 1: You are creating a program to manage a shopping list ,Write a python script to:
#Shopping list
shopping_list=[]
#Adding items to the list
shopping_list.append("Apples")
shopping_list.append("Bananas")
shopping_list.append("Milk")
#Removing the list elements
shopping_list.remove("Milk")
print(shopping_list)
print(f'The Length of the list',len(shopping_list))
['Apples', 'Bananas']
The Length of the list 2
Problem Statement 2: You are tasked with storing employee information for a company Each emplooyee has the following details:
Employee id(int)
Name(String)
Department(String)
employee1 = (1001,'Manoj','HR')
employee2 = (1002,'Srivathsa','Finance')
employee3 = (1003,'Vineel','HR')
employee4 = (1004,'Varshith','Finance')
employees = [employee1,employee2,employee3,employee4]
for employee in employees:
if employee[2] == 'Finance':
print(f'ID :{employee[0]},Name :{employee[1]}')
ID :1002,Name :Srivathsa
ID :1004,Name :Varshith
Probelm Statement 3:
Create a program to manage student grades using a dictionary .Perfrom the following:
student_grades ={}
while True:
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 grade")
print("4. Exit")
choice = input("Enter your choice (1/2/3/4): ")
if choice == '1':
name = input("Enter the student's name: ")
https://colab.research.google.com/drive/146PNCXL4t4iFkRqhmPIkEnCEOjKml-J6#scrollTo=quaaVoIHiv0C&printMode=true 1/4
2/19/25, 9:27 PM VineelPY.ipynb - Colab
grade = input("Enter the student's grade: ")
if name in student_grades:
print(f"{name} already has a grade of {student_grades[name]}.")
else:
student_grades[name] = grade
print(f"{name}'s grade has been updated to {grade}.")
elif choice == '2':
name = input("Enter the student's name: ")
if name in student_grades:
new_grade = input("Enter the new grade: ")
student_grades[name] = new
print(f"{name}'s grade has been updated to {new_grade}.")
else:
print(f"{name} is not in the student list.")
elif choice == '3':
print("Student Grades:")
for name, grade in student_grades.items():
print(f"{name}: {grade}")
elif choice == '4':
print("Exiting the program.")
break
else:
print("Invalid choice. Please select a valid option.")
Menu:
1. Add a student and their grade
2. Update the grade of a student
3. Print all students with their grade
4. Exit
Enter your choice (1/2/3/4): 1
Enter the student's name: Srivathsa
Enter the student's grade: 8.0
Srivathsa's grade has been updated to 8.0.
Menu:
1. Add a student and their grade
2. Update the grade of a student
3. Print all students with their grade
4. Exit
Enter your choice (1/2/3/4): 2
Enter the student's name: Rahul
Rahul is not in the student list.
Menu:
1. Add a student and their grade
2. Update the grade of a student
3. Print all students with their grade
4. Exit
Enter your choice (1/2/3/4): 3
Student Grades:
Srivathsa: 8.0
Menu:
1. Add a student and their grade
2. Update the grade of a student
3. Print all students with their grade
4. Exit
Enter your choice (1/2/3/4): 4
Exiting the program.
Problem Statement 4:
1. Deposit Money.
2. Withdraw money(ensure the balance does'nt go negative)
3. Print the current balance.
class BankAccount:
def __init__(self,owner,balance=0):
self.owner=owner
self.balance=balance
def deposit(self,amount):
if amount >0:
self.balance+=amount
print(f"Deposited amount :{amount}, New balance:{self.balance}")
else:
print("Deposit amount must be positive")
def withdraw(self,amount):
if amount>0:
if amount<=self.balance:
self.balance-=amount
print(f"Withdrew amount:{amount},New balance:{self.balance}")
https://colab.research.google.com/drive/146PNCXL4t4iFkRqhmPIkEnCEOjKml-J6#scrollTo=quaaVoIHiv0C&printMode=true 2/4
2/19/25, 9:27 PM VineelPY.ipynb - Colab
else:
print("Invalid withdrawal amount ")
else:
print("Withdrawal amount must be positive")
def get_balance(self):
print(f"Account Owner:{self.owner}Current Balance:{self.balance}")
def deposit(self,amount):
if amount >0:
self.balance+=amount
print(f"Deposited amount :{amount}, New balance:{self.balance}")
else:
print("Deposit amount must be positive")
def withdraw(self,amount):
if amount>0:
if amount<=self.balance:
self.balance-=amount
print(f"Withdraw amount:{amount} , New balance:{self.balance}")
else:
print("Invalid withdrawal amount ")
else:
print("Withdrawal amount must be positive")
def get_balance(self):
print(f"Account Owner:{self.owner} ,Current Balance:{self.balance}")
a=BankAccount("Raj",1000)
a.deposit(1000)
a.withdraw(500)
a.get_balance()
Problem Statement 5:
Write a function that takes a list of numbers and return a tuple contating the minimum,maximum,avergae of the numbers.
def calculate_stats(numbers):
if not numbers:
return None,None,None
minimum = min(numbers)
maximum = max(numbers)
average = sum(numbers) / len(numbers)
return minimum, maximum, average
numbers = [10, 20, 30, 40, 50]
minimum, maximum, average = calculate_stats(numbers)
print("Minimum:", minimum)
print("Maximum:", maximum)
print("Average:", average)
Minimum: 10
Maximum: 50
Average: 30.0
Problem Statement 6: Create an inventory management system using a dictionary and classes where: .Each item in the inventory has a name
and quantity. 2. You can add stock, remove stock, and check the total inventory
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
print(f"Added {quantity} {item_name}(s). Total: {self.items[item_name]}")
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}")
https://colab.research.google.com/drive/146PNCXL4t4iFkRqhmPIkEnCEOjKml-J6#scrollTo=quaaVoIHiv0C&printMode=true 3/4
2/19/25, 9:27 PM VineelPY.ipynb - Colab
# Usage
inventory = Inventory()
inventory.add_item("Laptop", 10)
inventory.add_item("Mouse", 25)
inventory.remove_item("Laptop", 3)
inventory.display_inventory()
https://colab.research.google.com/drive/146PNCXL4t4iFkRqhmPIkEnCEOjKml-J6#scrollTo=quaaVoIHiv0C&printMode=true 4/4