Concepts / Using the Python Interactive Mode (REPL)

Using the Python Interactive Mode (REPL)

Seeing output after an expression statement does not mean that every statement will display something. The statement type determines the interactive response.

  • Programming

The Prompt as a Laboratory

The Python interactive mode, often called the REPL, lets you enter a unit of code and immediately observe how the interpreter responds. This makes it useful for learning the difference between code that produces output and code that performs an action without displaying a result.

What do you think happens?

You enter an expression statement at the interactive prompt. What should you expect the interpreter to do next?

  • Execute it and display its result

  • Execute it silently

  • Store it as a variable without executing it

Reveal answer
Answer:

Execute it and display its result

Expression statements produce output in interactive mode. Assignment statements, by contrast, execute silently.

A Statement as One Unit of Work

Definition

A statement is a unit of code that the Python interpreter can execute. It is code that causes the interpreter to perform an action.

When you write Python code, you are giving instructions to the interpreter. A statement is a complete executable unit within those instructions. The interpreter can take that unit, execute it, and then continue to the next statement. Statements therefore provide the basic connection between written code and actions performed by a program.

REPL promptenter a statementStatementunit of executable codePython interpreterexecutes the unitActioncode takes effectentersubmitexecute
How does a statement move from being entered at the REPL prompt to causing the Python interpreter to perform an action?

Two Fundamental Statement Types

The source material highlights two statement types that appear immediately when you work with Python: expression statements and assignment statements. Both are executable units, but they have different purposes and produce different visible behavior in interactive mode.

ExpressionstatementprintAssignmentstatementvariable = valueDisplayed resultoutput in interactive modeSilent executionno displayed resultproducesexecutes as
What is the difference between evaluating an expression for a displayed result and assigning a value to a variable without displaying that value?
Statement typeSource patternInteractive-mode response
Expression statementAn expression such as printThe interpreter executes it and produces output
Assignment statementvariable = valueThe interpreter executes it silently

The two statement types highlighted in the source material

Expression Statements and Visible Results

An expression statement is treated as an expression that the interpreter executes. In interactive mode, expression statements produce output. The visible result tells you that the interpreter performed the statement and has something to display.

Worked example

Reading an Expression Statement

Suppose you enter an expression statement such as print at the interactive prompt. What should you look for next?

  1. 1
    Recognize the statement

    The entered code is an expression statement, one of the two statement types highlighted in the source material.

  2. 2
    Execution

    The Python interpreter executes the statement.

  3. 3
    Response

    Because expression statements produce output in interactive mode, the interpreter displays output.

Result

An expression statement is executed and produces visible output in interactive mode.

Key point

Seeing output after an expression statement does not mean that every statement will display something. The statement type determines the interactive response.

Assignment Statements and Silent Execution

An assignment statement has the pattern variable = value. It performs an action by assigning a value to a variable. In interactive mode, an assignment statement executes silently, so you should not expect a displayed result immediately after entering it.

Worked example

Reading an Assignment Statement

Suppose you enter the assignment statement score = 5 at the interactive prompt. What should you expect the interpreter to display?

  1. 1
    Recognize the statement

    The pattern variable = value identifies this as an assignment statement.

  2. 2
    Execution

    The Python interpreter executes the assignment.

  3. 3
    Response

    Assignment statements execute silently in interactive mode, so no displayed result is expected from the assignment itself.

Result

The assignment executes silently.

Execution in Order

In interactive mode, the interpreter executes each statement in order and displays a result if there is one, depending on the statement type. This gives the REPL a simple rhythm: enter a statement, let the interpreter execute it, observe the appropriate response, and then continue.

Expressionstatemententered firstAssignmentstatementvariable = valueExecuteinterpreter actsExecuteinterpreter actsOutputdisplayedNo outputsilent responsesubmitdisplayssubmitresponds with
What does the interpreter do next when you enter an expression statement versus an assignment statement?

What do you think happens?

You enter an assignment statement and see no displayed result. Which interpretation best matches the source material?

  • The interpreter executed the assignment silently

  • The interpreter skipped the statement

  • The interpreter changed the assignment into an expression statement

Reveal answer
Answer:

The interpreter executed the assignment silently

Assignment statements execute silently in interactive mode. The absence of displayed output is part of their expected behavior.

Statements in Complete Programs

The same idea applies beyond the REPL. Every Python program is built from statements. A small script may contain only a few statements, while a complex application may contain thousands. In either case, the interpreter reads statements in order, executes each one, and moves to the next.

A program is therefore not one undifferentiated block of instructions. It is a sequence of executable units. The REPL makes this structure easy to observe because you submit one statement at a time. As you encounter other statement types, such as if statements, for loops, and function definitions, the same broad principle remains: statements are executable units that the interpreter performs.

Key point

Statements are the building blocks that make programs do things. Without executable statements, code would not perform actions.

Common Recognition Mistakes

  • Common mistake

    Assuming every statement displays output

    What it looks like

    Treating an assignment statement such as score = 5 as though it must display a result

    Why it fails

    Assignment statements execute silently in interactive mode.

    Do this instead

    First identify the statement type. Expect displayed output from an expression statement and silent execution from an assignment statement.

  • Common mistake

    Treating silence as failure

    What it looks like

    Concluding that the interpreter did nothing because an assignment produced no visible response

    Why it fails

    The source material identifies silent execution as the expected response for assignment statements.

    Do this instead

    Interpret the absence of output according to the statement type.

  • Common mistake

    Thinking only interactive entries are statements

    What it looks like

    Using the word statement only for code entered at the REPL

    Why it fails

    Every Python program is built from statements, whether the code is entered interactively or organized as a program.

    Do this instead

    Use statement to mean a unit of code that the interpreter can execute.

  • Common mistake

    Ignoring execution order

    What it looks like

    Reasoning about a later statement without considering the statements entered before it

    Why it fails

    In interactive mode, the interpreter executes each statement in order.

    Do this instead

    Trace the sequence one statement at a time.

REPL Prediction Practice

EASY

For each entry, identify whether it represents an expression statement or an assignment statement. Then predict whether interactive mode produces output or executes silently: print, score = 5, and another expression statement.

Hints
  • Use the two statement types highlighted in the source material.
  • The pattern variable = value identifies an assignment statement.
  • Expression statements produce output; assignment statements execute silently.
  1. 1
    Identify the statement type before predicting the response.
  2. 2
    Ask whether the entry is an expression statement or follows the variable = value assignment pattern.
  3. 3
    Predict execution first: every statement is a unit the interpreter can execute.
  4. 4
    Predict visibility second: expression statements produce output in interactive mode, while assignment statements execute silently.
  5. 5
    Check the entries in their order because the interpreter executes statements in order.

Working Mental Model

  1. 1
    A statement is a unit of code that the Python interpreter can execute.
  2. 2
    Expression statements, such as print, produce output in interactive mode.
  3. 3
    Assignment statements use the pattern variable = value and execute silently in interactive mode.
  4. 4
    The interpreter executes statements in order and displays a result when the statement type produces one.
  5. 5
    Statements are the executable building blocks of Python programs.

Key Takeaways

  • A statement is an executable unit of Python code that causes the interpreter to perform an action.
  • Expression statements and assignment statements are two fundamental statement types.
  • Expression statements produce output in interactive mode, while assignment statements execute silently.
  • The interpreter processes statements in order, both at the REPL and in complete programs.
  • Recognizing statement type helps you predict what the interpreter will do and display.

Related concepts

More concepts starting with U