Diccionarios
La estructura más usada de Python. Guarda datos con etiqueta, no por posición.
Dictionaries
Python's most used structure. It stores data with labels, not by position.
Imagina guardar los datos de un jugador en una lista:
jugador = ["Joaco", 1500, 3]
¿Qué es el 1500? ¿Puntos, monedas? ¿Y el 3, vidas o nivel? Tendrías que recordar el orden exacto para siempre.
Un diccionario le pone nombre a cada dato. Va entre llaves { },
con "clave": valor.
Imagine storing a player's data in a list:
player = ["Joaco", 1500, 3]
What is 1500? Points, coins? And the 3, lives or level? You would have to remember the exact order forever.
A dictionary names each value. It goes between braces { }, as
"key": value.
Cada dato se pide por su clave, entre corchetes. Se lee solo, sin recordar ningún orden.
Each value is asked for by its key, in brackets. It reads by itself, no order to remember.
Funcionan como variables: puedes cambiar valores y crear claves nuevas sobre la marcha.
Pero pedir una clave que no existe da error. Para eso está
.get(), que devuelve un valor por defecto en vez de romperse.
They work like variables: you can change values and create new keys on the fly.
But asking for a key that does not exist is an error. That is what
.get() is for: it returns a default instead of breaking.
Con .items() recorres clave y valor a la vez. Es la forma más usada.
With .items() you loop over key and value at once. It is the most used form.
Acá se juntan las dos ideas, y así está armado prácticamente todo el software del mundo: una lista de diccionarios.
Una tabla de puntajes, un catálogo de productos, los datos que devuelve cualquier servicio de internet. Todos son esto — de hecho, el formato JSON que usan las aplicaciones para intercambiar datos es exactamente esta estructura.
Here the two ideas meet, and it is how practically all software in the world is built: a list of dictionaries.
A scoreboard, a product catalogue, the data any internet service returns. All of them are this — in fact the JSON format apps use to exchange data is exactly this structure.
Variables, decisiones, bucles, listas, funciones y diccionarios. Con eso puedes resolver la enorme mayoría de los problemas de programación.
Lo que viene son herramientas: leer archivos, automatizar tareas, hacer gráficos. Pero las ideas ya las tienes todas.
Y si te da curiosidad el otro lado —dibujar, animar, hacer juegos en el navegador—, ahí tienes el curso de JavaScript. Vas a reconocer todo, solo cambia cómo se escribe.
Variables, decisions, loops, lists, functions and dictionaries. With those you can solve the vast majority of programming problems.
What follows are tools: reading files, automating tasks, making charts. But you already have all the ideas.
And if the other side tempts you — drawing, animating, making browser games — there is the JavaScript course. You will recognise everything; only the writing changes.