Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Python Lab 1

The document outlines various Python programming exercises including managing a shopping list, storing employee information using tuples, managing student grades with a dictionary, creating a BankAccount class, calculating statistics from a list of numbers, and developing an inventory management system using classes and dictionaries. Each problem statement includes code examples demonstrating the required functionality. The document serves as a practical guide for applying basic programming concepts in Python.

Uploaded by

try.nahush
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 views

Python Lab 1

The document outlines various Python programming exercises including managing a shopping list, storing employee information using tuples, managing student grades with a dictionary, creating a BankAccount class, calculating statistics from a list of numbers, and developing an inventory management system using classes and dictionaries. Each problem statement includes code examples demonstrating the required functionality. The document serves as a practical guide for applying basic programming concepts in Python.

Uploaded by

try.nahush
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/ 4

2/19/25, 9:27 PM VineelPY.

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:

1. Add items to the shopping list.


2. Remove an item.
3. Print the total numberof items in the list.

#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)

The data should remain immutable to ensure it is not accidentally modified:

Write a python Script To:

Create a tuple for each


Store multiple employee tuples in a list
Display the detaqils of employee in the "Finance" Department

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:

1. Add a student and their grade.


2. Update the grade of a student.
3. Print all students with their grade.

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:

Cretae a BankAccount class to:

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()

Deposited amount :1000, New balance:2000


Withdraw amount:500 , New balance:1500
Account Owner:Raj ,Current Balance:1500

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

Double-click (or enter) to edit

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()

Added 10 Laptop(s). Total: 10


Added 25 Mouse(s). Total: 25
Removed 3 Laptop(s). Remaining: 7
Current Inventory:
Laptop: 7
Mouse: 25

Start coding or generate with AI.

https://colab.research.google.com/drive/146PNCXL4t4iFkRqhmPIkEnCEOjKml-J6#scrollTo=quaaVoIHiv0C&printMode=true 4/4

You might also like