Módulos: usar código que ya existe
La mayor fortaleza de Python no es el lenguaje: es todo lo que ya está escrito y listo para usar.
Modules: using code that already exists
Python's greatest strength is not the language: it is everything already written and ready to use.
Un módulo es un conjunto de funciones que alguien ya escribió. Para usarlo
se escribe import y su nombre.
Python trae decenas incluidos, sin instalar nada. Empecemos por el más entretenido:
random, para cosas al azar.
A module is a set of functions someone already wrote. To use it you write
import and its name.
Python ships with dozens built in, nothing to install. Let us start with the most fun one:
random, for anything by chance.
Las fechas son sorprendentemente difíciles de manejar a mano — meses de distinto largo, años
bisiestos, zonas horarias. Por eso nadie lo hace: se usa datetime.
Acá aparece otra forma de importar: from … import …, que trae solo lo que
necesitas y te ahorra escribir el prefijo.
Dates are surprisingly hard to handle by hand — months of different lengths, leap years,
time zones. So nobody does: you use datetime.
Here is another way of importing: from … import …, which brings only what you
need and saves you the prefix.
math trae lo que no viene de fábrica: raíces, redondeos, constantes y
trigonometría.
math brings what is not built in: roots, rounding, constants and trigonometry.
Además de los módulos incluidos, existe un universo de librerías hechas por la comunidad. Ahí está la verdadera razón por la que Python domina en datos e inteligencia artificial.
requests— pedir datos a internet.pandas— trabajar con tablas y planillas.matplotlib— hacer gráficos.openpyxl— leer y escribir archivos de Excel.
En tu computador se instalan con pip, que viene con Python. Desde la terminal:
pip install pandas
Y después se usa con import pandas, igual que los
incluidos.
Beyond the built-in modules there is a universe of community-made libraries. That is the real reason Python dominates data and artificial intelligence.
requests— fetching data from the internet.pandas— working with tables and spreadsheets.matplotlib— making charts.openpyxl— reading and writing Excel files.
On your computer you install them with pip, which comes with Python. From the terminal:
pip install pandas
Then you use it with import pandas, same as the
built-in ones.
import y from … import …. Los módulos random,
datetime y math. Y qué son pip y las librerías externas.
Ahora, algo que todo programa necesita: no morirse cuando algo sale mal.
import and from … import …. The random,
datetime and math modules. And what pip and external libraries are.
Now, something every program needs: not dying when something goes wrong.