Mastering Python: Days 4 — Data Structures

Risky Mulya Nugraha
3 min readSep 2, 2023

--

Welcome to Day 4 of your Python learning journey! Today, we’ll explore fundamental data structures in Python, including lists, tuples, dictionaries, and sets. These data structures are essential for organizing and manipulating data efficiently.

Part 1: Lists and Tuples

Lists and tuples are versatile data structures that allow you to store and manage collections of items. They are both ordered and iterable, but they have some key differences:

  • Lists: Lists are mutable, meaning you can change their elements after creation. They are defined using square brackets [].
  • Tuples: Tuples are immutable, meaning once you create them, you cannot change their elements. They are defined using parentheses ().

Example: Working with Lists and Tuples

# Lists
fruits = ["apple", "banana", "cherry"]

# Add an element
fruits.append("orange") # result: ['apple', 'banana', 'cherry', 'orange']

# Modify an element
fruits[1] = "kiwi" # result: ['apple', 'kiwi', 'cherry', 'orange']



# Tuples
colors = ("red", "green", "blue")
# colors[0] = "yellow" # This will raise an error (immutable)

Both lists and tuples support indexing and slicing to access their elements.

  • Indexing: You can access individual elements of a list or tuple by using their index. In Python, indexing starts from 0 for the first element.
# Access the first element of the list
first_fruit = fruits[0] # result: apple

# Access the second element of the tuple
second_color = colors[1] # result: green
  • Slicing: Slicing allows you to extract a portion of a list or tuple. It is done by specifying a start index and an end index separated by a colon.
# Get elements at index 1 and 2 (exclusive of 3)
selected_fruits = fruits[1:3] # result: ['kiwi', 'cherry']

# Get elements up to index 1
selected_colors = colors[:2] # result: ('red', 'green')

Part 2: Dictionaries

Dictionaries are another crucial data structure in Python. They are used to store collections of key-value pairs. Dictionaries are unordered but highly efficient for data retrieval.

Example: Working with Dictionaries

# Dictionary
person = {
"name": "Allie",
"age": 21,
"city": "New York"
}
print(person["name"]) # Accessing values by key
person["age"] = 31 # Modifying a value

Dictionaries are excellent for storing and retrieving data when you have a unique identifier (key) for each piece of information.

Part 3: Sets

Sets are data structures that store unique elements, ensuring that each item appears only once. They are useful for eliminating duplicates and performing set operations like union, intersection, and difference.

Example: Working with Sets

# Sets
fruits = {"apple", "banana", "cherry", "banana"}
unique_fruits = set(fruits) # Creating a set from a list
unique_fruits.add("orange") # Adding an element


# Set operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Union of sets
union = set1 | set2 # result {1, 2, 3, 4, 5}

# Intersection of sets
intersection = set1 & set2 # result {3}

# Difference between sets
difference = set1 - set2 # result {1, 2}

Sets are valuable when you need to work with distinct elements or perform operations involving multiple sets.

Exercise:

  • Practice creating and manipulating lists, tuples, dictionaries, and sets.
  • Explore more advanced operations and methods available for these data structures.

By the end of Day 4, you’ll have a solid understanding of essential data structures in Python, setting the foundation for more complex programming tasks. Next, we will delve into Functions and Modules in Python.

--

--