Data Structures
Playground Using Python
Introduction to Data Structures
In Python, data structures are used to store and organize data efficiently. Among the
most commonly used built-in data structures are sets, tuples, and dictionaries.
A list is an ordered and mutable collection of elements. It allows duplicates and can store different data
types. Lists are widely used when you need to store multiple items and modify them later — such as
adding, removing, or updating elements.
Example: fruits = ["apple", "banana", "cherry"]
A set is an unordered collection of unique elements. It is mainly used when we need to remove
duplicates or perform mathematical operations like union and intersection.
Example: numbers = {1, 2, 3, 4}
A tuple is an ordered and immutable sequence of elements. Once created, the values in a tuple cannot
be changed. Tuples are often used to store fixed collections of items, such as coordinates or records.
Example: point = (4, 5)
A dictionary is an unordered collection of key-value pairs, allowing fast data retrieval based on a key.
It’s one of the most powerful and flexible structures in Python, often used for mapping and storing
related information.
Example: student = {"name": "Rahul", "age": 20}
Feature List Tuple Set Dictionary
Ordered collection of Ordered, immutable Unordered collection Collection of key–
Definition elements collection of elements of unique elements value pairs
Syntax [1, 2, 3] (1, 2, 3) {1, 2, 3} {'a': 1, 'b': 2}
Mutable (can add, Mutable (can add,
Mutability remove, or change Immutable (cannot Mutable (can update, remove key–
elements) modify elements) add/remove elements) value pairs)
Maintains order of Maintains order of
Order insertion insertion Unordered Ordered
Does not allow Keys must be unique;
Duplicates Allows duplicates Allows duplicates duplicates values can repeat
Supports indexing and Supports indexing and Does not support Accessed by keys, not
Indexing / Slicing slicing slicing indexing or slicing by index
Fixed collections, Unique items,
Use Case Storing sequences of constants, or keys in a membership testing, Storing structured data
items dictionary set operations as key–value pairs
OBJECTIVES
The main objective of this task is to understand and demonstrate the use
of Python’s core data structures — List, Tuple, Set, and Dictionary —
through practical examples and operations.
.
🔹 Specific Objectives:
• Create lists, tuples, and sets using user input.
• Differentiate between mutable and immutable data types.
• Perform set operations such as union and intersection.
• Combine two lists into a dictionary using the zip() function.
• Use comprehension to build a square table efficiently.
• Count the frequency of elements in a list.
• Write functions to identify unique elements and duplicates.
• Sort a dictionary by its values.
• Demonstrate aliasing and cloning to understand memory behavior.
System Flow
Task - 1
This function, create_structures() takes user input and demonstrates how to create a list, tuple, and set in
Python using the same data. It first asks the user to enter elements separated by commas, then splits the
input into individual items and removes any extra spaces to form a clean list. The list is then converted into a
tuple, which is an immutable version of the same data, and into a set, which automatically removes
duplicate elements and stores items in an unordered way. Finally, the function prints all three data
structures, allowing us to clearly observe their differences in mutability, order, and handling of duplicates.
Task - 2
The mutable_vs_immutable() function demonstrates the difference between mutable and immutable data
types in Python. It takes user input to create a list and a tuple from the same data. The list is modified
successfully by adding a new element, showing that lists are mutable. When the function tries to modify the
tuple, it raises an error because tuples are immutable and cannot be changed after creation. This clearly
highlights the behavioral difference between lists and tuples.
Task - 3
The set_operations() function demonstrates basic set operations in Python. It takes two sets of elements from
the user, created by splitting comma-separated input and converting them into sets. The function then calculates
and displays the union of the two sets, which combines all unique elements, and the intersection, which shows
only the elements common to both sets. This example highlights how sets can be used to perform efficient
mathematical operations on collections of unique items.
Task - 4
The combine_lists_to_dict() function demonstrates how to combine two lists into a dictionary in Python. It takes
user input for a list of keys and a list of values, splits them by commas, and uses the zip() function to pair each
key with its corresponding value. The dict() function then converts these pairs into a dictionary, which is printed
to show the result. This example illustrates how lists can be efficiently combined to create key–value mappings.
Task - 5
The square_table() function generates a simple square table in Python. It asks the user to enter a number limit n
and then prints each number from 1 to n alongside its square. Using a for loop and formatted strings, the function
displays the number and its square in a clear n : n² format. This example demonstrates basic looping, arithmetic
operations, and formatted output in Python.
Task - 6
The count_frequency() function counts and displays the frequency of each element in a list. It takes user input of
comma-separated elements, cleans them, and stores them in a list. Using a loop, it iterates through each element and
prints the number of times it appears in the list, ensuring that each element is counted only once by keeping track of
already visited items. This function demonstrates how to analyze data in a list and determine element occurrences.
Task - 7
The unique_elements() function returns all unique items from a list by converting it into a set, while the duplicates() function
identifies elements that appear more than once, returning them as a set to avoid repetition. The run_unique_duplicate()
function takes user input of comma-separated elements, cleans the list, and then uses these two functions to display the
unique elements and duplicate elements separately. This example demonstrates how sets and list operations can be used to
analyze and organize data efficiently.
Task - 8
The sort_dict_by_values() function allows the user to create a dictionary by entering a specified number of key–value pairs.
After collecting the input, it sorts the dictionary based on the values in ascending order using the sorted() function with a
lambda function as the key. The sorted dictionary is then converted back into a dictionary and displayed. This function
demonstrates how dictionaries can be efficiently organized and sorted according to their values.
Task - 9
The aliasing_vs_cloning() function demonstrates the difference between aliasing and cloning in Python lists. It takes user input
to create a list a, then creates b as an alias of a and c as a cloned copy using the copy() method. When an element is added to
b, it also affects a because both refer to the same object, whereas adding an element to c does not change a, since it is a
separate copy. The function also compares the identities of the lists using id(), highlighting that aliasing shares the same
memory reference while cloning creates a new object. This example clearly illustrates how data can be shared or duplicated in
memory.
Menu and Entry Point
The main_menu() function displays an interactive menu of data structure operations and
repeatedly prompts the user to choose an option until they exit.
Menu and Entry Point
This code takes the user’s menu choice and calls the
corresponding function for the selected data structure
operation, or exits/handles invalid input.
Menu and Entry Point
This code defines the entry point of the Python program using the if __name__ == "__main__": construct. It ensures that
the program runs the following code only when the script is executed directly, not when it is imported as a module in
another script. In this case, it first prints a welcome message, "Welcome to the Data Structure Playground!", and then calls
the main_menu() function, which likely contains the interactive menu for exploring different data structures and their
operations. This structure is a standard practice in Python to organize code and control program execution.
Conclusion
In this task, we explored Python’s core data structures — lists, tuples, sets, and dictionaries —
and learned how they differ in terms of mutability, order, and uniqueness. By performing
various operations such as set union and intersection, list-to-dictionary conversion, data
comprehension, and sorting, we gained a practical understanding of how to organize and
process data effectively. The concepts of aliasing and cloning further highlighted how data is
stored and managed in memory. Overall, this activity helped build a strong foundation for
efficient programming and data handling using Python’s built-in data structures.
THANK
YOU