Getting Started with Programming

Running Python in the Sandbox


Learning Objectives

  • You can run a Python program in the course sandbox and read its output.
  • You can explain the difference between writing code and running code.
  • You understand that experimentation is a normal part of programming.
  • You can use small experiments to test your understanding of an unfamiliar line.

Before this part is over, you will have written multiple interactive Python programs. To do that, you need somewhere to run code. For this part of the course, that somewhere is the course sandbox: a Python environment that runs inside your browser.

You can open the sandbox by clicking the “Python” icon in the bottom right corner of this page. It will open an editor to the side of this page, where you can write and run Python code. The editor works locally in your browser.

Later in the course, you will install Python on your own computer. For now, the sandbox is enough, and it is a lot less hassle. Nothing to install, nothing to configure, no version mismatches.

What the Sandbox Lets You Do

The sandbox shows you two things at once: a code area where you write Python, and an output area where the result of running that code appears. You write a program, press the run button, and Python executes it from top to bottom. Anything the program prints appears in the output area. Anything the program asks you (with input(...)) waits for you to type a reply, just as it would in a real terminal.

You can think of the cycle as a loop:

  1. Write or edit code.
  2. Run it.
  3. Read the output.
  4. Decide what to change next.

That loop is the heart of how programmers actually work. Most code is not written all at once and then run for the first time at the end. It grows by running and adjusting.

The sandbox cycle

0 / 5 points

Which sequence best describes the basic cycle of working in the Python sandbox?

Code and Output Are Different Things

A common early confusion is the difference between what the code says and what the program does. They are related, but they are not the same thing.

Take this program:

print("Hello")
print("World")

The code is two lines. The output, when you run it, is also two lines, but they are different lines from a different place. The code is what you wrote. The output is what Python produced by running the code.

When something does not behave the way you expected, the first useful question is almost always: is the surprise in the code, or in the output? Sometimes the code is wrong. Sometimes the code is fine and your expectation was wrong. Telling those apart is most of debugging.

Small Experiments Reduce Confusion

Suppose you are reading a Python example and you are not sure what one line does. For instance:

"Hello" + "!"

Instead of guessing, you can run it. Open the sandbox, type it into the editor sandbox, press run, and see what happens. This is the same kind of experiment you might do with a small physical object: poke it, see how it responds, build a better mental model.

This habit is especially important when you encounter examples from the web or code suggested by an AI assistant. You do not need to understand a strange line by staring at it. You can reduce it to a small experiment and see what it produces.

Run an unfamiliar program

0 / 5 points

Copy this program into the sandbox and run it. As a reminder, the sandbox is a Python environment that runs inside your browser. You can open it by clicking the “Python” icon in the bottom right corner of this page.

You are not expected to understand how the program works yet — it also follows many bad habits, so you might not even want to look at it. Your task is only to run it and report what the last output line says.

def z9(q2):
if q2 <= 1:
return q2
return z9(q2 - 1) + z9(q2 - 2)
for x4 in range(7):
print(z9(x4))

What is the last line of output?


A Note on the Read-Evaluate-Print Loop

If you have heard the term REPL (read-evaluate-print loop), this is what it refers to: an environment where you can type one expression at a time and see the result immediately. The course sandbox is not strictly a REPL — it runs whole programs rather than expressions one at a time — but the same idea applies. You can write a very short program, run it, observe the result, and adjust.

When you install Python locally in Part 4, you will see a real REPL too. For now, treat the sandbox as a comfortable place to try small things.

Errors in the Sandbox

Sometimes you will run a program and instead of the output you expected, you will see an error message. Errors are not failures of the sandbox or of you. They are Python telling you what it could not do and where it stopped.

For example, running this program:

print("Hello"

produces an error rather than output, because the closing parenthesis is missing. What you would see is similar to the following:

File "<python-repl>", line 1
print("Hello"
^
SyntaxError: '(' was never closed

When seeking to understand an error, read the message carefully. Often, the most important parts to focus on are the last few lines. In the example above, the error message is a SyntaxError, and the problem is that a parenthesis was never closed.

Python programs may fail with syntax errors, but they can also fail with other kinds of errors. For instance, if you run this program:

print("Hello")
print(1 / 0)

the output message would be something like:

Hello
File "<python-repl>", line 2, in <module>
ZeroDivisionError: division by zero

The first line correctly prints “Hello”. The second line causes an error because you cannot divide a number by zero. The error message is a ZeroDivisionError, and the description is “division by zero”. The message also says that the error happened on line 2, which is where the division by zero happens.

We’ll look into arithmetics and numbers in more detail in Numbers and Arithmetic. For now, the important thing is to see how the error message points to the line that caused the problem and describes what went wrong.

Read the last error line

0 / 5 points

Copy this program into the sandbox and run it.

print("Checking the sandbox")
print(mystery_value)

The last line of the program causes an error. What error does the final line of the error message report?

Check Your Understanding

  1. What is the difference between the code you write and the output Python produces?
  2. What does it mean to “experiment” in the sandbox, and when is that useful?
  3. If a program produces an error message, what is a reasonable first thing to do?