Skip to main content

Running Python yourself

Install Python, check its version and run it two ways: at the interactive prompt, a read, evaluate, print loop that shows the value of whatever you type, and as a script, which shows only what it prints. Then read a first error message from its last line upwards.
P01.1.3L0 Foundation25 minIn review

Before this lesson

P01.1.2 From source text to running program

After this lesson you can

  • Check which version of Python is installed, and start and leave its interactive prompt.
  • Describe the interactive prompt as a read, evaluate, print loop, and predict what it shows for each line you type.
  • Write a script in a file, run it with the command python, and explain why it shows only what it prints.
  • Read an error message from its last line upwards: the kind of error, the line with its marker, then the file and line number.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

Type 55 + 65 + 80 + 40 + 60 at Python’s prompt and it answers 300 at once. Save the same line in a file, run the file, and nothing appears at all. Same line, same Python: why does one way show the answer while the other stays silent, and which way should you use?

From the last lesson

1. A four-line file has a syntax error in line 4. How many of its lines run, and why?

Show answer

None. The syntax error stops the compile stage, so no bytecode is made and the run stage never starts, even for the three good lines above it.

2. A four-line file misspells a name in line 3. How many lines run?

Show answer

Two, lines 1 and 2. The file compiles, and the unknown name is found only when line 3 runs.

Intuition

You can give Python its instructions in two ways.

At the interactive prompt you type one line, and Python answers it straight away, then waits for the next. It is a conversation. Python works out the value of what you typed and shows it, which is why a sum is answered with 300. Read a line, evaluate it, print the value, loop back for more: this cycle is the read, evaluate, print loop, or REPL.

In a script you write the lines in a file first, then ask Python to run the whole file. It compiles the file and runs it from top to bottom (P01.1.2) without stopping to talk. It shows only what the program sends to the screen with print. A sum on a line of its own is worked out and then dropped, because nothing asked for it to be shown.

Two panels side by side. Left, titled At the prompt, one line at a time: a small screen shows the prompt, three greater-than signs, then 9 star 2 plus 33, the answer 51 in vermillion, and a fresh prompt. Below it, four boxes form a loop joined by green arrows: 1 Read, the line you type; 2 Evaluate, work out 51; 3 Print, show the value; 4 Loop, wait for a line; and back to Read. A caption says: the read, evaluate, print loop (REPL). Right, titled As a script, the whole file: the four numbered lines of hello.py, a comment, print of Hello world, the bare sum 55 plus 65 plus 80 plus 40 plus 60, and print of 9 star 2 plus 33. Under them the command python hello.py and an arrow down to an output screen that shows only Hello, world and 51. A note says that line 1 is a comment and is skipped, and line 3 is worked out, then dropped, so 300 never appears.
P01.1.3-F1

Two ways to run Python. The prompt answers each line you type with its value; a script runs the whole file and shows only what it prints.

Both ways use the same interpreter. The prompt is for trying things: one line, an answer, the next line. A script is for keeping: a program you can run again tomorrow, change and share. Machine-learning work uses both. You try an idea at a prompt, or in a notebook (P02.9), then keep it in a script that can be run again on new data.

Build it up

Step 1. Install Python and check its version. Python is free from python.org. On Windows, the Python install manager, from python.org or the Microsoft Store, sets it up. On macOS, use the installer from python.org; most Linux systems have Python already. Then open a terminal, the window in which you type commands to the computer (Terminal on macOS and Linux, Terminal or PowerShell on Windows), and type:

python --version

It answers with the version number, for example:

Python 3.15.2

Your numbers may differ; this course needs Python 3.12 or later. On some systems the command has another name: python3 on most Macs and Linux systems, or py on Windows. If python is not found, try those, and use that name wherever this course writes python.

Step 2. Start the prompt. Type python on its own. Python prints two lines of greeting, the first giving its version, then its prompt, >>>. The prompt means Python is waiting for a line of Python. Type a line and press Enter:

