Working with the Python Interpreter
Every block in Python must contain at least one statement. An empty block causes a SyntaxError.
Why Empty Blocks Fail
When Python encounters a block, it expects that block to contain at least one statement. A block with no statement is empty, and an empty block causes a SyntaxError. This rule matters when you are experimenting in the interpreter or outlining code before you are ready to implement its behavior.
The pass Placeholder
The pass statement is a placeholder that does nothing. Its purpose is structural: it gives a block the statement it requires, even when the actual code for that block is not ready. Using pass therefore lets you write a syntactically valid block without adding behavior to it.
In this generated example, the block contains pass, so it is not empty. The statement does nothing, but it satisfies the requirement that the block contain at least one statement.
Giving an unfinished block a valid body
You are not ready to write the actual code inside a Python block. How can you keep the block syntactically valid?
- 1Identify the empty body
A block with no statement is an empty block, and Python reports a
SyntaxErrorfor an empty block. - 2Add pass
Place the pass statement inside the block. pass does nothing, but it counts as the required statement.
- 3Continue later
The block is now syntactically valid while its actual behavior remains to be written.
Use pass as a temporary placeholder when a block is required but its real code is not ready.
Reading Interpreter Prompts
The Python interpreter uses its prompt to show whether it is ready for a new top-level entry or is waiting for the body of a block. The normal prompt is >>>. After you enter a block, the prompt changes to ... . The ... prompt signals that the interpreter is waiting for the block body.
>>> if True:
... pass
...
>>>- 1Enter the block header at the >>> prompt.
- 2The interpreter changes the prompt to ... because it is waiting for the body.
- 3Enter pass as the block's statement.
- 4Create a blank line to terminate the block.
- 5The prompt returns to >>>.
- The >>> prompt indicates the interpreter is ready for a new entry.
- After the block begins, the ... prompt indicates that the interpreter is waiting for the block body.
- The indented pass statement supplies the block body.
- After the block is terminated, the >>> prompt returns.
Terminating a Block
- 1Enter the block header at the >>> prompt.
- 2Wait for the prompt to change from >>> to ... .
- 3Enter at least one statement in the block, such as pass.
- 4Press Enter once to move to a new line.
- 5Press Enter again to create a blank line.
- 6Confirm that the prompt returns to >>>.
What do you think happens?
After entering a block body at the ... prompt, what should you do to return to the >>> prompt?
Reveal answer
Press Enter twice: once to move to a new line and once more to create a blank line.
The blank line terminates the block in the interpreter, after which the prompt returns to >>>.
Mistakes to Avoid
Common mistake Leaving a block empty.
What it looks likeA block is started, but no statement is placed inside it.
Why it failsEvery Python block must contain at least one statement. An empty block causes a
SyntaxError.Do this insteadAdd the actual statement or use pass as a placeholder.
Common mistake Treating pass as code that performs an action.
What it looks likeUsing pass and expecting it to produce behavior inside the block.
Why it failspass is a placeholder that does nothing.
Do this insteadUse pass only when you need a syntactically valid block but are not ready to write the actual code.
Common mistake Forgetting to terminate the block in the interpreter.
What it looks likeContinuing to see the ... prompt after entering the block body.
Why it failsThe ... prompt means the interpreter is still waiting for the block body.
Do this insteadPress Enter twice to create a blank line and return to the >>> prompt.
Practice
In your own words, explain the difference between the >>> and ... prompts. Then describe what you would add to a block if its real code was not ready yet, and state how you would finish entering that block in the interpreter.
Hints
- One prompt indicates readiness for a new entry; the other indicates that the interpreter is waiting for a block body.
- The placeholder statement does nothing but prevents the block from being empty.
- A blank line terminates the block.
Key Takeaways
- 1Every Python block must contain at least one statement.
- 2An empty block causes a
SyntaxError. - 3pass is a placeholder statement that does nothing but makes a block syntactically valid.
- 4The interpreter changes from >>> to ... while it waits for a block body.
- 5Press Enter twice to create a blank line, terminate the block, and return to >>>.
At a glance
- Python does not allow an empty block; each block needs at least one statement.
- Use pass as a temporary placeholder when the actual block code is not ready.
- The ... prompt means that the interpreter is waiting for a block body.
- A blank line, entered by pressing Enter twice, terminates the block and brings back the >>> prompt.