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

Check what you've learned