Concepts / Variables and Assignment in Interactive Mode

Variables and Assignment in Interactive Mode

Interactive mode is a live environment for testing Python commands one at a time.

  • Programming

Starting the Conversation

Interactive mode gives you a live conversation with Python. Instead of writing a complete script and running it all at once, you enter one command, see what Python does immediately, and then decide what to try next. This makes interactive mode useful for experimenting, testing ideas, and investigating mistakes while you learn.

  1. 1
    Make sure Python is installed on your machine.
  2. 2
    Open a terminal or command window.
  3. 3
    Type python and press Enter.
  4. 4
    Wait for Python to display its version information and welcome text.
  5. 5
    Look for the >>> prompt.
Terminalready for inputpythontyped and submittedPython interpreterstarts>>>ready for a commandtypepress Enterdisplay version information, then prompt
How does control move from typing python in a terminal to seeing the Python version information and the >>> prompt?

Reading the Prompt

The >>> prompt is Python's readiness signal. When it is visible, Python is waiting for you to enter a command. Type your command after the prompt and press Enter. Do not type the >>> characters themselves; Python displays them for you.

Waiting>>> visibleCommand enteredpress EnterEvaluatingPython processes thecommandWaitingnew >>> visibletype commandpress Enterevaluation finishes
What does the >>> prompt signal about Python's current state and when you can enter a command?

Tracing One Command

Worked example

Evaluating a Simple Expression

What happens when you enter the mathematical expression 2 + 3 at the Python prompt?

  1. 1
    Enter the command

    At the visible >>> prompt, type only 2 + 3 and press Enter. The prompt itself was printed by Python and is not part of what you type.

  2. 2
    Python evaluates it

    Python processes the arithmetic expression.

  3. 3
    The result appears

    The result 5 is displayed on the following line.

  4. 4
    The prompt returns

    A new >>> prompt appears, showing that Python is ready for another command.

Result

The command produces 5, and the new >>> prompt marks the beginning of the next interaction.

python
Output
5
>>>
2 + 3entered by learnerEvaluatePython processes theexpression5displayed result>>>ready for the next commandpress Enterproducethen display
Where does a command go after you type it at the >>> prompt, and what appears before Python presents the next prompt?

The Read-Evaluate-Print Cycle

Interactive mode repeats one consistent cycle. Python reads the command you enter, evaluates it, prints the result, and displays a new >>> prompt. The returned prompt is important because it marks the start of the next cycle. This immediate sequence is what makes interactive mode different from running a script all at once: you can observe each command's result before entering another command.

Readreceive your commandEvaluateprocess the commandPrintshow the result>>>wait for the next commandcommand receivedevaluation completesresult displayednext command
What happens after you enter a command at the >>> prompt, and how does Python read, evaluate, display the result, and wait for the next command?
Key point

The cycle is not finished when the result appears. It is complete when the new >>> prompt returns. That prompt tells you Python has finished evaluating the previous command and is ready to receive another one.

Mistakes at the Prompt

  • Common mistake

    Typing the >>> characters as part of the command.

    What it looks like
    >>> 2 + 3
    Why it fails

    The >>> characters are Python's prompt, not characters that you should enter.

    Do this instead

    Type only 2 + 3 after the prompt that Python has already displayed.

  • Common mistake

    Entering a command before Python is ready.

    What it looks like

    Typing while the >>> prompt is missing.

    Why it fails

    The visible prompt is the signal that Python is waiting for input. If it is missing or replaced, Python may still be processing a command or something may have gone wrong.

    Do this instead

    Wait for the >>> prompt to appear before entering the next command.

  • Common mistake

    Assuming interactive mode displays the next prompt before the result.

    What it looks like

    Expecting >>> immediately after pressing Enter.

    Why it fails

    Python evaluates the command and prints its result before showing a new >>> prompt.

    Do this instead

    Read the result first, then use the returned prompt as the signal for the next command.

Best practice

Use the prompt as a rhythm for your work: see >>>, enter one command, press Enter, inspect the result, and wait for >>> to return. This habit helps you distinguish a ready interpreter from one that is still processing a previous command.

Try the Cycle Yourself

EASY

In a terminal, start Python by typing python and pressing Enter. When the >>> prompt appears, enter 2 + 3 without typing the prompt characters. Before pressing Enter, predict what will appear first and what will appear afterward. Then compare your observation with the read-evaluate-print cycle.

Hints
  • The first visible result comes from evaluating the expression.
  • The new >>> prompt appears after the result.
  1. 1
    Interactive mode is a live environment for testing Python commands one at a time. You start it by typing python in a terminal. Python then displays version information and the >>> prompt. The prompt means that Python is ready for input, and you should type only your command after it. Each interaction follows the same pattern: Python reads your command, evaluates it, prints the result, and displays a new >>> prompt.

Key Takeaways

  • Type python in a terminal to start Python's interactive interpreter.
  • Python displays version information and then the >>> prompt.
  • The >>> prompt means Python is ready to receive a command; do not type the prompt yourself.
  • Interactive mode follows a read-evaluate-print cycle: read, evaluate, print the result, and show a new prompt.
  • The returned >>> prompt signals that the previous command is complete and the interpreter is ready for the next command.

Related concepts

More concepts starting with V