Here's a table that illustrates the four data structures – list, set, tuple, and dictionary – along with an example involving a fruit vendor:
| |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Now, let's explore the example with a fruit vendor using these data structures:
Suppose a fruit vendor wants to manage their inventory and prices:
python
# Using a list to store fruits in order
fruits_list = ['apple', 'banana', 'orange']
# Using a set to store unique fruits
unique_fruits = {'apple', 'banana', 'orange'}
# Using a tuple to store coordinates of a fruit stand
coordinates = (3, 4)
# Using a dictionary to store fruit prices
fruit_prices = {'apple': 0.5, 'banana': 0.3, 'orange': 0.6}
In this example:
- The
fruits_list
represents the vendor's inventory. The list maintains the order of fruits, allowing duplicates. - The
unique_fruits
set ensures that each fruit is listed only once. It helps to quickly identify distinct fruit types. - The
coordinates
tuple stores the location of the vendor's fruit stand. - The
fruit_prices
dictionary associates fruit names (keys) with their corresponding prices (values).
Each data structure serves a specific purpose in managing different aspects of the fruit vendor's information.
No comments:
Post a Comment