- Lists (mutable) and tuples (immutable) allow you to store ordered collections of elements of different types.
- Lists support operations such as append, remove and index access; their size is obtained with len().
- Dictionaries use key-value pairs for efficient data access using unique keys.
- Sets store unique elements and allow union, intersection, and difference between collections.
Welcome to the exciting section on Working with Data in Python ! Here we'll discover the fundamental tools for efficiently storing, organizing, and manipulating information in our programs.
We'll begin by exploring two essential data structures: lists and tuples. These allow us to store multiple elements in an ordered manner and perform common operations on them. We'll learn how to add, remove, and access elements in these structures, giving us the power to manage data efficiently.
Next, we'll delve into the world of dictionaries, where we'll learn how to use key-value pairs to quickly access and modify information. We'll also explore sets, which allow us to manage collections of unique elements and perform mathematical operations between them.
Knowing these data structures is essential for working with data in Python and developing more robust and flexible programs. Get ready to master the art of organizing and manipulating information accurately as you embark on this exciting journey through Working with Data in Python. Let’s start building a solid foundation for your programming skills!
Home Page of the Python Course from Scratch
4.1 Lists and Tuples
Using lists and tuples to store and manipulate data in an orderly manner
In Python, lists and tuples are data structures that allow us to store multiple elements of different types in a single variable. The main difference between them lies in their mutability: lists are mutable (they can be modified), while tuples are immutable (once created, they cannot be changed).
To create a list, we simply place the elements in brackets [] and we separate them by commas. For example:
frutas = ["manzana", "plátano", "naranja", "kiwi"]
To create a tuple, we place the elements in parentheses () and we separate them by commas:
coordenadas = (10, 20)
Common operations with lists and tuples
Both data structures allow us to perform common operations such as adding elements, removing elements, accessing specific elements, and getting their length.
Example of operations with lists:
numeros = [1, 2, 3, 4, 5] # Agregar un elemento al final de la lista numeros.append(6) # Eliminar un elemento específico de la lista numeros.remove(3) # Acceder a un elemento por índice tercer_elemento = numeros[2] # Longitud de la lista longitud_lista = len(numeros)
Example of operations with tuples:
coordenadas = (10, 20) # Acceder a un elemento por índice x = coordenadas[0] y = coordenadas[1] # Longitud de la tupla longitud_tupla = len(coordenadas)
4.2 Dictionaries and Sets
Explaining Dictionaries and Sets as Useful Data Structures in Python
Dictionaries and sets are two other important data structures in Python.
Dictionaries are collections of key-value pairs, where each key must be unique. They allow their values to be accessed using a key rather than an index, making them ideal for storing and accessing data quickly and efficiently. An example of a dictionary is:
estudiantes = {
"Juan": 25,
"María": 28,
"Carlos": 22
}
Sets are unordered collections of unique elements. They allow mathematical operations such as union, intersection and difference between sets to be performed easily. An example of a set is:
numeros_primos = {2, 3, 5, 7, 11}
How to access, modify and iterate over dictionaries and sets
To access the values of a dictionary, we use the corresponding key:
estudiantes = {
"Juan": 25,
"María": 28,
"Carlos": 22
}
edad_maria = estudiantes["María"]
If we want to modify the value associated with a key, we simply assign a new value:
estudiantes["Carlos"] = 23
To iterate over a dictionary, we can use a loop for:
for nombre, edad in estudiantes.items():
print(f"{nombre} tiene {edad} años")
As for sets, we can perform operations such as union, intersection and difference of sets using the methods union(), intersection() y difference(), respectively:
numeros_primos = {2, 3, 5, 7, 11}
numeros_pares = {2, 4, 6, 8, 10}
union_numeros = numeros_primos.union(numeros_pares)
interseccion_numeros = numeros_primos.intersection(numeros_pares)
diferencia_numeros = numeros_primos.difference(numeros_pares)
In short, lists, tuples, dictionaries, and sets are versatile and powerful data structures that allow us to efficiently store, organize, and manipulate data in Python. As we explore these structures in depth, you'll be ready to work with a wide variety of data and solve more complex problems in your programming projects.