Unit 4 • Lesson 1

Introduction to Data Structures

Overview

Learn what data structures are and why they matter. You'll understand how they let programs manage and organize data for real-world applications such as analytics and databases, and discover the fundamental role they play in efficient programming.

Beginner 15–20 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • What data structures are: Understand how data structures organize and store information.
  • Why they matter: Learn why choosing the right data structure makes programs more efficient.
  • Types of data structures: Discover the main data structures in Python.
  • When to use each: Understand which data structure fits different situations.

Why This Matters

So far, you've worked with individual variables that store single values. But real programs need to work with collections of data - like a list of students, a dictionary of user information, or a set of unique items. Data structures let you organize and manage multiple pieces of data efficiently. Understanding data structures is essential for writing programs that can handle real-world data!

Step 1: What Is a Data Structure?

A data structure is a way of organizing and storing data in a computer so that it can be accessed and used efficiently. Think of it like different types of containers - each container is designed for a specific purpose and has different ways of storing and accessing items.

Simple Variables vs Data Structures
# Single variable - stores one value
name = "Alice"

# Data structure - stores multiple values
names = ["Alice", "Bob", "Charlie"]
1

Single Variables

Regular variables like name = "Alice" store one piece of data. This works fine when you only need to remember one thing.

2

Data Structures

Data structures like names = ["Alice", "Bob", "Charlie"] store multiple pieces of data together. This lets you work with collections of information.

3

Organization

Different data structures organize data in different ways. Some keep items in order, some allow duplicates, some don't. Each has advantages for different situations.

Key Concept: Data structures are like different types of storage containers. A list is like a shopping cart where items stay in order. A dictionary is like a filing cabinet where you find things by their label. A set is like a collection where duplicates aren't allowed. Each structure is designed for specific needs!

Mini Practice #1: Understanding Data Structures

Try It Yourself

Try creating different types of data structures:

Press Run to see output

What happened? You can see the difference between storing a single value in a variable versus storing multiple values in data structures. The list fruits stores multiple items in order. The dictionary person stores information organized by keys (like "name" and "age"). Each data structure has its own way of organizing and accessing data!

Step 2: Why Do Data Structures Matter?

Data structures matter because they help you:

Organize Data

Keep related information together in a logical way.

students = ["Alice", "Bob"]

Access Efficiently

Find and use data quickly based on how it's organized.

grades = {"Alice": 95}

Real-World Example

Imagine you're building a program to manage a library. You could use a list to store all the books in order. You could use a dictionary to look up a book by its ISBN number. You could use a set to keep track of which books are currently checked out (no duplicates). Each data structure helps solve a different part of the problem!

Step 3: Main Data Structures in Python

Python has four main built-in data structures:

Lists

Ordered collections that can change. Items stay in order and duplicates are allowed.

[1, 2, 3]

Tuples

Ordered collections that cannot change. Like lists but immutable.

(1, 2, 3)

Dictionaries

Key-value pairs. Find values by their keys, like a real dictionary.

{"key": "value"}

Sets

Unordered collections of unique items. No duplicates allowed.

{1, 2, 3}

Remember: Each data structure has different properties. Lists are ordered and changeable. Tuples are ordered but unchangeable. Dictionaries store key-value pairs. Sets are unordered and only store unique values. You'll learn about each one in detail in the following lessons!

Mini Practice #2: Exploring Data Structures

Try It Yourself

Try creating and printing different data structures:

Press Run to see output

What happened? You created all four main data structures! Notice how each one looks different. The list uses square brackets [], the tuple uses parentheses (), the dictionary uses curly braces with colons {}, and the set uses curly braces {}. Also notice that the set only shows unique values - even though you put 3 twice, it only appears once in the set!

Step 4: When to Use Each Data Structure

Choosing the right data structure depends on what you need:

1

Use Lists

When you need an ordered collection that can change. Perfect for shopping carts, to-do lists, or any sequence where order matters and items might be added or removed.

2

Use Tuples

When you need an ordered collection that shouldn't change. Great for coordinates, dates, or any data that should stay fixed once created.

3

Use Dictionaries

When you need to look up values by keys. Perfect for user profiles, settings, or any data where you need to find information by a specific label.

4

Use Sets

When you need unique items and don't care about order. Great for tracking unique visitors, removing duplicates, or checking membership.

Tip: Don't worry if you're not sure which one to use yet. As you learn more about each data structure in the following lessons, you'll develop a sense for which one fits each situation. The key is understanding what each structure can do!

End-of-Lesson Exercises

Exercise 1: Create Different Data Structures

Create a list with three colors, a tuple with two numbers representing coordinates, a dictionary with your name and age, and a set with five numbers (include some duplicates). Print each one.

Use square brackets for lists, parentheses for tuples, curly braces with colons for dictionaries, and curly braces for sets.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Identify Data Structure Types

Create variables for: a list of three fruits, a tuple of two coordinates (x, y), a dictionary mapping "city" to "New York", and a set with numbers 1, 2, 2, 3. Print each and explain what type it is in a comment.

Use comments to label each data structure type.

Write your code above and click "Check Answer" to verify it's correct.