Topic 05

The Interactive Prompt

Hands-on

Before you write a program into a file, there is a faster way to try an idea: ask Python directly. Type python on Windows or python3 on a Mac, with nothing after it, and Python starts a conversation. It shows a prompt of three greater-than signs and waits. You type one line, press Enter, and the answer appears straight away.

This prompt is like a calculator with a short memory. It is perfect for checking something in ten seconds, and useless for keeping anything, because everything you typed is gone the moment you leave. Real programs live in files, which is the next topic. This one is for trying things.

The Three Arrows

When Python starts this way it prints its version on the first line, a line suggesting a few commands, and then the prompt: three greater-than signs and a space. That prompt means Python is waiting for one instruction. Whatever you type after it is read when you press Enter, worked out, and the result is printed on the next line. Then the prompt appears again.

That cycle has a name you will see in documentation: the REPL, short for read, evaluate, print, loop. Read your line, work it out, print the answer, wait for the next one. The three arrows are not something you type. They are Python's side of the conversation.

What the REPL's name means
Readyour line
→
Evaluatework it out
→
Printshow the answer
→
Loopwait again

Python as a Calculator

Try some arithmetic. Type two plus two and press Enter; Python answers four. Seven times six gives forty-two; the star is Python's multiplication sign. Ten divided by four gives two point five; a forward slash means divide. Notice that you never asked Python to show the answers. At the prompt, the result of each line is printed automatically. That convenience exists only here; in a program file, as you will see in the next topic, you have to ask for output explicitly.

A first session (macOS shown; on Windows type python)
you@mac ~ % python3
Python 3.15.0 (main, ...) [Clang ...] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 2
4
>>> 7 * 6
42
>>> 10 / 4
2.5

Your First Error, on Purpose

Now type the word hello, with no quotes, and press Enter. Python prints several lines of what looks like trouble. Read the last line first. It says NameError: name hello is not defined. Did you mean help? In plain words: Python treated hello as a name for something, looked for a thing called hello, and found nothing. It even suggests a name that does exist and looks similar.

Typing a word Python does not know
>>> hello
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    hello
NameError: name 'hello' is not defined. Did you mean: 'help'?
>>>

Two things are worth noticing. First, nothing broke. The prompt came straight back, ready for the next line. Second, the message was precise: it named the kind of problem and the exact word that caused it. Chapter 2 shows how to make Python treat hello as a piece of text rather than a name, and Chapter 3 teaches you to read these messages in full. For now it is enough that an error is a message, not a disaster.

Leaving

To end the conversation, type exit() and press Enter. There is also a keyboard shortcut. On a Mac it is Control and D. On Windows it is Control and Z, followed by Enter. Either way you are back at the terminal's own prompt, and everything you typed in Python is gone. That is exactly why the next topic moves your instructions into a file.

Common Confusions
  • "The three arrows are part of what I type." They are Python's prompt. You type only what comes after them.
  • "The prompt saves my work." Nothing is kept after you leave. Anything you want to keep belongs in a file.
  • "An error means I broke Python." An error is a message about one line. The prompt carries on at once, ready for the next.
  • "Results always print by themselves." Only at the prompt. In a program file, Python shows nothing unless you ask it to print.
Why It Matters
  • A ten-second experiment at the prompt answers most "what does this do?" questions faster than any search, and professionals use it constantly for exactly that.
  • Meeting an error in a safe place makes the next hundred errors far less alarming.

Knowledge Check

What do the three greater-than signs at the start of a line mean?

  • They must be typed before every line of Python you write
  • Python is waiting for you to type one instruction
  • The line before contained an error that Python has marked
  • Python is saving everything typed so far to a file

What happens to the lines you typed at the prompt after you leave it?

  • They are saved to a file in your home folder automatically
  • They are loaded again the next time you start Python
  • They are gone; the prompt keeps nothing after you exit
  • They are kept online so you can open them from another computer

You type hello at the prompt and see NameError: name 'hello' is not defined. What does it mean?

  • Python looked for something named hello and found nothing
  • Python has stopped working and must be installed again
  • Python does not accept greetings, so the word is blocked
  • The word must be typed in capital letters to be understood

Which is a way to leave the interactive prompt?

  • Press Enter twice on an empty line to end the session
  • Type cd and two dots to move back out of Python
  • Type stop, which Python treats as a special command
  • Type exit() with the brackets, then press Enter

You got correct