Variables y tipos
Un programa que no recuerda nada no sirve de nada. Acá aprendes a guardar datos.
Variables and types
A program that remembers nothing is useless. Here you learn to store data.
Una variable es una caja donde guardas algo con un nombre, para pedirlo
después. En Python no hace falta anunciarla: le pones nombre, un =, y el valor.
El = no significa "es igual a" como en matemáticas. Significa
"guarda esto acá".
A variable is a box where you store something with a name, to ask for it
later. In Python you do not declare it: just a name, an =, and the value.
The = does not mean "equals" like in maths. It means
"store this here".
Cada dato tiene un tipo, y Python lo deduce solo. Estos cuatro cubren casi todo:
str— texto, siempre entre comillas. String en inglés.int— número entero, sin decimales.float— número con decimales. Ojo: se escribe con punto, no con coma.bool— verdadero o falso:TrueoFalse, con mayúscula.
La función type() te dice cuál es cada uno:
Each value has a type, and Python works it out by itself. These four cover almost everything:
str— text, always in quotes. Short for string.int— whole number, no decimals.float— number with decimals. Note: written with a dot.bool— true or false:TrueorFalse, capitalised.
The type() function tells you which is which:
El símbolo + hace cosas distintas según el tipo: con números suma,
con textos los pega.
Y si los mezclas, Python se niega — a diferencia de otros lenguajes que inventan un resultado raro. Eso en realidad es bueno: te avisa en vez de dejarte con un bug silencioso.
The + symbol does different things by type: with numbers it adds,
with text it joins.
And if you mix them, Python refuses — unlike other languages that invent a strange result. That is actually good: it warns you instead of leaving a silent bug.
Ese TypeError es de los más frecuentes al empezar. Hay
dos formas de arreglarlo, y la segunda es la que usa todo el mundo hoy:
That TypeError is among the most common when starting.
There are two fixes, and the second is what everyone uses today:
input() le pide algo a quien usa el programa. Y acá va la trampa clásica:
lo que devuelve siempre es texto, aunque escriban un número.
Por eso casi siempre verás int(input(...)): pedir, y convertir de inmediato.
Como acá no hay teclado disponible, simulamos la respuesta:
input() asks the person using the program for something. And here is the
classic trap: what it returns is always text, even if they type a number.
That is why you will almost always see int(input(...)): ask, then convert
immediately.
Since there is no keyboard here, we simulate the answer:
Crear variables, los cuatro tipos básicos, por qué no se puede sumar texto con número, las
f-strings, y convertir con int() y float().
Ahora que el programa recuerda, hagamos que decida.
Creating variables, the four basic types, why text and numbers cannot be added, f-strings,
and converting with int() and float().
Now that it remembers, let us make it decide.