Concepts / Using the Python Interactive Mode (REPL)

Using the Python Interactive Mode (REPL)

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

  • 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

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.

entersubmitexecuteREPL promptenter a statementStatementunit of executable codePython interpreterexecutes the unitActioncode takes effect
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.

producesexecutes asExpressionstatementprintAssignmentstatementvariable = valueDisplayed resultoutput in interactive modeSilent executionno displayed result
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

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.

Reading an Expression Statement

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

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

Execution: The Python interpreter executes the statement.

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

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

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.

Reading an Assignment Statement

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

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

Execution: The Python interpreter executes the assignment.

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

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.

submitdisplayssubmitresponds withExpressionstatemententered firstAssignmentstatementvariable = valueExecuteinterpreter actsExecuteinterpreter actsOutputdisplayedNo outputsilent response
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.

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

Common Recognition Mistakes

  • Assuming every statement displays output

    Assignment statements execute silently in interactive mode.

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

  • Treating silence as failure

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

    Fix: Interpret the absence of output according to the statement type.

  • Thinking only interactive entries are statements

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

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

  • Ignoring execution order

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

    Fix: 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. Identify the statement type before predicting the response.
  2. Ask whether the entry is an expression statement or follows the variable = value assignment pattern.
  3. Predict execution first: every statement is a unit the interpreter can execute.
  4. Predict visibility second: expression statements produce output in interactive mode, while assignment statements execute silently.
  5. Check the entries in their order because the interpreter executes statements in order.

Working Mental Model

  1. A statement is a unit of code that the Python interpreter can execute.
  2. Expression statements, such as print, produce output in interactive mode.
  3. Assignment statements use the pattern variable = value and execute silently in interactive mode.
  4. The interpreter executes statements in order and displays a result when the statement type produces one.
  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.