What Is a Statement?
A statement is a unit of code that the Python interpreter can execute. It is code that causes the interpreter to perform an action.
A Program Moves One Step at a Time
A Python program is a sequence of instructions. Python does not execute all of those instructions at once. It runs statements in order from top to bottom, and each statement completes before the next one begins. This ordering gives you a practical way to understand a program: start at the first statement, determine what it does, then continue to the next.
A statement is a unit of code that the Python interpreter can execute. It is code that causes the interpreter to perform an action.
Tracing a Script
To predict what a multi-statement script will do, trace it line by line. For each statement, ask two questions: What does this statement do, and does it produce output? Also note whether it changes a variable, because a value assigned earlier remains available for use in later statements.
message = "Ready"
print(message)
print("Go")- 1First, Python executes message = "Ready". The value is stored for later use, and this assignment produces no visible output.
- 2Next, Python executes
print(message). The stored value is used, so Ready appears on screen. - 3Finally, Python executes
print("Go"). Go appears after Ready because this statement runs later.
- The first statement assigns a value to message.
- The second statement displays the value stored in message.
- The third statement displays Go.
Ready
GoPredicting the output order
What appears when Python executes the three statements from top to bottom?
- 1Trace the assignment
The first statement stores Ready in message. It changes what value is available later, but it does not display anything.
- 2Trace the first display
The second statement uses message with
print(), so the stored value Ready is displayed. - 3Trace the second display
The third statement uses
print()with Go, so Go is displayed after Ready.
The visible output is Ready on the first line, followed by Go on the second line.
Storing Versus Displaying
The two fundamental statement types highlighted here are expression statements, such as print(), and assignment statements, written in the form variable = value. They perform different kinds of work. An assignment statement stores a value in memory. A print() statement displays a result. Because storing and displaying are different actions, an assignment can change what later statements use without producing anything visible immediately.
5Interactive Mode Responses
Interactive mode also follows the statement type. The interpreter executes each statement you enter in order and displays a result if there is one. Expression statements produce output in interactive mode, while assignment statements execute silently. This explains why entering a display-oriented statement can show something immediately, while entering an assignment may show no visible response even though the value is now available for later use.
| Statement type | Typical form | Interactive response |
|---|---|---|
| Expression statement | print(value) | Produces output |
| Assignment statement | variable = value | Executes silently and stores a value |
The source distinguishes these two statement types by the action they perform and what appears in interactive mode.
Mistakes Beginners Make
Common mistake Assuming every executed statement prints something
What it looks liketotal = 10Why it failsAn assignment stores a value but produces no output.
Do this insteadUse print(total) when you want the stored value to appear.
Common mistake Reading a multi-statement script as if all lines happen at once
What it looks likename = "Ada" print(name) print("Done")Why it failsPython executes statements sequentially, from top to bottom, with each statement completing before the next begins.
Do this insteadTrace the script one statement at a time and record output in execution order.
Common mistake Thinking a silent assignment cannot affect later code
What it looks likemessage = "Ready" print(message)Why it failsValues assigned in earlier statements remain available for later statements.
Do this insteadTrack both visible output and changes to values as you trace.
Practice the Trace
What do you think happens?
What is displayed, and in what order, when this script runs?
first = "One"
second = "Two"
print(second)
print(first)Reveal answer
Two, then One
Python executes from top to bottom. The first two statements store values silently. The third statement displays the value stored in second, and the fourth displays the value stored in first.
Trace this script yourself. Identify which statements store values, identify which statements display results, and write the visible output in order.
language = "Python"
print("Learning")
print(language)Hints
- Start with the assignment and decide whether it displays anything.
- Then follow the two
print()statements in their written order. - Use the stored value of language when tracing the final statement.
Why Statements Matter
Statements are the building blocks of programs because each one is an executable unit of work. A program may contain only a few statements or many thousands, but the interpreter applies the same basic model: read a statement, execute it, complete it, and move to the next. Once you can identify the statement type and trace the order, you can predict both what the program stores and what it displays.
- 1A statement is a unit of code that the Python interpreter can execute.
- 2Python executes statements sequentially from top to bottom, with each statement completing before the next begins.
- 3Assignment statements store values and produce no output.
- 4Expression statements such as
print()display results in interactive mode. - 5To predict a script, trace each statement and record both visible output and changes to values.
Key Takeaways
- A statement is executable code that causes the Python interpreter to perform an action.
- Statements run one after another from top to bottom.
- Assignments store values silently, while
print()statements display results. - Earlier assignments remain available to later statements.
- Tracing each statement in order helps you predict output and debug programs.