>>> 55 + 65 + 80 + 40 + 60
300
>>> 9 * 2 + 33
51
>>> print("Study table")
Study table
>>> "Study table"
'Study table'

Each line is read, evaluated and its value printed. The last two lines show a detail: the prompt shows a piece of text with its quotes, so that you can see it is text, while print shows the text itself. To leave, type exit(), or press Ctrl-D on macOS and Linux, Ctrl-Z then Enter on Windows. The terminal’s own prompt comes back.

Step 3. Write a script. Open a plain-text editor: IDLE, which comes with Python from python.org, a code editor such as VS Code, or any editor that saves plain text, but not a word processor. Type these lines and save them in a file called hello.py:

# hello.py: a first script
print("Hello, world")
55 + 65 + 80 + 40 + 60
print(9 * 2 + 33)

Step 4. Run it. In the terminal, in the folder that holds the file, type:

python hello.py

The output is:

Hello, world
51

Lines 2 and 4 print. Line 3 is worked out to 300 and dropped, because it does not print.

Step 5. Comments. Line 1 is a comment. It starts with the hash sign, #, and runs to the end of the line, and Python skips it completely. A comment is a note for people who read the code, including you next month. It can also follow code on the same line, here to say what the 2 in the sum stands for:

print(9 * 2 + 33)  # 2 hours

The formal statement

NoteDefinitions
  • The terminal is a window in which you type commands for the computer. Its own prompt, often ending in $, % or >, waits for a command such as python hello.py.
  • The interactive prompt is Python’s prompt, >>>. There Python reads and runs one statement at a time. It is also called the REPL, for read, evaluate, print loop.
  • A script is a file of Python code, by custom named with the ending .py, run as a whole with the command python followed by the file’s name.
  • An expression is a piece of code that has a value: 9 * 2 + 33 has the value 51, and "Study table" has as its value that text.
  • A comment starts with # and runs to the end of its line. Python ignores it.
NoteWhat each way shows
  • At the prompt, Python reads one line, runs it and, if the line is an expression, shows its value. print shows its text; its own result is a special value meaning nothing (None, P01.2.1), which the prompt does not show.
  • In a script, Python compiles the whole file and runs it from the top. Only what the program sends out with print appears. The value of an expression on a line of its own is worked out and dropped.

When a line fails, Python prints an error message. The message from a running program starts with the line Traceback (most recent call last):, and the part that matters most is at the bottom. Read it from the bottom up:

  1. The last line names the kind of error and says what went wrong.
  2. Above it, Python repeats the line that failed, with a marker, ^^^^^, under the part it could not carry out.
  3. Above that, the file and the line number where that line is.
A real error message in a panel: Traceback (most recent call last), then File hello.py, line 4, in module, then the line Print(9 * 2 + 33) with five carets under Print, and last NameError: name 'Print' is not defined. Did you mean: 'print'? Yellow numbered badges mark the parts in reading order: 1 under the last line, the kind of error and what went wrong, read this first; 2 beside the code and marker, the line itself, with a marker under the part; 3 beside the File line, which file, which line. A note beside the first line says: read from the bottom up.
P01.1.3-F3

The three parts of an error message, numbered in the order to read them: from the bottom up.

A message from a syntax error has no Traceback line, because nothing was running yet (P01.1.2), but it is read the same way: the kind of error at the bottom, then the line with its marker, then the file and line number.

Worked example

The same four lines, two ways. Run hello.py as a script, then type its four lines at the prompt one by one, and compare what appears.

As a script, the output is Hello, world and 51, as in step 4. At the prompt, line 1 is a comment, so nothing appears. Line 2 prints Hello, world. Line 3 is an expression, so the prompt shows its value, 300, and line 4 prints 51.

