When you first learn Python, you usually start with simple variables that hold a single value—like 5, True, or "hello". That’s a great start, but real programs almost always need to work with collections of values.
That’s where data structures come in. In this tutorial, you’ll learn four of the most important built-in Python data structures:
Along the way, you’ll see practical examples, learn how each structure behaves, and understand when to use which one.
[Data structures overview / diagram]
Imagine you’re building a small app that stores:
If you try to store everything in separate variables like score1, score2, score3, you’ll quickly run into messy code. Data structures let you keep related items together in a single variable, making your code cleaner, more scalable, and easier to maintain.
A list is the most commonly used collection in Python. It can store multiple values in a single variable, and you can modify it (add, remove, update values).
my_list = []
print(my_list)
Code language: PHP (php)
This creates an empty list.
[ Empty list output in Python]
my_list = [10, 20, 30, 40]
print(my_list)
Code language: PHP (php)
This list has 4 elements.
Lists can store strings:
names = ["Alice", "Bob", "Charlie"]
print(names)
Code language: PHP (php)
They can also store mixed types:
mixed = ["Python", 100, True, 3.14]
print(mixed)
Code language: PHP (php)
Python allows this flexibility, but in real projects you’ll often keep lists consistent (e.g., a list of numbers or a list of strings) for clarity.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix)
Code language: PHP (php)
This is called a list of lists. Each inner list is its own list.
[ Nested list output]
len()Use len() to get how many elements are in a list:
numbers = [10, 20, 30, 40]
print(len(numbers)) # 4
Code language: PHP (php)
Important detail: for a list of lists, len() counts only the outer elements.
list_of_lists = [[1, 2], [3, 4], [5, 6]]
print(len(list_of_lists)) # 3
Code language: PHP (php)
Even though each inner list has values, the outer list contains three items, so the length is 3.
append()Lists are mutable, which means you can change them after creating them.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # [1, 2, 3, 4]
Code language: PHP (php)
You can keep appending:
my_list.append(6)
print(my_list) # [1, 2, 3, 4, 6]
Code language: CSS (css)
A set is similar to a list, but it has two major differences:
Sets use curly braces {}:
my_set = {1, 2, 3, 4, 5}
print(my_set)
Code language: PHP (php)
You can check its type:
print(type(my_set))
Code language: PHP (php)
And you can check its size:
print(len(my_set))
Code language: PHP (php)
[ Printing a set and its type]
my_set = {1, 1, 1, 2, 2}
print(my_set) # {1, 2}
print(len(my_set)) # 2
Code language: PHP (php)
Even if you try to add duplicates, the set keeps only one copy.
Lists care about order:
print([1, 2] == [1, 2]) # True
print([1, 2] == [2, 1]) # False
Code language: PHP (php)
Sets do not care about order:
print({1, 2} == {2, 1}) # True
Code language: PHP (php)
Even if you write elements in a different order, it still represents the same set.
A tuple is like a list in the sense that it stores items in order, but the biggest difference is:
✅ Tuples cannot be changed after creation (they are immutable).
Tuples use parentheses ():
my_tuple = (1, 2, 3)
print(my_tuple)
print(len(my_tuple)) # 3
Code language: PHP (php)
print((1, 2) == (1, 2)) # True
print((1, 2) == (2, 1)) # False
Code language: PHP (php)
This works with lists:
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
Code language: PHP (php)
But tuples don’t support append():
my_tuple = (1, 2, 3)
# my_tuple.append(4) # AttributeError
Code language: PHP (php)
If you try, Python raises an error because tuples are not meant to be modified.
[ Tuple append error message]
If tuples are less flexible, why do programmers use them?
One big reason is efficiency. Because Python knows a tuple won’t grow or shrink, it can store it more compactly in memory compared to a list.
A very common real-world use case is storing fixed pairs like coordinates:
point = (10, 20) # (x, y)
Code language: PHP (php)
If your program stores thousands or millions of coordinate pairs, tuples can be a good choice.
A dictionary (also called dict) stores values in key-value pairs.
Think of it like a real dictionary:
Dictionaries also use curly braces {}, but inside they contain key-value pairs separated by colons :.
my_dict = {
"apple": "a red fruit",
"bear": "a scary animal"
}
print(my_dict)
Code language: PHP (php)
[ Dictionary printed output]
print(my_dict["apple"]) # a red fruit
Code language: PHP (php)
Here:
"apple" is the key"a red fruit" is the valueIf you assign the same key again, the new value replaces the old one.
my_dict = {
"apple": "a red fruit",
"apple": "sometimes a green fruit"
}
print(my_dict["apple"]) # sometimes a green fruit
Code language: PHP (php)
This replacement behavior is important to remember when working with dictionaries.
In dictionaries, you don’t use “position” like in lists or tuples. You always access values by key.
This makes dictionaries perfect for problems like:
{"name": "...", "age": ...})Use lists when you need:
Use sets when you need:
Use tuples when you need:
(x, y))Use dictionaries when you need:
"apple" → "red fruit"){1, 1, 2, 2, 3}. Print the set and explain what happened.("Jan", "Feb", "Mar"). Try to modify one value and observe the error.{"CPU": "...", "RAM": "..."}. Print the meaning of one key.[ Exercise outputs]
Now that you’ve met Python’s core data structures, you’re ready to use them in real programming patterns—looping through lists, filtering sets, grouping data in dictionaries, and building structured data with tuples.
In the next lessons, you’ll use these structures repeatedly, and they’ll quickly start to feel natural.