Skip to content

    Chapter 1, Environment

    What is Python and how does it execute

    Before installing anything, I want you to have the right execution model in mind, because it explains almost every beginner's mistake.

    Code for this chapter: ambiente/cap01_como_executa.py

    Python in a few words

    Python is a high-level, general-purpose language created by Guido van Rossum and released in 1991. The name comes from the comedy group Monty Python, not the snake. It supports procedural, object-oriented, and functional programming, and the syntax uses indentation to delimit blocks, which forces all code to look the same.

    ambiente/cap01_como_executa.pylinha 10
    print("Olá, Python")
    
    Output
    Olá, Python
    

    One line is enough for a complete program. In Java, the equivalent requires a class, a method main and a long call. This economy is the first reason why Python is a good first language.

    Compiled or interpreted: both

    The popular explanation says that Python is "interpreted, line by line". It is useful to start and wrong in a detail that matters. What CPython (the reference implementation) does is:

    1. Reads the entire file and compiles it to bytecode, an intermediate representation.
    2. Executes that bytecode on the Python virtual machine.

    That's why a syntax error on the last line prevents the first line from running. Python doesn't even get to execute anything.

    ambiente/cap01_como_executa.pylinhas 15 a 20
    codigo = "print('primeira linha')\nprint('segunda linha'"
    
    try:
        compile(codigo, "<exemplo>", "exec")
    except SyntaxError as erro:
        print("Nada foi executado. Erro de sintaxe:", erro.msg)
    
    Output
    Nada foi executado. Erro de sintaxe: '(' was never closed
    

    Logic, type, and value errors only appear when the problematic line is executed:

    ambiente/cap01_como_executa.pylinhas 22 a 29
    def dividir(a, b):
        return a / b
    
    print("esta linha roda normalmente")
    try:
        dividir(1, 0)
    except ZeroDivisionError as erro:
        print("erro só apareceu na execução:", erro)
    
    Output
    esta linha roda normalmente
    erro só apareceu na execução: division by zero
    

    If you want to see the bytecode, the module dis shows it. The output changes from version to version of Python, so I won't fix the output here. Run it on your computer:

    ambiente/cap01_como_executa.pylinhas 31 a 38
    import dis
    
    
    def soma(a, b):
        return a + b
    
    
    dis.dis(soma)
    

    Where each error appears

    PhaseWhat happensTypical errors
    CompilationThe entire file is read and translated to bytecodeSyntaxError, IndentationError
    ExecutionThe bytecode runs, one instruction at a timeNameError, TypeError, ValueError, ZeroDivisionError

    Keep this table. In the exceptions chapter, I show that both groups belong to the same family of classes, but the difference in when the error appears still holds.

    Bytecode and the __pycache__ folder

    When you import a module, Python writes the bytecode to files .pyc inside __pycache__, so it doesn't recompile the next time. You can delete the folder whenever you want: it is recreated. And it never goes to Git.

    Where Python fits into your work

    Python appears in automation and scripts, in data analysis, in APIs and web systems (Django, Flask, and FastAPI), in testing, and in almost the entire orchestration layer of artificial intelligence projects. A fair observation about this last case: AI libraries do the heavy lifting in compiled code (C, C++, CUDA), and Python is the glue that connects them. This is the healthiest way to think about the language: it is excellent for expressing intent and good enough in performance when the bottleneck is in another layer.