Python Data Structures | List, Set, Tuple, Dictionary, Array, Linked Lists

 Data Structures in Python


What is Data Structure (DS) ?

A data structure is a way of organizing and storing data in a computer so that it can be accessed and used efficiently. In other words, a data structure provides a systematic way of managing and manipulating data.

Data structures can be classified into two categories: 

primitive data structures and non-primitive data structures.

Primitive data structures are the basic building blocks of programming languages, and include data types such as integers, floats, characters, and Boolean values. These data types are used to store simple values, and are not typically used to organize larger sets of data.

Non-primitive data structures, on the other hand, are used to organize and manage complex data sets. Examples of non-primitive data structures include arrays, lists, stacks, queues, trees, graphs, and hash tables. These data structures are designed to store and manipulate large sets of data efficiently, and can be used to implement algorithms for solving complex problems.


Here are some of the commonly used data structures in Python

1. Lists

A list is an ordered collection of items, where each item can be of any data type. Lists are mutable, meaning they can be modified after creation.

Example:

    my_list = list("FreeITCamp")

    print(my_list)          # Output: ['F', 'r', 'e', 'e', 'I', 'T', 'C', 'a', 'm', 'p']

2. Tuples: 

A tuple is similar to a list, but once created, it cannot be modified. Tuples are usually used to store related pieces of data.

Example:

    my_tuple = tuple("FreeITCamp")

    print(my_tuple)      # Output: ('F', 'r', 'e', 'e', 'I', 'T', 'C', 'a', 'm', 'p')


3. Dictionaries: 

A dictionary is an unordered collection of key-value pairs, where each key is unique. Dictionaries are used to store and retrieve data by their keys

Example:
    # Create a dictionary with some key-value pairs
    my_dict = {"one": 1, "two": 2, "three": 3}

    # Print the dictionary
    print(my_dict)  # Output: {'one': 1, 'two': 2, 'three': 3}

    # Accessing values in the dictionary
    print(my_dict["one"])  # Output: 1
    print(my_dict["three"])  # Output: 3

    # Modifying values in the dictionary
    my_dict["two"] = 20
    print(my_dict)  # Output: {'one': 1, 'two': 20, 'three': 3}

    # Adding new key-value pairs to the dictionary
    my_dict["four"] = 4
    print(my_dict)  # Output: {'one': 1, 'two': 20, 'three': 3, 'four': 4}

    # Removing key-value pairs from the dictionary
    del my_dict["three"]
    print(my_dict)  # Output: {'one': 1, 'two': 20, 'four': 4}


4. Sets:

A set is an unordered collection of unique items. Sets are useful when you need to keep track of a collection of items, but you don't care about their order.

Example:

    my_set = set("FreeITCamp")

    print(my_set)      # Output: {'C', 'T', 'r', 'F', 'm', 'p', 'a', 'e', 'I'}


5. Arrays: 

An array is a collection of items of the same data type. Arrays are used when you need to perform numerical operations on large amounts of data. 

In Python, you need to define an array by importing the built-in array module and calling the array function with the following arguments:

Type code: A string that specifies the data type of the array elements. This can be one of the following values:

'b' (signed char)

'B' (unsigned char)

'u' (Py_UNICODE)

'h' (signed short)

'H' (unsigned short)

'i' (signed int)

'I' (unsigned int)

'l' (signed long)

'L' (unsigned long)

'f' (float)

'd' (double)

Initial values: A sequence of values that will be used to initialize the array.

Example:

     import array

    # Create an array of integers
    my_array = array.array('i', [1, 2, 3, 4, 5])

    # Print the array
    print(my_array)  # Output: array('i', [1, 2, 3, 4, 5])

    # Accessing elements in the array
    print(my_array[0])  # Output: 1
    print(my_array[3])  # Output: 4

    # Modifying elements in the array
    my_array[2] = 10
    print(my_array)  # Output: array('i', [1, 2, 10, 4, 5])

    # Appending elements to the array
    my_array.append(6)
    print(my_array)  # Output: array('i', [1, 2, 10, 4, 5, 6])



6. Linked Lists:

A linked list is a data structure that consists of a sequence of nodes, where each node contains a value and a reference to the next node in the list.

Example:

    class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

    def create_linked_list(s):
        head = Node(s[0])
        current = head
        for char in s[1:]:
            new_node = Node(char)
            current.next = new_node
            current = new_node
    return head

    my_linked_list = create_linked_list("FreeITCamp")
    print(my_linked_list.data)      # Output: 'F'
    print(my_linked_list.next.data)      # Output: 'r'






Comments

Popular posts from this blog

Simplifying Software Development: A Beginner's Guide

Web Development Made Easy: A Beginner's Guide