Concepts / Writing and Running Python Scripts

Writing and Running Python Scripts

The operating system terminal is where you launch Python. The >>> prompt is the signal that Python is now ready to receive Python statements.

  • Programming

From Terminal to Python

Python can be used through an interactive interpreter: a live environment where you enter a statement and see Python respond immediately. The first important skill is recognizing the transition from your operating system terminal into the Python interpreter. The two environments use different prompts and understand different kinds of instructions.

Key point

The operating system terminal is where you launch Python. The >>> prompt is the signal that Python is now ready to receive Python statements.

Recognizing the Two Prompts

When you open a terminal, you see a prompt belonging to the operating system. The source describes examples such as $ or C:\>. That prompt understands operating-system commands such as cd, ls, or dir. It does not understand Python code as Python code. To enter the Python environment, type python at the operating system prompt and press Enter. The prompt then changes to >>>, which belongs to Python.

$ or C:\>Operating system>>>Python interpretercd, ls, dirOperating-system commandsPython statementsPython codeacceptsaccepts
How can you tell whether you are entering an operating-system command or a Python statement?

Opening the Interpreter

  1. 1
    Open the operating system terminal so its command prompt is visible.
  2. 2
    Type python at the operating system prompt.
  3. 3
    Press Enter.
  4. 4
    Look for the >>> prompt.
  5. 5
    When >>> appears, enter Python statements at that prompt.
OS terminal$ or C:\>pythonPress EnterPython interpreter>>>typelaunches
What changes in the command-line environment when you run the Python command from the operating-system terminal?
Worked example

Launching Python

How do you move from the operating system terminal into the Python interpreter?

  1. 1
    Start at the terminal

    The operating system prompt is waiting for an instruction.

  2. 2
    Enter python

    Type the word python at the operating system prompt and press Enter.

  3. 3
    Check the new prompt

    The prompt changes to >>>. This indicates that the Python interpreter is ready.

Result

You are now inside the Python interpreter and can enter Python statements directly.

The Immediate Feedback Cycle

At the >>> prompt, type a Python statement and press Enter. Python immediately processes that statement and displays the result when there is a result to display. The >>> prompt then appears again. This means you can enter another statement without first creating a complete program or waiting for a separate run step.

ReadStatementEvaluateExecutePrintResultLoopNext >>>thenthenthennext statement
What happens after you type a Python statement at the interpreter prompt, and how does the result lead to the next prompt?
Key point

REPL means Read-Eval-Print Loop. The name describes the repeating sequence: Python reads your statement, evaluates it, prints the result, and loops back to the prompt.

Entering a Statement

python

This source example shows the complete feedback cycle. The statement is entered after >>>. After Enter is pressed, Python displays Hello World on the next line. The >>> prompt then reappears, showing that the interpreter is ready for another statement.

UserTypes statementPython interpreterReads and evaluatesTerminal screenDisplays result and >>>statementresultnext >>> prompt
How does a statement typed at the Python prompt move from input to execution and displayed output?

Returning to the Terminal

The REPL continues accepting statements until you exit the interpreter. When you finish experimenting, type exit() at the >>> prompt and press Enter. Python closes, and the operating system terminal prompt appears again. You can then use operating-system commands or launch Python again.

Mistakes with Prompts

  • Common mistake

    Typing Python code at the operating system prompt

    What it looks like

    Entering a Python statement while the prompt is $ or C:\>.

    Why it fails

    The operating system prompt understands operating-system commands, not Python code.

    Do this instead

    Type python first, press Enter, and wait for >>> before entering Python statements.

  • Common mistake

    Assuming the command python has already opened Python

    What it looks like

    Typing python and then continuing without checking the prompt.

    Why it fails

    The important signal is the prompt change to >>>.

    Do this instead

    Confirm that >>> appears before entering Python code.

  • Common mistake

    Expecting the prompt to disappear after one statement

    What it looks like

    Entering a statement, seeing its output, and thinking the interpreter has finished.

    Why it fails

    The REPL loops back to the Python prompt after each statement.

    Do this instead

    Treat the returning >>> as an invitation to enter the next statement.

  • Common mistake

    Forgetting how to return to the operating system terminal

    What it looks like

    Continuing to enter statements after finishing an experiment.

    Why it fails

    The interpreter remains active until you exit it.

    Do this instead
    Type exit() at >>> and press Enter.

Practice the Transition

EASY

Imagine that your terminal currently shows an operating-system prompt. Describe the sequence you would use to enter Python, identify the signal that confirms the transition, enter one Python statement, and return to the operating-system terminal.

Hints
  • Begin with the command typed at the operating-system prompt.
  • Name the exact prompt that signals Python is ready.
  • Include what happens after you press Enter on a Python statement.
  • Finish with the command used to leave the interpreter.

What do you think happens?

After Python displays the result of a statement in the interpreter, what should you expect to see next?

  • The operating-system prompt

  • The >>> prompt

  • Nothing until the terminal is reopened

Reveal answer
Answer:

The >>> prompt

The REPL loops back to the Python prompt after reading, evaluating, and printing a statement, so another statement can be entered immediately.

Key Takeaways

  • The operating-system terminal uses a prompt such as $ or C:\>, while the Python interpreter uses >>>.
  • Type python at the operating-system prompt and press Enter to open the Python interpreter.
  • At >>>, Python statements are processed immediately.
  • The REPL means Read-Eval-Print Loop: read, evaluate, print, and return to the prompt.
  • Type exit() at >>> to close Python and return to the operating-system terminal.

More concepts starting with W