**Python: Data Structures

This lesson introduces the fundamental data structures in Python: lists, tuples, dictionaries, and sets. You'll learn how to create, access, and manipulate these data structures, which are essential building blocks for any data science task.

Learning Objectives

  • Define and differentiate between lists, tuples, dictionaries, and sets.
  • Create and modify lists and dictionaries.
  • Access elements within each data structure using indexing and keys.
  • Understand the use cases for each data structure in data science applications.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Data Structures

Data structures are ways of organizing and storing data in a computer so that it can be used efficiently. Python provides several built-in data structures that are crucial for data science. These include lists, tuples, dictionaries, and sets. Understanding how they work is fundamental to working with data in Python.

Lists

Lists are ordered, mutable (changeable) collections of items. They are defined using square brackets []. Lists can contain items of different data types.

my_list = [1, 2, 3, 'apple', 'banana', True]
print(my_list)
# Accessing elements (starts at index 0)
print(my_list[0])  # Output: 1
print(my_list[3])  # Output: apple

# Modifying elements
my_list[1] = 4
print(my_list) # Output: [1, 4, 3, 'apple', 'banana', True]

# Adding and removing elements
my_list.append('orange')
print(my_list) # Output: [1, 4, 3, 'apple', 'banana', True, 'orange']
my_list.remove('banana')
print(my_list) # Output: [1, 4, 3, 'apple', True, 'orange']

Tuples

Tuples are ordered, immutable (unchangeable) collections of items. They are defined using parentheses (). Once a tuple is created, you cannot add, remove, or modify its elements.

my_tuple = (1, 2, 3, 'apple')
print(my_tuple)
print(my_tuple[0]) # Output: 1
# my_tuple[0] = 4  # This would raise an error because tuples are immutable

Tuples are often used to represent data that should not be changed, such as coordinates or fixed sets of values.

Dictionaries

Dictionaries are unordered collections of key-value pairs. They are defined using curly braces {}. Keys must be unique and immutable (like strings or numbers), while values can be any data type.

my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(my_dict)

# Accessing values using keys
print(my_dict['name'])  # Output: Alice
print(my_dict['age'])   # Output: 30

# Modifying values
my_dict['age'] = 31
print(my_dict)

# Adding a new key-value pair
my_dict['occupation'] = 'Data Scientist'
print(my_dict)

# Removing a key-value pair
del my_dict['city']
print(my_dict)

Sets

Sets are unordered collections of unique items. They are defined using curly braces {} but unlike dictionaries, they only store values, not key-value pairs. Sets are useful for removing duplicate values and performing mathematical set operations.

my_set = {1, 2, 3, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5} (duplicates are automatically removed)

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

# Removing an element
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}
print(set1.difference(set2)) # Output: {1, 2}
Progress
0%