0% found this document useful (0 votes)
1 views10 pages

Batch 9 Python.......

The document outlines Python functions for managing customer data, including retrieving, updating, and merging customer information stored in dictionaries. It provides test cases for each function to ensure their correctness and demonstrates their application in real-world business scenarios, such as retail and subscription services. The report concludes that these functions enhance customer data management and improve business operations through effective data handling.

Uploaded by

hariikrishna5701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views10 pages

Batch 9 Python.......

The document outlines Python functions for managing customer data, including retrieving, updating, and merging customer information stored in dictionaries. It provides test cases for each function to ensure their correctness and demonstrates their application in real-world business scenarios, such as retail and subscription services. The report concludes that these functions enhance customer data management and improve business operations through effective data handling.

Uploaded by

hariikrishna5701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PROBLEM SOLVING THROUGH

PYTHON
Batch no.09

A.sriya, 242FA05006
K.Srithanvi, 242FA05033
K.arun kumar, 242FA05038
P.prashanth, 242FA05055
def
1. Introduction get_customer_by_name(customers,
name):
Efficient customer data handling is """Returns the dictionary for the
crucial in business applications. This customer with the given name."""
report details Python for customer in customers:
implementations for retrieving, if customer.get("name") ==
updating, and merging customer name:
information stored as dictionaries return customer
within a list. The report also return None
introduces sorting and filtering
functionalities to improve usability. TEST CASES
Programs to solve def test_get_customer_by_name():
# Test Case 1: Customer
2.PROBLEM SOLVEING exists with the given name
THROUGH PYTHON customers = [
{"name": "Alice", "age": 30,
A. RETRIEVING A "email": "[email protected]"},
CUSTOMER BY NAME {"name": "Bob", "age": 25,
"email": "[email protected]"}
The following function searches for
]
a customer by name and returns their result =
details: get_customer_by_name(customers,
"Alice")

2 Insert title of presentation | ieee.org/inserturl


print("Test Case 1:", result) # B. UPDATING CUSTOMER
Expected: {"name": "Alice", "age":
30, "email": "[email protected]"}
AGE
The following function updates the
# Test Case 2: Customer does age of a customer in the list of
not exist with the given name dictionaries:
result = def
get_customer_by_name(customers,
update_customer_age(customers,
"Charlie")
print("Test Case 2:", result) # name, new_age):
Expected: None """Updates the age of a customer
in the list of dictionaries."""
# Test Case 3: Empty list of
for customer in customers:
customers
customers = [] if customer.get("name") ==
result = name:
get_customer_by_name(customers, customer["age"] = new_age
"Alice")
return True # Update
print("Test Case 3:", result) #
Expected: None successful
return False # Customer not
# Run the tests found
test_get_customer_by_name()
TEST CASES
def test_update_customer_age():

3 Insert title of presentation | ieee.org/inserturl


# Test Case 1: Update a print("Customers list after update
customer's age successfully attempt:", customers) # Expected:
customers = [ no changes to the list
{"name": "Alice", "age": 30,
"email": "[email protected]"}, # Test Case 3: Empty list of
{"name": "Bob", "age": 25, customers
"email": "[email protected]"} customers = []
] result =
result = update_customer_age(customers,
update_customer_age(customers, "Alice", 35)
"Alice", 35) print("Test Case 3 (Empty list):",
print("Test Case 1 (Update result) # Expected: False
successful):", result) # Expected: print("Customers list after update
True attempt:", customers) # Expected: []
print("Updated customer:",
customers) # Expected: {"name": # Run the tests
"Alice", "age": 35, "email": test_update_customer_age()
"[email protected]"} C. MERGING CUSTOMER
# Test Case 2: Customer not DATA
found (name does not exist) The following function merges two
result = customer dictionaries, combining
update_customer_age(customers, purchase histories:
"Charlie", 40) def
print("Test Case 2 (Customer not
merge_customer_data(customer1,
found):", result) # Expected: False
customer2):

4 Insert title of presentation | ieee.org/inserturl


"""Combines two customer TEST CASES
dictionaries, merging purchase def test_merge_customer_data():
histories and updating fields.""" # Test Case 1: Successfully
if customer1.get("name") != merging two customers with
customer2.get("name"): matching names
customer1 = {
return "Error: Customer names "name": "Alice",
do not match!" "age": 30,
"email": "[email protected]",
merged_customer = "purchases": ["item1", "item2"]
}
customer1.copy() customer2 = {
for key, value in "name": "Alice",
customer2.items(): "age": 30,
if key == "purchases": "email":
"[email protected]",
merged_customer[key] = "purchases": ["item2", "item3"]
list(set(customer1.get(key, []) + }
customer2.get(key, []))) result =
elif key != "name": # Avoid merge_customer_data(customer1,
customer2)
modifying the name print("Test Case 1 (Merge
merged_customer[key] = successful):", result)
value # Expected: Merged customer
with updated email and combined
purchase history:
return merged_customer #{

