Variables and data types in Python
Dynamic typing, int, float, str, bool, f-strings and type conversion, with labs to try it right away.
En Python no declaras el tipo de una variable: escribes x = 5 y Python
deduce que es un entero. Es tipado dinámico: la misma variable puede guardar después un texto.
Los tipos básicos son int, float, str y bool.
edad = 25 # int
altura = 1.75 # float
nombre = "Ana" # str
es_mayor = True # bool
print(type(edad)) # <class 'int'>
Type inspector
Escribe un valor y Python te dice qué tipo infiere, igual que type().
Dynamic typing
Una variable puede cambiar de tipo al reasignarla. Útil, pero úsalo con cuidado.
f-strings: text with variables
Poniendo una f delante de la comilla, puedes meter variables (y expresiones) entre llaves.
input() and type conversion
input() siempre devuelve un texto (str), aunque el usuario escriba un
número. Para operar con él hay que convertirlo con int() o float().
Quick table
| Expresión | Tipo | Ejemplo |
|---|---|---|
| Entero | int | edad = 25 |
| Decimal | float | precio = 9.99 |
| Texto | str | nombre = "Ana" |
| Booleano | bool | activo = True |
| Sin valor | NoneType | resultado = None |
Check what you've learned
edad = input("Edad: ") — ¿de qué tipo es edad?x = 5 y luego x = "hola". ¿Qué pasa?nombre = "Ana", ¿qué imprime print(f"Hola {nombre}")?