Computational Thinking Using Python – CSE1500
Lab sheet – 3.1
Problem:
A company collects employee email addresses from different departments. Some
employees are in multiple teams, so there are duplicate emails.
Task:
Write a Python program using a set to remove duplicates.
Code :
Method 1:
unique_emails = set(emails)
print("Unique employee emails:", unique_emails)
Method 2:
read emails from keyboard ( or input dynamically from user)
emails = input("Enter email addresses separated by space: ").split()
unique_emails = set(emails)
print("Unique employee emails:", unique_emails)
================
Problem 2. Checking Common Students in Two Courses
Two courses — Python and Data Science — have student enrollment lists.
Task:
Find out which students are enrolled in both courses.
Code : Method 1)
python_course = {"Asha", "Ravi", "John", "Meena"}
data_science = {"John", "Meena", "Kiran", "Ravi"}
common_students = python_course.intersection(data_science)
print("Students enrolled in both courses:", common_students)
Method 2:
python_course = set(input("Enter Python course students separated by space ").split())
data_science = set(input("Enter Data Science students (space-separated): ").split())
common_students = python_course.intersection(data_science)
print("Students enrolled in both courses:", common_students)
=====================
Problem 3. Finding Unique Visitors to a Website
A website logs user IDs visiting it every day.
Task:
Find the total number of unique visitors over a week.
Method 1:
# store first 3 days of visitors numbers
monday = {101, 102, 103, 104}
tuesday = {102, 104, 105, 106}
wednesday = {101, 106, 107}
unique_visitors = monday.union(tuesday).union(wednesday)
print("Unique visitors in 3 days:", unique_visitors)
print("Total unique visitors:", len(unique_visitors))
Method 2:
monday = set(input("Enter Monday visitors IDs: ").split())
tuesday = set(input("Enter Tuesday visitors IDs: ").split())
wednesday = set(input("Enter Wednesday visitors IDs: ").split())
unique_visitors = monday.union(tuesday).union(wednesday)
print("Unique visitors in 3 days:", unique_visitors)
print("Total unique visitors:", len(unique_visitors))
=====================
Problem 4. Finding Absent Students
You have a list of all students and those who attended today’s class.
Task:
Find who was absent.
Method 1:
all_students = {"Asha", "Ravi", "John", "Meena", "Kiran"}
present_students = {"John", "Meena", "Ravi"}
absent_students = all_students.difference(present_students)
print("Absent students:", absent_students)
Method 2:
all_students = set(input("Enter all students' names: ").split())
present_students = set(input("Enter present students' names: ").split())
absent_students = all_students.difference(present_students)
print("Absent students:", absent_students)
===============
Problem 5:
A school has a “coding club,” which must only include students from the “computer science
department.”
Task:
Verify if all coding club members belong to the computer science department.
Hint : Subset or Superset Check
Method 1:
cs_department = {"Asha", "Ravi", "John", "Meena", "Kiran"}
coding_club = {"Ravi", "John"}
print("All club members are from CS:", coding_club.issubset(cs_department))
print("CS dept contains all club members:", cs_department.issuperset(coding_club))
Method 2:
cs_department = set(input("Enter CS department students: ").split())
coding_club = set(input("Enter coding club members: ").split())
print("All club members are from CS:", coding_club.issubset(cs_department))
print("CS dept contains all club members:", cs_department.issuperset(coding_club))
============================
Problem 6: Detecting Common Customers Between Two Stores
Two branches of a retail store maintain customer ID lists.
Task:
Find the customers who shop at both branches and those who shop only in one.
Method 1:
store_A = {1001, 1002, 1003, 1004}
store_B = {1003, 1004, 1005, 1006}
both = store_A & store_B
only_one = store_A ^ store_B
print("Customers who shop at both:", both)
print("Customers who shop only at one store:", only_one)
Method 2:
store_A = set(input("Enter Store A customer IDs: ").split())
store_B = set(input("Enter Store B customer IDs: ").split())
both = store_A & store_B
only_one = store_A ^ store_B
print("Customers who shop at both stores:", both)
print("Customers who shop only at one store:", only_one)
===================
Problem 6: Finding Missing Items in Inventory
An e-commerce warehouse has a record of items received and items shipped.
Task:
Find which items are still in stock.
received = {"TV", "Laptop", "Mobile", "Camera", "Headphones"}
shipped = {"Laptop", "Mobile"}
in_stock = received - shipped
print("Items still in stock:", in_stock)
Student Activity : Rewrite the above program by reading data dynamically form keyboard.
==================
Problem : Student Marks Management System
A school needs to store and retrieve students’ marks.
Problem:
Create a dictionary where the keys are student names and the values are their marks.
Then, allow the user to:
Add a new student
Update marks
Display all students with marks
Code:
students = {}
while True:
print("\n1. Add Student\n2. Update Marks\n3. Display All\n4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
students[name] = marks
elif choice == 2:
name = input("Enter student name to update: ")
if name in students:
students[name] = int(input("Enter new marks: "))
else:
print("Student not found!")
elif choice == 3:
for name, marks in students.items():
print(f"{name}: {marks}")
elif choice == 4:
break
else:
print("Invalid choice!")
===============
Problem : Bank Account Management
A bank keeps track of account holders and their balances.
Create a dictionary where keys are account numbers, and values are account details.
Code:
accounts = {}
while True:
print("\n1. Create Account\n2. Deposit\n3. Withdraw\n4. Display All\n5. Exit")
ch = int(input("Enter your choice: "))
if ch == 1:
acc = input("Enter Account Number: ")
name = input("Enter Name: ")
bal = float(input("Enter Opening Balance: "))
accounts[acc] = {"Name": name, "Balance": bal}
elif ch == 2:
acc = input("Enter Account Number: ")
amt = float(input("Enter Deposit Amount: "))
if acc in accounts:
accounts[acc]["Balance"] += amt
else:
print("Account not found!")
elif ch == 3:
acc = input("Enter Account Number: ")
amt = float(input("Enter Withdraw Amount: "))
if acc in accounts:
if accounts[acc]["Balance"] >= amt:
accounts[acc]["Balance"] -= amt
else:
print("Insufficient balance!")
else:
print("Account not found!")
elif ch == 4:
for acc, info in accounts.items():
print(f"{acc} → {info}")
elif ch == 5:
break
=====================