A table with four rows, one per line of hello.py, and the columns Line of hello.py, At the prompt, and As a script. Line 1, the comment, shows nothing in both columns. Line 2, print of Hello world, shows Hello, world in both. Line 3, the bare sum, shows 300 at the prompt and nothing as a script, and its row is shaded sky blue. Line 4, print of 9 star 2 plus 33, shows 51 in both. A sentence underneath says that only line 3 differs: the prompt shows the value of whatever you type, while a script shows only what print sends to the screen.
P01.1.3-F2

The four lines of hello.py run two ways. Only line 3, the bare sum, behaves differently.

A first error message. Now change line 4 of hello.py to Print(9 * 2 + 33), with a capital P, save the file and run it again. Python 3.15 prints this (the wording varies a little between versions, and on your computer the file name includes its folder):

Hello, world
Traceback (most recent call last):
  File "hello.py", line 4, in <module>
    Print(9 * 2 + 33)
    ^^^^^
NameError: name 'Print' is not defined. Did you mean: 'print'?

Read it from the bottom up.

The last line. A NameError: Python knows no name Print, and it even suggests print.

The line and its marker. The marker sits under Print, the part Python could not carry out.

The file and line. The line is line 4 of hello.py.

The fix is a lower-case p. Notice, too, that Hello, world was printed before the message. That is the rule of P01.1.2: lines 1 to 3 ran before line 4 failed. The top line of the message, the same in every such message, makes sense after the call stack of P01.4.3.

Code

The cells on this page work like a script, with one extra: like the prompt, a cell also shows the value of its last line, if that line is an expression. This is a convenience of notebooks, the tool these pages are built with (P02.9 returns to them).

# A comment: Python skips everything from the hash to the end of the line.
print("Hello, world")
55 + 65 + 80 + 40 + 60
Hello, world
300

The cell printed Hello, world, then showed 300, the value of its last line. Only the last line gets this treatment, so the value of the first line below is dropped, as in a script:

9 * 2 + 33
55 + 65 + 80 + 40 + 60
300

Explore

This module’s interactive, the line-by-line stepper, lives in lesson P01.1.2. It runs a file the way a script runs: plant a misspelt name and watch the output stop at that line, with the error message below it.

Pitfalls

Wrong beside right. Left, marked with a cross and titled At Python's prompt: a screen where the command python hello.py was typed after the three greater-than signs, and Python answers with File python-input-0, line 1, the line again with carets under hello, and SyntaxError: invalid syntax, with the note that Python reads the command as Python and cannot make sense of it. Right, marked with a tick and titled At the terminal's prompt: a screen where python hello.py follows a dollar sign, and the output Hello, world and 51 appears, with the note that the terminal starts Python, which runs the file, so leave the prompt first with exit().
P01.1.3-F4

Wrong beside right. python hello.py is a command for the terminal, not a line of Python.

“I typed python hello.py and got a SyntaxError.”

You typed it at Python’s prompt, >>>, which expects Python, not a command. Leave the prompt with exit(), so that the terminal’s own prompt comes back, and type the command there.

“My script ran, but it printed nothing.”

A script shows only what print sends out. A line that only works something out, such as 55 + 65 + 80 + 40 + 60, is computed and dropped. Wrap it in print(...) to see it.

“The top of the error message tells me what went wrong.”

The top line is the same in every message and says nothing about this one. Start at the last line, which names the error, then work upwards. The marker shows where Python noticed the problem, which the Python tutorial warns “is not always the place that needs to be fixed”.

“I wrote my script in a word processor.”

A word processor turns straight quotes into curly ones, which Python cannot read: Python 3.15 stops with SyntaxError: invalid character '“' (U+201C). Use a plain-text or code editor.

Check yourself

Recall

1. What command shows which version of Python you have?

Show answer

python --version, typed in the terminal (or python3 or py on some systems).

2. What do the letters REPL stand for, and what happens at each step?

Show answer

Read, evaluate, print, loop. Python reads the line you type, works out its value, shows the value, and goes back to wait for the next line.

3. The line 9 * 2 + 33 is typed at the prompt, and the same line is put on its own in a script. What appears in each case?

Show answer

At the prompt, 51. In the script, nothing: the value is worked out and dropped, because only print sends things to the screen.

