Python comes with several built-in data structures that are essential for writing effective programs. Let's explore the four primary ones.
1. List []
A list is a collection of items that is ordered and mutable (changeable). Lists can contain items of different data types.
- Ordered: The items have a defined order, and that order will not change.
- Mutable: You can add, remove, or change items after the list has been created.
Python
# Creating a list
fruits = ["apple", "banana", "cherry"]
print(f"Original list: {fruits}")
# Accessing items by index (starts at 0)
print(f"First fruit: {fruits[0]}") # Output: apple
# Changing an item
fruits[1] = "blueberry"
print(f"Changed list: {fruits}")
# Adding an item to the end
fruits.append("orange")
print(f"Appended list: {fruits}")
# Removing an item
fruits.remove("apple")
print(f"Final list: {fruits}")
2. Tuple ()
A tuple is a collection that is ordered and immutable (unchangeable). Once a tuple is created, you cannot change its values. They are often used for data that should not be modified, like coordinates (x, y).
- Ordered: Items have a defined order.
- Immutable: You cannot add, remove, or change items.
Python
# Creating a tuple
coordinates = (10.0, 20.0)
print(f"Coordinates: {coordinates}")
# Accessing items by index
print(f"Latitude: {coordinates[0]}")
# Trying to change an item will cause an error!
# coordinates[0] = 15.0 # This would raise a TypeError
Because they are immutable, tuples can be slightly more memory-efficient and faster than lists.
3. Dictionary {key: value}
A dictionary (dict) is a collection of key-value pairs. It is unordered (in Python versions before 3.7) and mutable. Each key must be unique and is used to access its corresponding value.
- Key-Value Pairs: Data is stored as a key associated with a value.
- Mutable: You can add, change, or remove key-value pairs.
- Unique Keys: A dictionary cannot have two keys with the same name.
Python
# Creating a dictionary
student = {
"name": "John Doe",
"age": 21,
"major": "Computer Science"
}
print(f"Student info: {student}")
# Accessing a value by its key
print(f"Student's name: {student['name']}")
# Adding a new key-value pair
student["gpa"] = 3.8
print(f"Updated info: {student}")
# Changing a value
student["age"] = 22
print(f"Final info: {student}")
4. Set {}
A set is a collection that is unordered, unindexed, and contains no duplicate items. Sets are mutable. Their primary use is to test for membership and to perform mathematical set operations like union, intersection, and difference.
- Unordered: Items do not have a defined order.
- Unique: Every item in a set must be unique.
- Mutable: You can add or remove items.
Python
# Creating a set (duplicates are automatically removed)
numbers = {1, 2, 3, 3, 4, 5, 5}
print(f"Set of numbers: {numbers}") # Output: {1, 2, 3, 4, 5}
# Adding an item
numbers.add(6)
print(f"Set after adding 6: {numbers}")
# Checking for membership (this is very fast!)
print(f"Is 3 in the set? {3 in numbers}") # Output: True
# Set operations
set_a = {1, 2, 3}
set_b = {3, 4, 5}
print(f"Union: {set_a | set_b}") # Output: {1, 2, 3, 4, 5}
print(f"Intersection: {set_a & set_b}") # Output: {3}