Friday, August 11, 2023

Data Structures and Fruit Vendors

 Here's a table that illustrates the four data structures – list, set, tuple, and dictionary – along with an example involving a fruit vendor:



Data Structure

Description

Example with Fruit Vendor

List

An ordered collection of items, allowing duplicates and modification of elements.

fruits_list = ['apple', 'banana', 'orange', 'banana']

Set

An unordered collection of unique items.

unique_fruits = {'apple', 'banana', 'orange'}

Tuple

An ordered and immutable collection of items.

coordinates = (3, 4)

Dictionary

A collection of key-value pairs, where each key is unique.

fruit_prices = {'apple': 0.5, 'banana': 0.3, 'orange': 0.6}






Python Data Structures






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

Trajectory of Chennai to 2050s

  Having considered all this. Let us bring Chennai to this equation. Now tell me which trajectory will chennai follow in the next 25 years C...