You can run code directly in Gemini, and it’s not just for spitting out snippets; it’s about interacting with live, evolving computation.
Here’s a Python script running in a Gemini notebook environment:
import time
print("Starting a countdown...")
for i in range(5, 0, -1):
print(f"{i}...")
time.sleep(1)
print("Blast off!")
Executing this cell will show the output Starting a countdown..., followed by 5..., 4..., and so on, until Blast off!. This isn’t just a static display of output; the notebook is actively running a Python interpreter, managing state, and executing instructions sequentially.
The core problem this solves is bridging the gap between understanding code and experiencing it. Traditionally, you’d copy code into a local IDE or a separate online interpreter, run it, and then bring the results back to your AI chat. The Gemini Code Execution tool integrates this entire loop. Internally, it spins up a secure, isolated execution environment for each notebook session. When you input code, it’s sent to this environment, executed, and the standard output, standard error, and any exceptions are streamed back to your Gemini interface. This allows for interactive development, debugging, and exploration of code within the same context as your AI conversation.
You control this through the notebook interface itself. You can write Python code, import standard libraries (like math, datetime, requests), and even run shell commands. The environment is persistent within a single notebook session, meaning variables you define in one cell are available in subsequent cells. You can also install additional Python packages using pip if needed, though the pre-installed set is quite comprehensive. For instance, if you wanted to use a library not included by default, you could run !pip install pandas in a code cell.
The most surprising thing is how seamlessly it handles asynchronous operations and I/O. You might expect a web-based interpreter to struggle with anything beyond simple scripts, but it can manage network requests, file operations (within its isolated filesystem), and even time-based delays like the time.sleep(1) in the example. This is thanks to robust backend infrastructure that manages the execution environments and streams results back efficiently, making the interaction feel almost as responsive as a local setup.
The next concept you’ll encounter is managing dependencies and environment reproducibility across different notebook sessions.