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?
Identify the empty body: A block with no statement is an empty block, and Python reports a SyntaxError for an empty block.
Add pass: Place the pass statement inside the block. pass does nothing, but it counts as the required statement.
Continue 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 ... >>>
Terminating a Block
- Enter the block header at the >>> prompt.
- Wait for the prompt to change from >>> to ... .
- Enter at least one statement in the block, such as pass.
- Press Enter once to move to a new line.
- Press Enter again to create a blank line.
- Confirm 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
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
Leaving a block empty.
Every Python block must contain at least one statement. An empty block causes a SyntaxError.
Fix:
Add the actual statement or use pass as a placeholder.Treating pass as code that performs an action.
pass is a placeholder that does nothing.
Fix:
Use pass only when you need a syntactically valid block but are not ready to write the actual code.Forgetting to terminate the block in the interpreter.
The ... prompt means the interpreter is still waiting for the block body.
Fix:
Press 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
- Every Python block must contain at least one statement.
- An empty block causes a SyntaxError.
- pass is a placeholder statement that does nothing but makes a block syntactically valid.
- The interpreter changes from >>> to ... while it waits for a block body.
- Press Enter twice to create a blank line, terminate the block, and return to >>>.
Key Takeaways
- 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.