Python for Humans · Draft v0.2 · Nov 1, 2026 · Draft position: chapter 17 · Add a recommended editor setup and clarify the saved-code handoff.

Keeping Your Work

Local Python & Saved Programs

You should already know
The finished word-prediction generator from chapter 9; copying code from an edited code box into a plain-text file.
You'll build
Your existing generator, running from a saved file on your own computer.
You'll be able to
Find, edit, save and rerun a Python program after closing and reopening your tools.
You need
A desktop or laptop where you can install Python; a plain-text editor of your choice; your saved generator.

Your generator runs in the browser. You've changed its source, tried different starting words, and copied the code into a plain-text file.

Now let's run that copy on your computer.

The code is familiar. What changes is where we keep it and how we start it. By the end of this chapter, you'll be able to close everything, come back later, and run your changed program again.

Your browser already saves edits in its code boxes. A saved file gives you a separate copy, and running Python locally lets you use that copy independently of this website.

Three Tools, Three Jobs

We'll use three things:

Tool Its job
A text editor Open, change and save your Python source code.
A terminal Accept commands, including the command that starts Python.
Python Read your source code and carry out its instructions.

Some applications combine these tools in one window. They're still doing different jobs.

Use any editor that opens and saves plain text. You don't need to change editors if you already have one. It should preserve indentation and save your file with a .py extension. Use UTF-8 if it asks you to choose an encoding.

A word processor's formatted document isn't a Python source file. If an application offers document styles, pages and fonts, make sure you can switch it to plain-text mode before using it for code.

We'll run the program from a terminal, so the instructions don't depend on your editor having a particular Run button.

If You Don't Have an Editor Yet

Use Visual Studio Code, a free plain-text code editor. Download it from code.visualstudio.com for your operating system:

In the editor, choose File → New Text File. Type print('My editor saved this.'), choose File → Save As, and save it as editor_check.py somewhere you can find again. Close the file's tab, choose File → Open File, and reopen that file. Check that your line is still there.

That is the editor workflow we'll use: open, change, save, and reopen. Once Python is installed, we'll run files from the separate terminal described below. You can use this editor for the rest of the course.

Get Python Running

Follow the instructions for your operating system, then join us at Try a Familiar Expression.

Windows

Visit Python's download page and install the Python install manager for Windows. It is also available through the Microsoft Store link on that page.

Open PowerShell from the Start menu. If it was already open during installation, close it and open a new window.

Type this command and press Enter:

python --version

With a new installation, the manager may first download Python. Let it finish. You should then see a version beginning with Python 3..

If Python is already installed and this command prints a Python 3 version, you can keep using it for this chapter. If your existing installation works with py --version instead, use py wherever the Windows instructions below say python.

If the command isn't recognized or opens the Store instead of running Python, check the official Windows troubleshooting instructions. Resolve that before moving on: the next steps need a working Python command.

macOS

Visit Python's download page and download the current stable Python 3 installer for macOS. Open the installer and follow its steps. The official macOS installation guide also explains the supplied Install Certificates.command; run it to finish the setup.

Open Terminal using Spotlight, then type:

python3 --version

You should see a version beginning with Python 3.. If Terminal was open before installation and cannot find the command, close it and open a new window.

We'll use python3 for the macOS commands in this chapter.

Linux

Open your terminal and check whether Python is already installed:

python3 --version

Many Linux distributions already provide Python 3. If yours doesn't, install the python3 package using your distribution's software manager. Then repeat the version check.

We'll use python3 for the Linux commands in this chapter, too.

Try a Familiar Expression

At the terminal, type the command for your system:

Windows macOS / Linux
python python3

Python starts and displays some version information, followed by >>>. This is the same kind of Python prompt we used in the browser.

Type the expression after the prompt, without typing the >>> itself:

>>> 2 + 2
4
>>> starting_word = 'the'
>>> starting_word
'the'

Same language, different home.

Now leave the Python prompt by typing exit() and pressing Enter:

exit()

You're back at the terminal's command prompt. It may begin with PS on Windows or end with $ or % on macOS and Linux. Its exact appearance depends on your computer.

The prompt tells you who's listening. At >>>, Python expects Python code. At the terminal's command prompt, you can start a program with a command such as python my_generator.py.

We'll type that command at the terminal, after leaving >>>.

Put Your Program in One Folder

Use your computer's file manager to create a folder called python-for-humans somewhere you can find again, such as your Documents folder.

Put your saved generator into it. If you haven't saved a copy yet, return to the generator chapter, select the code in your finished generator box, and copy it into a new plain-text file in your course folder.

Use the filename my_generator.py for this chapter. On Windows, turn on View → Show → File name extensions in File Explorer so you can check the complete name. It should end in .py, with no extra .txt after it.

Next, save the course's Shakespeare text file into the same folder. Right-click the link and choose Save link as (or your browser's equivalent), keeping the filename shakespeare.txt. The saved file should contain the text itself.

Your folder should now contain:

python-for-humans/
    my_generator.py
    shakespeare.txt

The first file contains instructions. The second contains the data those instructions use. Downloading the program doesn't automatically download its data.

Open my_generator.py using your editor's Open command. Look for the line that sets text. If you downloaded the worked example before changing it to use Shakespeare, it will still contain the dragon's tiny sample sentence. Replace that assignment with:

text = open('shakespeare.txt', encoding='utf-8').read()

