TP2 PYTHON 24_25 (1)
TP2 PYTHON 24_25 (1)
ING RSI
Lab Work 2
Practical Data Structures in Python
Objectives:
To understand and apply sets, lists, tuples, and dictionaries.
To practice using built-in functions relevant to each data structure.
To work on real-life applications that demonstrate the need for each type of data
structure.
Reminder:
1. Set:
Definition: An unordered collection of unique elements.
Syntax: {element1, element2, ...} or set([element1, element2, ...])
Characteristics:
No duplicates are allowed.
Unordered, so elements don't have an index.
Commonly used for operations like union, intersection, and difference.
2. List:
Definition: An ordered, mutable collection of elements.
Syntax: [element1, element2, ...]
Characteristics:
Allows duplicates.
Elements can be accessed by index.
Commonly used when order is important, or when you need to modify
elements.
3. Tuple:
Definition: An ordered, immutable collection of elements.
Syntax: (element1, element2, ...)
Characteristics:
Allows duplicates.
Elements cannot be modified after creation.
Often used for fixed collections of items, such as coordinates or database
records.
4. Dictionary:
Definition: An unordered collection of key-value pairs.
Syntax: {key1: value1, key2: value2, ...}
Characteristics:
Keys must be unique, but values can be duplicated.
Elements are accessed by keys, not indexes.
Used when you need to map keys to values, like storing settings or data
records.
1/3
Instructions:
Scenario: You are analyzing different signal channels received from various base stations in a
telecommunications network and need to identify unique frequencies in use.
1. Create sets representing frequencies received from three different base stations (with
some overlapping frequencies).
2. Combine all frequency sets into a single set to determine unique frequencies in the
network.
3. Print the sorted list of unique frequencies.
Scenario: A robotic arm has a series of tasks to perform in a sequence. These tasks are
queued, and the list is updated as tasks are added, completed, or prioritized.
1. Create a list with five initial tasks (e.g., pick, place, weld, inspect, release).
2. Add two more tasks to the list and remove a completed task.
3. Sort the list alphabetically to organize tasks and then reverse it to process in reverse
order.
4. Print the final task queue.
Scenario: A robot records sensor data readings every hour. For data integrity, the readings
(temperature, humidity, and voltage) are stored in tuples to prevent modification.
Scenario: In a telecom network, user profiles store information like name, data balance, and
plan type. A dictionary is useful here for efficient data management.
1. Create a dictionary with keys as user IDs and values as dictionaries with user
information (e.g., {"name": "Mohamed", "data_balance": 500, "plan_type":
"prepaid"}).
2/3
2. Add a new user to the dictionary and update the data balance of an existing user.
3. Remove a user who has deactivated his profile.
4. Check if a user is active and display his data balance.
Scenario: In a robotics lab, inventory needs to track available components, quantities, and
vendors for robot assembly.
1. Create a dictionary with components as keys and a list of dictionaries for each vendor
with their supply quantities and price.
2. Update the quantity if a vendor restocks or remove a vendor if they run out.
3. Find all unique component names in stock.
4. Print the inventory with each component and corresponding vendor details.
Scenario: A telecom system records log entries in a list of dictionaries, where each log
contains details about errors or alerts (time, error code, severity level).
3/3