**Python Fundamentals Continued & Introduction to Data Structures

This lesson builds upon the Python fundamentals introduced in Day 1, focusing on data structures – the building blocks for organizing and manipulating data. You will learn about lists, tuples, dictionaries, and sets, understanding their key characteristics and how to use them to solve basic programming problems.

Learning Objectives

  • Define and differentiate between the four primary Python data structures: lists, tuples, dictionaries, and sets.
  • Understand how to create, access, and modify elements within each data structure.
  • Apply data structures to store and retrieve data relevant to simple data science tasks.
  • Explain the concept of mutability and immutability in the context of Python data structures.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Data Structures

Data structures are fundamental in programming, especially in data science. They are ways of organizing and storing data to be efficiently accessed and modified. Python offers several built-in data structures, each with its strengths and weaknesses. We will focus on lists, tuples, dictionaries, and sets.

Lists

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

Example:

my_list = [1, 'hello', 3.14, [4, 5]]
print(my_list)
print(my_list[0])  # Accessing the first element (index 0)
my_list[1] = 'world' # Modifying an element
print(my_list)

Tuples

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

Example:

my_tuple = (1, 'hello', 3.14)
print(my_tuple)
print(my_tuple[1]) # Accessing elements
# my_tuple[0] = 2  # This will cause an error - you can't modify a tuple

Dictionaries

Dictionaries are unordered collections of key-value pairs. They are defined using curly braces {}. Keys must be unique and immutable (e.g., strings, numbers, tuples), while values can be of any data type. Dictionaries are incredibly useful for storing data where you need to quickly look up information by a specific key.

Example:

my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(my_dict)
print(my_dict['age'])  # Accessing a value by its key
my_dict['occupation'] = 'Data Scientist' # Adding a new key-value pair
print(my_dict)

Sets

Sets are unordered collections of unique elements. They are defined using curly braces {} (similar to dictionaries, but without key-value pairs). Sets are useful for removing duplicate values and performing mathematical set operations like union, intersection, and difference.

Example:

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

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

Mutability vs. Immutability

Understanding mutability is crucial. Mutable objects (like lists and dictionaries) can be changed after they are created. Immutable objects (like tuples, strings, and numbers) cannot. Modifying a mutable object directly alters its memory location, while attempting to 'modify' an immutable object creates a new object in a new memory location.

Progress
0%