2025-06-03
What Are Data Structures?
So far, you've learned about basic data types like int, str, and bool. These are for storing a single value.
But in real programs, you often want to handle "all students' names" or "a list of test scores"—in other words, many pieces of data together.
That's where "data structures" come in.
- Data Structure: A way to efficiently store and manage multiple pieces of data
- Types: Arrays (lists), dictionaries, sets, and more
- Purpose: To organize, search, add, and remove data efficiently
In this chapter, you'll learn about the most basic and important data structure: the array (list).
What Is an Array (List)?
An array (list) is a "box" that can store multiple values in order.
- Features:
- Manage many pieces of the same kind of data together
- Has an order (first, second, ...)
- You can add, remove, or change values later
In Python, it's called a "list," but in other languages, it's often called an "array."
How to Make an Array (List)
# List of strings
fruits = ["apple", "orange", "banana"]
# List of numbers
scores = [85, 92, 78, 96, 88]
# Empty list (add data later)
students = []- Use
[](square brackets) and separate values with commas - Usually, you group the same kind of data, but you can mix types
- You can also make an empty list
Basic List Operations
Accessing Elements (Index)
fruits = ["apple", "orange", "banana"]
print(fruits[0]) # apple (first)
print(fruits[1]) # orange (second)
print(fruits[2]) # banana (third)- List numbering (index) starts at 0
- Use
list_name[number]to get the value at that position
Adding and Removing Elements
fruits = ["apple", "orange"]
# Add to the end
fruits.append("banana")
print(fruits) # ['apple', 'orange', 'banana']
# Insert at a specific position
fruits.insert(1, "strawberry")
print(fruits) # ['apple', 'strawberry', 'orange', 'banana']
# Remove an element
fruits.remove("orange")
print(fruits) # ['apple', 'strawberry', 'banana']Checking List Information
scores = [85, 92, 78, 96, 88]
print(len(scores)) # 5 (number of elements)
print(max(scores)) # 96 (max value)
print(min(scores)) # 78 (min value)
print(sum(scores)) # 439 (total)Why Use Arrays?
Why do we need arrays? Let's look at an example.
Bad Example: Making Lots of Variables
student1 = "Taro"
student2 = "Hanako"
student3 = "Jiro"
student4 = "Misaki"
print(student1)
print(student2)
print(student3)
print(student4)Problems with this method:
- You need a new variable for every student
- It's hard to process them all together
- The program gets long
Good Example: Using an Array
students = ["Taro", "Hanako", "Jiro", "Misaki"]
print(students[0]) # Taro
print(students[1]) # Hanako
print(students[2]) # Jiro
print(students[3]) # MisakiWith this method:
- You manage lots of data with one variable
- It's easy to add more students
- The program is cleaner
Basic List Operations
Creating a List
# List of strings
fruits = ["apple", "orange", "banana"]
# List of numbers
scores = [85, 92, 78, 96, 88]
# Empty list (add data later)
shopping_list = []Accessing Elements (Index)
fruits = ["apple", "orange", "banana"]
print(fruits[0]) # apple (first)
print(fruits[1]) # orange (second)
print(fruits[2]) # banana (third)Important: List numbering (index) starts at 0
Adding and Changing Elements
fruits = ["apple", "orange"]
# Add to the end
fruits.append("banana")
print(fruits) # ['apple', 'orange', 'banana']
# Change an element
fruits[1] = "strawberry"
print(fruits) # ['apple', 'strawberry', 'banana']Checking List Information
scores = [85, 92, 78, 96, 88]
print(len(scores)) # 5 (number of elements)
print(max(scores)) # 96 (max value)
print(min(scores)) # 78 (min value)Summary
In this chapter, you learned the basics of data structures and the most important one: arrays (lists).
Key Points
- Data Structure: A way to efficiently manage multiple pieces of data
- Creating a List:
[item1, item2, item3] - Getting Elements:
list_name[index](starts at 0) - Adding Elements:
list_name.append(value)
Benefits of Lists
- Manage related data together
- Cleaner, easier-to-read programs
- Easy to add or change data
In the next chapter, you'll learn about loops (for statements) using lists. Combining lists and for loops lets you write even more powerful and practical programs.