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.
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?
Reveal 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.
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.
| Statement type | Source pattern | Interactive-mode response |
|---|---|---|
| Expression statement | An expression such as print | The interpreter executes it and produces output |
| Assignment statement | variable = value | The 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.
Reading an Expression Statement
Suppose you enter an expression statement such as print at the interactive prompt. What should you look for next?
- 1Recognize the statement
The entered code is an expression statement, one of the two statement types highlighted in the source material.
- 2Execution
The Python interpreter executes the statement.
- 3Response
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?
- 1Recognize the statement
The pattern variable = value identifies this as an assignment statement.
- 2Execution
The Python interpreter executes the assignment.
- 3Response
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.
What do you think happens?
You enter an assignment statement and see no displayed result. Which interpretation best matches the source material?
Reveal 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
Common mistake Assuming every statement displays output
What it looks likeTreating an assignment statement such as score = 5 as though it must display a result
Why it failsAssignment statements execute silently in interactive mode.
Do this insteadFirst 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 likeConcluding that the interpreter did nothing because an assignment produced no visible response
Why it failsThe source material identifies silent execution as the expected response for assignment statements.
Do this insteadInterpret the absence of output according to the statement type.
Common mistake Thinking only interactive entries are statements
What it looks likeUsing the word statement only for code entered at the REPL
Why it failsEvery Python program is built from statements, whether the code is entered interactively or organized as a program.
Do this insteadUse statement to mean a unit of code that the interpreter can execute.
Common mistake Ignoring execution order
What it looks likeReasoning about a later statement without considering the statements entered before it
Why it failsIn interactive mode, the interpreter executes each statement in order.
Do this insteadTrace the sequence one statement at a time.
REPL Prediction Practice
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.
- 1Identify the statement type before predicting the response.
- 2Ask whether the entry is an expression statement or follows the variable = value assignment pattern.
- 3Predict execution first: every statement is a unit the interpreter can execute.
- 4Predict visibility second: expression statements produce output in interactive mode, while assignment statements execute silently.
- 5Check the entries in their order because the interpreter executes statements in order.
Working Mental Model
- 1A statement is a unit of code that the Python interpreter can execute.
- 2Expression statements, such as print, produce output in interactive mode.
- 3Assignment statements use the pattern variable = value and execute silently in interactive mode.
- 4The interpreter executes statements in order and displays a result when the statement type produces one.
- 5Statements 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.