if, elif y else
Que el programa elija qué hacer. Y de paso, la regla más particular de Python.
if, elif and else
Making the program choose. And with it, Python's most distinctive rule.
La estructura es: if, la pregunta, dos puntos, y debajo lo que
hay que hacer con sangría.
Esos dos elementos —los dos puntos y la sangría— son obligatorios. No se pueden olvidar.
The structure is: if, the question, a colon, and below it
what to do indented.
Those two things — the colon and the indentation — are mandatory. You cannot skip them.
Esta es la diferencia más grande de Python con casi todos los demás
lenguajes. Otros usan llaves { } para marcar dónde empieza y termina un bloque.
Python usa los espacios del principio de la línea.
Suena frágil, pero tiene una ventaja: obliga a que el código quede ordenado. No existe el Python mal indentado — simplemente no funciona.
La convención son 4 espacios. Y no mezcles espacios con tabulaciones, porque eso produce errores difíciles de ver.
This is Python's biggest difference from almost every other language.
Others use braces { } to mark where a block starts and ends. Python uses the
spaces at the start of the line.
It sounds fragile, but it has an upside: it forces tidy code. Badly indented Python does not exist — it simply does not run.
The convention is 4 spaces. And do not mix spaces with tabs, because that causes errors that are hard to spot.
Ahora mira qué pasa cuando la sangría no calza:
Now see what happens when the indentation does not line up:
else es el plan B: se ejecuta cuando la condición del if no se
cumple.
Y cuando hay más de dos caminos, se encadenan con elif — abreviatura de
else if. Esto es propio de Python: en JavaScript y otros lenguajes se
escribe else if, separado.
Se leen en orden, de arriba hacia abajo, y en cuanto uno se cumple, los demás ni se miran.
else is plan B: it runs when the if condition is not met.
And when there are more than two paths, you chain them with elif — short for
else if. This is Python-specific: in JavaScript and others you write
else if, separated.
They are read in order, top to bottom, and as soon as one matches, the rest are not even checked.
Ojo con esto: = con uno guarda, == con dos
compara. Confundirlos es un error clásico.
Y para combinar condiciones, Python usa palabras en vez de símbolos — otra razón por la que se lee tan fácil:
Careful: = with one stores, == with two
compares. Mixing them up is a classic mistake.
And to combine conditions, Python uses words instead of symbols — another reason it reads so easily:
| Python | JavaScript | |
|---|---|---|
and | && | |
or | || | |
not | ! | |
!= | !== |
if, elif y else. Que los dos puntos y la sangría son
obligatorios. Que = guarda y == compara. Y combinar con
and, or y not.
Falta una idea para poder hacer casi cualquier cosa: repetir.
if, elif and else. That the colon and indentation are
mandatory. That = stores and == compares. And combining with
and, or and not.
One idea left before you can build almost anything: repeating.