Keep one assignment to text in that place, before followers = next_words(text). Save the file.

The encoding='utf-8' part tells Python how to interpret the text file's characters. This course file uses UTF-8. We'll work more with encodings and closing files in the files chapter.

You can also open shakespeare.txt in the editor to check what you downloaded. Leave its contents unchanged.

Open a Terminal in That Folder

The terminal has a current folder, also called its working directory. For our first run, it needs to be the python-for-humans folder containing both files.

Windows

In File Explorer, open python-for-humans. Right-click an empty area inside the folder and choose Open in Terminal. If that option is unavailable, click the Explorer address bar, type powershell, and press Enter.

In the terminal, type:

dir

Look for both my_generator.py and shakespeare.txt in the listing.

macOS

In Finder, select the python-for-humans folder and press Option-Command-C to copy its full path.

Open Terminal. Type cd followed by a space and an opening double quote. Paste the copied path, type the closing double quote, then press Enter. For example, if your username were sam and the folder were in Documents, the complete command would be:

cd "/Users/sam/Documents/python-for-humans"

Use your copied path, rather than the example's path. cd means “change directory.” The quotes keep a path containing spaces together as one argument.

Now type:

ls

Look for both my_generator.py and shakespeare.txt.

Linux

Open python-for-humans in your file manager and use Open in Terminal if it offers that option. Otherwise, copy the folder's full path and use cd in your terminal, putting double quotes around the path. For example:

cd "/home/sam/Documents/python-for-humans"

Substitute your actual path. Then use ls to confirm that both files are there.

Run Your Generator

At the terminal's command prompt, run:

Windows macOS / Linux
python my_generator.py python3 my_generator.py

The command names the program to start, Python, followed by the source file it should read.

Your generator prints a passage, then the terminal's command prompt returns. That's a complete run. You don't need to type exit() after running a saved file this way.

Run it again. The passage will probably change because the program makes random choices. Check that it starts with your chosen word and stops at your word limit or a word with no recorded follower. An identical passage isn't required.

The starting_word name we made at the Python prompt isn't part of this run. The saved program creates its own values by carrying out its assignments.

If the First Run Fails

What you see What to check
SyntaxError after typing the terminal command If you're at >>>, type exit() first. Then run the command at the terminal's prompt. If the error points inside your source file, keep the named line and error message for the debugging chapter.
A message saying Python can't open my_generator.py Use dir or ls to check the current folder and exact filename. Check for a duplicate-download suffix or an extra .txt.
FileNotFoundError mentioning shakespeare.txt Check that the text file is in the current folder, alongside your program, with that exact name.
Only the dragon example's words Check the text assignment in the saved program. It should read shakespeare.txt.

The data filename in open('shakespeare.txt') is relative to the terminal's current folder. Opening a source file in an editor doesn't change that folder. We'll explore paths more fully in Files and the filesystem.

Make a Change You Can Recognize

Open my_generator.py in your editor. Add this line immediately before the final print(output):

# Try it: Change the message in your editor, but leave the file
# unsaved. Run the terminal command again. Which message appears?
# Now save and repeat the command. Explain the difference.
print('My generator is running on my computer.')

Save the file, return to the terminal, and run the same command again.

The new message should appear before the passage. It's a more reliable check of your edit than looking for a particular randomly generated sentence.

Now change the word limit in the while condition to 10. Save and run again. The passage should contain at most ten words, including the starting word. It may finish earlier.

If your editor saves automatically, turn that feature off for this experiment. Otherwise, it may save your edit before you run the command.

Python reads the file saved on disk when you run it. Text you've changed only in the editor hasn't reached that file yet.

Close Everything and Come Back

Save the source file, then close your editor and terminal.

Open your python-for-humans folder again. Open a terminal there, check that you can see both files, and run the generator.

Your custom message should still appear. The passage may be different. The saved instructions survived; the random choices are made again.

You can now run this program without opening the course website.

Your Turn

Predict

You change the word limit in your editor from 10 to 30, leave the edit unsaved, then run the saved program from the terminal. Which limit does Python use, and why?

Check your explanation

It uses 10, the limit in the file on disk. Saving the edit changes that file; moving back to the terminal doesn't save it for you.

Find the Wrong Prompt

Someone enters this at the Python prompt:

>>> python my_generator.py

Python reports a syntax error. Explain why it treated the command as Python code, then write the steps to run the saved program instead. Use the command appropriate to your operating system.

Run It Independently

Close your tools again. Without replaying the walkthrough:

  1. Find your saved generator and open it in your editor.
  2. Change its custom message to something new and save it.
  3. Open a terminal in the folder containing both files.
  4. Run the program and check that the new message appears.

Can you point to the source file you changed, the data file it reads, and the place you typed the command? If a step doesn't work, use the file listing and error message to decide what to check.

As an optional follow-up, run a game you downloaded earlier. A game that asks for input waits for your answer in the terminal. Type the requested answer there; terminal commands have to wait until the game finishes. Ctrl-C usually interrupts a running Python program if you need to stop it.

Keeping a game's source doesn't preserve its board or inventory between runs. Saving progress comes later.

What I've Learned

In your own words, explain how editing, saving and running differ. Name one thing that survives closing the program and one thing that doesn't.

You've moved a familiar program onto your own computer, changed it, and run it again after closing your tools. Next, we'll learn how to investigate a program when a change breaks it—or makes it give a plausible but wrong answer.