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.
print("Olá, Python")
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:
- Reads the entire file and compiles it to bytecode, an intermediate representation.
- 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.
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)
Nada foi executado. Erro de sintaxe: '(' was never closed
Logic, type, and value errors only appear when the problematic line is executed:
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)
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:
import dis
def soma(a, b):
return a + b
dis.dis(soma)
Where each error appears
| Phase | What happens | Typical errors |
|---|---|---|
| Compilation | The entire file is read and translated to bytecode | SyntaxError, IndentationError |
| Execution | The bytecode runs, one instruction at a time | NameError, 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
.pycinside__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.