Advanced Data Types in Python

List:

  • Lists are ordered, mutable (changeable), and allow duplicate elements.
  • Denoted by square brackets [ ].
  • Example: my_list = [1, 2, 3, 4, 5]

Use Cases:

  1. Storing collections of similar items.
  2. Manipulating sequential data.
  3. Iterating through elements.

Coding Examples:

pythonCopy code# Creating a list
my_list = [1, 2, 3, 4, 5]

# Accessing elements
print(my_list[0])  # Output: 1

# Adding elements
my_list.append(6)
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

# Removing elements
my_list.remove(3)
print(my_list)  # Output: [1, 2, 4, 5, 6]

# List comprehension
squared_numbers = [x**2 for x in my_list]
print(squared_numbers)  # Output: [1, 4, 16, 25, 36]

Tuple:

  • Tuples are ordered, immutable (unchangeable), and allow duplicate elements.
  • Denoted by parentheses ( ).
  • Example: my_tuple = (1, 2, 3, 4, 5)

Use Cases:

  1. Storing related data that shouldn’t be changed.
  2. Unpacking sequences into variables.
  3. Efficiently passing around data.

Coding Examples:

pythonCopy code# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)

# Accessing elements
print(my_tuple[0])  # Output: 1

# Unpacking
a, b, c, d, e = my_tuple
print(a, b, c, d, e)  # Output: 1 2 3 4 5

Set:

  • Sets are unordered, mutable, and do not allow duplicate elements.
  • Denoted by curly braces { }.
  • Example: my_set = {1, 2, 3, 4, 5}

Use Cases:

  1. Eliminating duplicate values from a collection.
  2. Set operations like union, intersection, difference.

Coding Examples:

pythonCopy code# Creating a set
my_set = {1, 2, 3, 4, 5}

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

# Removing elements
my_set.remove(3)
print(my_set)  # Output: {1, 2, 4, 5, 6}

# Set operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))  # Output: {1, 2, 3, 4, 5}
print(set1.intersection(set2))  # Output: {3}

Dictionary:

  • Dictionaries are unordered, mutable, and store data in key-value pairs.
  • Denoted by curly braces { }, with key-value pairs separated by colon :.
  • Example: my_dict = {'a': 1, 'b': 2, 'c': 3}

Use Cases:

  1. Storing data with a unique key for quick retrieval.
  2. Representing real-world entities and their attributes.

Coding Examples:

pythonCopy code# Creating a dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}

# Accessing elements
print(my_dict['a'])  # Output: 1

# Adding elements
my_dict['d'] = 4
print(my_dict)  # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

# Removing elements
del my_dict['b']
print(my_dict)  # Output: {'a': 1, 'c': 3, 'd': 4}

# Dictionary comprehension
squared_dict = {key: value**2 for key, value in my_dict.items()}
print(squared_dict)  # Output: {'a': 1, 'c': 9, 'd': 16}

These examples cover basic operations and common use cases for each data type.

Operations with Data Types

  • Creating a List:
pythonCopy codemy_list = [1, 2, 3, 4, 5]
  • Accessing Elements:
pythonCopy codeprint(my_list[0])  # Output: 1
  • Adding Elements:
pythonCopy codemy_list.append(6)          # Adds 6 to the end
my_list.insert(2, 7)       # Inserts 7 at index 2
my_list.extend([8, 9])     # Extends list by adding elements of another list
  • Removing Elements:
pythonCopy codemy_list.remove(3)          # Removes the first occurrence of 3
popped_element = my_list.pop()  # Removes and returns the last element
del my_list[0]             # Removes element at index 0
  • List Slicing:
pythonCopy codesubset = my_list[1:3]      # Returns elements from index 1 to 2

Tuple:

  • Creating a Tuple:
pythonCopy codemy_tuple = (1, 2, 3, 4, 5)
  • Accessing Elements:
pythonCopy codeprint(my_tuple[0])  # Output: 1
  • Unpacking:
pythonCopy codea, b, c, d, e = my_tuple

Set:

  • Creating a Set:
pythonCopy codemy_set = {1, 2, 3, 4, 5}
  • Adding Elements:
pythonCopy codemy_set.add(6)
my_set.update([7, 8, 9])  # Adds multiple elements
  • Removing Elements:
pythonCopy codemy_set.remove(3)
my_set.discard(5)     # Removes 5 if present
popped_element = my_set.pop()  # Removes and returns an arbitrary element
  • Set Operations:
pythonCopy codeset1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)
difference_set = set1.difference(set2)

Dictionary:

  • Creating a Dictionary:
pythonCopy codemy_dict = {'a': 1, 'b': 2, 'c': 3}
  • Accessing Elements:
pythonCopy codeprint(my_dict['a'])  # Output: 1
  • Adding/Updating Elements:
pythonCopy codemy_dict['d'] = 4  # Adds a new key-value pair
my_dict.update({'e': 5, 'f': 6})  # Updates multiple key-value pairs
  • Removing Elements:
pythonCopy codedel my_dict['b']      # Removes key 'b' and its value
popped_value = my_dict.pop('c')  # Removes key 'c' and returns its value
my_dict.clear()       # Clears all key-value pairs
  • Dictionary Iteration:
pythonCopy codefor key, value in my_dict.items():
    print(key, value)

Leave a comment