0% found this document useful (0 votes)
8 views

TP2 PYTHON 24_25 (1)

Uploaded by

bedairiainel
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)
8 views

TP2 PYTHON 24_25 (1)

Uploaded by

bedairiainel
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/ 3

Electronic Department

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:

There are four main data structures in Python:

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:

For each exercise, write a Python program to solve the task.

Exercise 1: Network Signal Channels

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.

Functions to Use: set(), .union(), .add(), sorted()

Exercise 2: Task Queue for Robotic Arm

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.

Functions to Use: .append(), .remove(), .sort(), .reverse()

Exercise 3: Sensor Data Log

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.

1. Create tuples representing three hours of sensor data.


2. Calculate the average temperature, humidity, and voltage.
3. Print each hour’s data along with the averages.

Functions to Use: .count(), .index(), sum(), len()

Exercise 4: Mobile Network User Profiles

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.

Functions to Use: .keys(), .values(), .items(), .get(), .pop()

Exercise 5: Robot Component Inventory (Combined Data Structures)

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.

Functions to Use: .update(), .remove(), .union(), .add()

Exercise 6: Telecom System Logs (Nested Data Structures)

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

1. Create a list of dictionaries where each dictionary represents a log entry.


2. Count the number of "critical" errors.
3. Add a new log entry when an alert is received and remove old entries if they are
resolved.
4. Print the complete list of logs, organized by severity.

Functions to Use: .append(), .pop(), .count(), .sort()

3/3

You might also like