Skip to content

    Chapter 1, Environment

    Why Python became the language of data

    Before writing a line of NumPy, it's worth understanding why it exists and why it lives in Python. The answer is less about the language and more about the people who built the libraries around it.

    Code for this chapter: ambiente/cap01_ecossistema_python.py

    Python is small, and the ecosystem is large

    Python itself doesn't know how to train a neural network, draw a graph, or read a spreadsheet. None of this is built-in. What Python has is an easy-to-read syntax and a huge ease of calling code written in other languages. This is what attracted those working with numerical computation: it was possible to write the heavy core in C or Fortran and use it through a simple interface in Python.

    NumPy was born in the mid-2000s from the unification of two previous projects (Numeric and Numarray). The rest of the data stack was built on top of it:

    LibraryWhat it offersWhy it depends on arrays
    NumPyThe array ndarray and the math on itIt's the foundation
    pandasTables with labels (DataFrame)Each column is, underneath, an array
    MatplotlibGraphsReceives arrays and draws
    scikit-learnClassic machine learningData and models are arrays
    PyTorch, JAX, TensorFlowDeep learningThe "tensor" is an array with GPU and derivatives

    None of these libraries are part of Python. Each one was written by people, published for free, and maintained by a community. You can check this in the interpreter itself:

    ambiente/cap01_ecossistema_python.pylinhas 10 a 17
    import importlib.util
    import sys
    
    import numpy as np
    
    print("numpy faz parte da biblioteca padrão?", "numpy" in sys.stdlib_module_names)
    print("numpy está instalado aqui?", importlib.util.find_spec("numpy") is not None)
    print(type(np.add).__name__, type(np.ndarray).__name__)
    
    Output
    numpy faz parte da biblioteca padrão? False
    numpy está instalado aqui? True
    ufunc type
    

    The np.add is an object of type ufunc, a compiled function that operates on integer arrays. The heavy lifting happens in this compiled code, and Python just orchestrates. This is the healthy way to think about the whole stack: Python is the glue, and the speed comes from the lower layers.

    How much Python do I need to know beforehand?

    Less than you think. Variables, lists, loops, functions, and import are enough to get started, and the rest you learn by using. No one memorizes an entire library. Those who work with data consult the documentation all the time, and what matters is knowing what is possible, to know what to look for.

    Exercise 1

    Which of these modules come with Python?

    Given the list ["math", "numpy", "json", "pandas"], return only those that are part of the standard library.