4. What does Python do with a comment?

Show answer

It skips it. A comment runs from # to the end of the line and is there only for human readers.

5. In which order should you read an error message, and what does each part tell you?

Show answer

From the bottom up. The last line gives the kind of error and what went wrong; above it are the line that failed and a marker under the part at fault; above that, the file and line number.

Exercises

1. ★ What appears when each line is typed at the prompt? Which of them would show anything if it stood alone in a script? (a) 2 + 4 + 5 + 1 + 3 (b) print(2 + 4 + 5 + 1 + 3) (c) "Hours" (d) # 15

Show solution

(a) 15, the value of the sum. (b) 15, printed by print. (c) ‘Hours’, with quotes, because the prompt shows text as text. (d) Nothing: a comment is skipped. In a script only (b) shows anything, because only (b) prints.

2. ★★ What does this script print? What appears if its five lines are typed at the prompt instead, one by one?

# scores.py
55 + 65
print(80 + 40)
"Done"
print("End")
Show solution

As a script it prints only the two print lines: 120, then End. At the prompt every line with a value shows it: nothing for the comment, then 120 for 55 + 65, 120 again from print(80 + 40), ‘Done’ with its quotes, and End.

3. ★★★ Two scripts stop with these messages. For each, give the kind of error, the line at fault and how many lines of the script ran, and say how the message itself shows that count.

Hours
15
69
Traceback (most recent call last):
  File "hours.py", line 4, in <module>
    Print("End")
    ^^^^^
NameError: name 'Print' is not defined. Did you mean: 'print'?
  File "hours.py", line 2
    print(2 + 4 + 5 + 1 + 3
         ^
SyntaxError: '(' was never closed
Show solution

The first is a NameError in line 4: Print has a capital P. Three lines ran, which shows twice: three lines of output sit above the message, and a NameError is found only when its line runs, so lines 1 to 3 had run.

The second is a syntax error in line 2: a bracket is never closed. No line ran. There is no output above the message, and no Traceback line, because the file failed at the compile stage, before anything ran.

Summary card

At the prompt Python answers each line with its value; a script shows only what it prints.

The anchor figure again: on the left, the prompt answering 9 times 2 plus 33 with 51 and the read, evaluate, print loop drawn as four boxes; on the right, hello.py run as a script, which shows only Hello, world and 51.
P01.1.3-F1

The prompt and the script.

  • Version. python --version in the terminal (python3 or py on some systems).
  • Prompt. python alone starts the REPL at >>>; exit() leaves it.
  • Script. Save the code in a .py file and run python hello.py in the terminal.
  • Comments. From # to the end of the line, skipped by Python.
  • Error messages. Read from the bottom: kind of error, then the line and marker, then file and line.

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Python Software Foundation, The Python Tutorial, chapter 2. Section 2.1 names the commands (python3.15 on Unix, py on Windows) and leaving with Control-D, Control-Z or quit(); section 2.1.2 describes the primary prompt >>>.
  • Python Software Foundation, The Python Tutorial, chapter 3: its opening, where comments start with #, and section 3.1, where the interpreter “acts as a simple calculator” that writes the value of an expression and print omits the enclosing quotes.
  • Python Software Foundation, Python Setup and Usage, section 1.1: the --version option, running a script with python myscript.py, and interactive mode; section 4.1 on the Windows install manager and its python and py commands.
  • Python Software Foundation, Glossary: REPL, “the read–eval–print loop”, and interactive; and Tutorial chapter 8, where “the last line of the error message indicates what happened”.
  • Allen B. Downey, Think Python, 3rd edition: section 2.6, where only the value of the last expression is displayed, and section 2.8 on comments.

Next. P01.1.4 Bits, bytes and memory opens up the memory that holds the program and its values, and finds only 0s and 1s.

This lesson unlocks

P01.1.4 Bits, bytes and memory

Your progress is saved in this browser only. No account, no tracking.
Back to top