0%

Python Outside the Browser

A Running Python Has Memory

By the end of this course you will have a project on your own computer, with its own environment, its own tests, and instructions good enough that somebody else can run it. Along the way you will meet the , package management, real files, and a service that sometimes fails on purpose.

We start somewhere small, though, because one idea underneath all of that is worth getting straight first: what is actually running your code, and what happens to everything it remembers when it stops.

Two words worth having

Python code is a set of instructions. The Python interpreter is the program that reads those instructions and carries them out. It is a real program, sitting on a real machine, the same way a text editor or a browser is.

While it runs, the computer gives that work some memory to keep track of things. One running instance of a program, along with its temporary memory, is a process.

You do not need operating-system details here. The distinction that matters is smaller and more practical: instructions you saved can stick around, while the memory one run used disappears when that run ends.

Watch a name disappear

The panel beside this lesson is a REPL, a Python interpreter waiting for one line at a time. The >>> prompt means it is ready. It is not a shell, and it is not Python installed on your computer; local setup starts in Chapter 3.

Type this and press Enter:

message = "ready"

Now type just the name and press Enter:

message

The interpreter shows 'ready'. It remembered.

Now press Restart on the REPL, and ask for message one more time.

This time Python tells you the name is not defined. Nothing is broken. That restart ended one process and started a fresh one, and the new one never knew about your name. The memory went with the run.

Try it a couple more times with different values if it helps. It is a small thing to watch, and it explains a surprising amount later: why a program cannot remember anything between runs unless you deliberately make it, and why “it worked a minute ago” sometimes means “it worked in a process that no longer exists.”

Meet Monty

Monty is the AI tutor built into Python Land, and this is a good moment to introduce him, because the way you ask matters more than most people expect.

A vague question gets a vague answer. Try this one, which is specific enough to be useful:

Ask Monty: “Why does message disappear when I restart the REPL, but the code I type in the editor does not?”

That question names what you did, what you expected, and what surprised you. Those three things are what let anyone, human or otherwise, actually help you.

Monty is free throughout this chapter, so use it freely here to get a feel for what good questions look like. From Chapter 2 onward, full Monty support is part of the Guided subscription. The course is completely finishable without it, and everything you need is in the lessons themselves. What Guided buys you is a second explanation when the first one did not land, and a hint when you are stuck at eleven at night with nobody to ask.

You define message = "ready" in the REPL, restart it, and ask for message again. Why is it undefined?

Which pair of statements is correct?

The next lesson keeps the instructions instead of depending on one session’s memory.