Introduction to Control Flow Statements
A statement is a unit of code that the Python interpreter can execute—code that causes the interpreter to perform an action.
From Code to Action
When you write Python code, you are giving instructions to the interpreter. A statement is a complete unit of code that the Python interpreter can execute. In other words, it is code that causes the interpreter to perform an action. Some statements calculate a value, while others store a value. Together, statements turn written code into a program that does something.
A statement is executable code: a unit of code that the Python interpreter performs.
This definition gives you a useful test. When you see a complete piece of Python code, ask what the interpreter does with it. Does it produce a value or output? Does it store a value? The answer helps you recognize the statement's role.
A Statement in Motion
The diagram shows the key interactive-mode contrast. The interpreter executes both kinds of statements, but the visible response depends on the statement type. An expression statement produces output in interactive mode. An assignment statement performs its storage action silently in interactive mode.
What do you think happens?
In interactive mode, what should you expect after entering an expression statement such as print("Hello") and then an assignment statement such as score = 10?
Reveal answer
Answer: The expression statement produces output, while the assignment statement executes silently.
The source distinguishes the two responses in interactive mode: expression statements produce output, while assignment statements execute silently.
Two Fundamental Statement Types
Python has many kinds of statements. Two fundamental types highlighted here are expression statements and assignment statements. They are both executable units of code, but they emphasize different actions: an expression statement evaluates an expression and can produce output in interactive mode, while an assignment statement stores a value in a variable.
| Statement type | Typical form | Main action | Interactive-mode response |
|---|---|---|---|
| Expression statement | An expression, such as print("Hello") | Evaluates an expression and can produce output | Produces output |
| Assignment statement | variable = value | Stores a value in a variable | Executes silently |
The two statement types emphasized in this introduction.
Reading a Short Sequence
Following Statements in Order
Consider these interactive-mode entries: print("Ready"), score = 10, and score. Predict what happens after each one.
First statement: print("Ready") is used as an expression statement in this example. It produces the output Ready in interactive mode.
Second statement: score = 10 is an assignment statement. It stores the value 10 in the variable score, but the assignment executes silently in interactive mode.
Third statement: score is an expression statement. It refers to the stored value, so interactive mode displays the result 10.
Order matters: The interpreter executes each statement in order and moves to the next statement. The second statement changes what is available when the third statement is evaluated.
The sequence produces visible output for the first and third entries, while the assignment in the middle performs its storage action without displaying a result.
This example illustrates why it is useful to separate an action from its visible response. The assignment does not print anything, but it changes the program's available information by storing a value in a variable. The later expression can then produce that value as output.
In interactive mode, the interpreter executes statements in order. To predict the next response, identify the statement type and then ask whether it produces output or performs a silent assignment.
Mistakes About Visible Results
Assuming that every executed statement displays something in interactive mode.
Assignment statements execute silently in interactive mode even though they store a value.
Fix:
Separate execution from display. An assignment can change stored information without producing visible output.Treating an expression statement and an assignment statement as the same kind of action.
The first is an expression statement that produces output, while the second is an assignment statement that stores a value.
Fix:
Look for the statement's main action: expression statements evaluate and can produce output; assignment statements store a value in a variable.Ignoring statement order when predicting behavior.
The interpreter executes statements in order, so an earlier statement cannot rely on a later assignment having already occurred.
Fix:
Trace the sequence from top to bottom and record what each statement does before moving to the next one.Thinking that code is useful only when it prints text.
Statements can perform actions other than displaying output, including storing values.
Fix:
Ask what action the interpreter performs, not only what appears on the screen.
When debugging interactive Python, predict two separate things for each statement: the action the interpreter performs and the visible response, if any. This prevents a silent assignment from being mistaken for code that did nothing.
Practice the Interpreter's Response
For each entry, identify whether it is being used as an expression statement or an assignment statement. Then state whether interactive mode produces output or executes silently: print("Start"), total = 25, and total.
Hints
- An assignment statement has the form variable = value.
- Expression statements produce output in interactive mode.
- Assignment statements execute silently in interactive mode.
- Trace the entries in order because the final expression uses the value stored earlier.
Practice Check
Classify print("Start"), total = 25, and total, and predict the interactive-mode response.
print("Start"): This is treated as an expression statement in the example, so it produces output.
total = 25: This is an assignment statement, so it stores a value in total and executes silently.
total: This is an expression statement that evaluates the stored value, so interactive mode displays the result.
The first entry produces Start, the second produces no displayed result, and the third displays 25.
Programs Built from Statements
A working Python program is a sequence of executable statements. A short script may contain only a few statements, while a complex application may contain thousands, but the basic principle remains the same: the interpreter reads statements in order, executes each one, and moves to the next.
The two statement types in this lesson provide the starting mental model. Expression statements evaluate expressions and produce output in interactive mode. Assignment statements store values and execute silently in interactive mode. Later, you will encounter other statement types, including if statements, for loops, and function definitions. They all follow the same central principle: they are executable units of code that the interpreter performs.
- A statement is a unit of code that the Python interpreter can execute.
- Expression statements, such as print, produce output in interactive mode.
- Assignment statements use the pattern variable = value and execute silently in interactive mode.
- The interpreter executes statements in order.
- Statements are the building blocks that make programs perform actions.
Key Takeaways
- A statement is a complete, executable unit of Python code.
- Expression statements evaluate expressions and produce output in interactive mode.
- Assignment statements store values in variables and execute silently in interactive mode.
- The interpreter executes statements in order, so statement sequence affects what happens next.
- Programs are built by combining statements into a sequence of actions.