5 Insert title of presentation | ieee.org/inserturl


# "name": "Alice", "age": 30, # Expected: "Error: Customer
"email": "[email protected]", names do not match!"
"purchases": ["item1", "item2",
"item3"] # Test Case 3: Merging customers
#} where one customer has an empty
purchase history
customer1 = {
# Test Case 2: Attempting to "name": "Alice",
merge customers with different "age": 30,
names "email": "[email protected]",
customer1 = { "purchases": []
"name": "Alice", }
"age": 30, customer2 = {
"email": "[email protected]", "name": "Alice",
"purchases": ["item1", "item2"] "age": 30,
} "email":
customer2 = { "[email protected]",
"name": "Bob", "purchases": ["item3"]
"age": 25, }
"email": "[email protected]", result =
"purchases": ["item2", "item3"] merge_customer_data(customer1,
} customer2)
result = print("Test Case 3 (Empty
merge_customer_data(customer1, purchase history):", result)
customer2) # Expected: Merged customer
print("Test Case 2 (Names do not with updated email and combined
match):", result) purchase history:

6 Insert title of presentation | ieee.org/inserturl


#{ FILTERING
# "name": "Alice", "age": 30,
"email": "[email protected]", CUSTOMERS BY
"purchases": ["item3"]
MINIMUM AGE
#}

# Run the tests The following function filters


test_merge_customer_data() customers by a minimum age
Extension: Sorting and criterion:
Filtering
def
SORTING CUSTOMERS BY
filter_customers_by_min_age(custo
AGE
mers, min_age):
The following function sorts """Filters customers by a
customers by age in ascending or minimum age criteria."""
descending order: return [customer for customer in
def customers if customer["age"] >=
sort_customers_by_age(customers, min_age]
descending=False):
"""Sorts customers by age in Results and Discussion
ascending or descending order."""
To demonstrate the functionalities,
return sorted(customers,
consider the following example
key=lambda x: x["age"],
dataset:
reverse=descending)

7 Insert title of presentation | ieee.org/inserturl


customers = [ ["Laptop", "Phone"]}
{"name": "Alice", "age": 30, customer2 = {"name": "Alice",
"purchases": ["Laptop", "Phone"]}, "age": 32, "purchases":
{"name": "Bob", "age": 25, ["Headphones", "Phone"]}
"purchases": ["Tablet"]}, print(merge_customer_data(custo
{"name": "Charlie", "age": 35, mer1, customer2))
"purchases": ["Monitor",
"Keyboard"]}, # Sort customers by age
] print(sort_customers_by_age(cust
Function Usage Example omers))
# Get a customer by name print(sort_customers_by_age(cust
print(get_customer_by_name(cust omers, descending=True))
omers, "Alice"))
# Filter customers by minimum
# Update customer age age
update_customer_age(customers, print(filter_customers_by_min_ag
"Alice", 31) e(customers, 30))
print(customers)
Case Studies: Business
# Merge two customer records
Applications
customer1 = {"name": "Alice",
"age": 30, "purchases":

8 Insert title of presentation | ieee.org/inserturl


CASE STUDY 1: RETAIL segment users based on age groups.
BUSINESS APPLICATION By analyzing customer data, the
A retail company, XYZ Electronics, company optimized its content
uses the implemented Python recommendations, leading to a 20%
functions to manage its customer increase in watch time among
database. By leveraging these younger audiences and a 10%
functions, the company efficiently improvement in customer retention.
retrieves customer information,
updates records, merges purchase Conclusion
This report presents essential Python
histories, and filters customers for
functions for handling customer data
targeted marketing campaigns. For
effectively. The implemented
example, XYZ Electronics sorted
solutions support name-based
customers by age to personalize
retrieval, age updates, merging of
promotions for different
records, and extended functionalities
demographics, resulting in a 15%
such as sorting and filtering. These
increase in customer engagement.
tools facilitate efficient customer
CASE STUDY 2: data management for business
SUBSCRIPTION SERVICE applications.
OPTIMIZATION
A streaming platform uses the
sorting and filtering functions to

9 Insert title of presentation | ieee.org/inserturl


References
[1] IEEE Standards for Software
Engineering, IEEE Xplore, 2024.
[2] Python Documentation,
https://docs.python.org/3/

1 Insert title of presentation | ieee.org/inserturl


0

You might also like