Indentation and Code Blocks
When you read Python code, you see lines arranged in an editor.
One Statement, Different Layouts
When you read Python code, you see lines arranged in an editor. Python also interprets those lines as statements. Most of the time, one physical line becomes one logical line, but Python provides ways to connect several physical lines into one logical statement or to place multiple logical statements on one physical line. Understanding this difference explains why some multi-line expressions work and why other line breaks end a statement.
A physical line is what appears in your editor. A logical line is what Python interprets as a single statement.
The Default Line Rule
By default, Python treats each physical line as one logical line. This is the ordinary case and supports readable code because the visual line arrangement usually matches the statements Python interprets.
A simple one-line statement
Determine how Python interprets a simple statement that occupies one physical line.
- 1Observe the editor
The statement appears on one physical line.
- 2Apply the default
Python normally treats that physical line as one logical line.
- 3Identify the result
The complete statement is interpreted as one logical line.
One physical line corresponds to one logical line in the standard case.
Implicit Line Joining
Implicit line joining occurs when parentheses, brackets, or braces remain unclosed. Python recognizes that the statement is not complete on the current physical line and continues reading the next physical line as part of the same logical line. This is the preferred way to break a long statement across physical lines.
A long expression inside parentheses
Explain why an arithmetic expression spread across two physical lines can remain one logical line.
- 1Start the expression
The first physical line contains an opening parenthesis.
- 2Check whether the delimiter is closed
The opening parenthesis has no matching closing parenthesis on the first line.
- 3Read the next line
Python continues reading the next physical line as part of the same logical line.
- 4Complete the statement
The logical statement is complete when the closing parenthesis is reached.
Several physical lines can form one logical statement through implicit line joining.
Prefer implicit line joining whenever the statement naturally contains parentheses, brackets, or braces. The source material identifies this approach as the most readable and the standard choice in modern Python.
Explicit Line Joining
Explicit line joining uses a backslash at the end of a physical line. Python joins that line with the following physical line before interpreting the result, so the connected lines become one logical line. This technique is useful for long strings or expressions that do not naturally contain parentheses, brackets, or braces.
A long string split with a backslash
Determine how Python interprets a long string assignment split across two physical lines with a backslash at the end of the first line.
- 1Inspect the first line
The first physical line ends with a backslash.
- 2Connect the lines
The backslash explicitly joins the first physical line to the following physical line.
- 3Interpret the result
Python treats the joined text as one logical line before executing it.
The two physical lines represent one logical statement.
Semicolons on One Line
A semicolon can separate multiple logical lines on one physical line. In other words, one visible line can contain more than one statement when semicolons are used as separators.
| Arrangement | Logical interpretation | Guidance |
|---|---|---|
| Two statements separated by a semicolon | Multiple logical lines on one physical line | Discouraged |
| Two statements on separate physical lines | Each physical line normally represents one logical line | Preferred for readability |
The source material contrasts semicolon-separated statements with separate physical lines.
Although semicolons can separate statements, the source material recommends not using them to place multiple statements on one line. Give each statement its own physical line because this makes code easier to read, debug, and understand.
Reading Code Blocks
The section topic is named Indentation and Code Blocks, but the provided material focuses its detailed explanation on physical lines, logical lines, and line joining. It establishes that readable arrangement matters and that statements can occupy one or several physical lines. It does not provide a separate rule set for how changes in indentation assign statements to code blocks, so this article does not add unstated indentation rules.
Common Line-Joining Mistakes
Common mistake Counting physical lines as though they always equal logical lines.
What it looks likeA long expression is displayed across two physical lines, but an unclosed parenthesis connects them.
Why it failsImplicit line joining can make several physical lines part of one logical statement.
Do this insteadCheck whether an unclosed parenthesis, bracket, or brace, or an explicit backslash, continues the statement.
Common mistake Using a backslash when delimiters already provide implicit joining.
What it looks likeA long expression that naturally contains parentheses is split with explicit joining instead.
Why it failsThe source material identifies implicit joining as the preferred and more readable approach when it is available.
Do this insteadUse parentheses, brackets, or braces to continue the statement when the expression naturally supports them.
Common mistake Putting several statements on one physical line with semicolons.
What it looks likeTwo separate statements are written on one line with a semicolon between them.
Why it failsAlthough semicolons can separate logical lines, the practice makes code harder to read, debug, and understand.
Do this insteadGive each statement its own physical line.
Common mistake Assuming every long statement requires a backslash.
What it looks likeA multi-line arithmetic expression inside parentheses is treated as needing explicit joining.
Why it failsUnclosed delimiters already provide implicit line joining.
Do this insteadPrefer implicit line joining when parentheses, brackets, or braces are available.
Choose the Joining Method
For each situation, choose the clearest interpretation: default physical-to-logical treatment, implicit line joining, explicit line joining, or separate physical lines. Situation one: a simple statement occupies one physical line. Situation two: a long expression continues inside an unclosed pair of parentheses. Situation three: a long string does not naturally contain delimiters. Situation four: two independent statements are being placed on one line with a semicolon.
Hints
- The default is one physical line treated as one logical line.
- Unclosed parentheses, brackets, and braces support implicit joining.
- A backslash is useful when implicit joining is not available.
- Semicolons can separate statements, but separate physical lines are recommended.
Selecting a technique
Choose a line arrangement for a long expression that naturally contains parentheses.
- 1Identify the available structure
The expression already contains parentheses.
- 2Compare the joining methods
Both explicit joining with a backslash and implicit joining can connect physical lines, but implicit joining is the preferred method when delimiters are available.
- 3Choose the clearer layout
Break the expression inside the parentheses so Python continues reading it as one logical line.
Use implicit line joining for a long expression that naturally contains parentheses.
Key Takeaways
- 1A physical line is what appears in the editor; a logical line is what Python interprets as one statement.
- 2By default, Python treats each physical line as one logical line.
- 3Implicit line joining uses unclosed parentheses, brackets, or braces and is the preferred way to break long statements when possible.
- 4Explicit line joining uses a backslash and is useful for long strings or expressions without suitable delimiters.
- 5Semicolons can separate multiple logical lines on one physical line, but separate physical lines are recommended for readability and maintenance.
At a glance
- Physical lines describe what appears in the editor, while logical lines describe the statements Python interprets.
- Several physical lines can become one logical line through implicit or explicit line joining.
- Implicit joining with unclosed parentheses, brackets, or braces is preferred when the statement naturally supports it.
- A backslash provides explicit joining when implicit joining is not available.
- Semicolons can place multiple logical statements on one physical line, but separate physical lines are clearer.