Python Data Types: List, Tuple and set

  1. Lists:

    • Ordered collection of elements.

    • Mutable, meaning you can change, add, or remove elements after creation.

    • Denoted by square brackets [].

    • Allows duplicate elements.

    • Example:

    # Creating a list
    my_list = [1, 2, 3, 4, 4, 5]
    print(my_list)  # Output: [1, 2, 3, 4, 4, 5]

    # Modifying a list
    my_list.append(6)
    print(my_list)  # Output: [1, 2, 3, 4, 4, 5, 6]
  1. Tuples:

    • Ordered collection of elements.

    • Immutable, meaning once created, you can't change, add, or remove elements.

    • Denoted by parentheses ().

    • Allows duplicate elements.

    • Example:

    # Creating a tuple
    my_tuple = (1, 2, 3, 4, 4, 5)
    print(my_tuple)  # Output: (1, 2, 3, 4, 4, 5)
  1. Sets:

    • Un-ordered collection of unique elements.

    • Mutable, you can add or remove elements after creation, but the elements themselves are immutable.

    • Denoted by curly braces {}.

    • Does not allow duplicate elements.

    • Example:

    # Creating a set
    my_set = {1, 2, 3, 4, 4, 5}
    print(my_set)  # Output: {1, 2, 3, 4, 5}

    # Adding elements to a set
    my_set.add(6)
    print(my_set)  # Output: {1, 2, 3, 4, 5, 6}

To illustrate their differences, you could create each of these data structures with sample elements, perform operations like adding or removing elements, and then display the contents to observe how they behave differently. You can use a Python environment or an online Python interpreter like Repl.it or Jupyter Notebook to run these examples. If you have specific questions or encounter any issues while working with these data structures, feel free to ask!