Indentation and Code Blocks
A physical line is what appears in your editor; a logical line is what Python interprets as a single statement.
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.
Observe the editor: The statement appears on one physical line.
Apply the default: Python normally treats that physical line as one logical line.
Identify 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.
Start the expression: The first physical line contains an opening parenthesis.
Check whether the delimiter is closed: The opening parenthesis has no matching closing parenthesis on the first line.
Read the next line: Python continues reading the next physical line as part of the same logical line.
Complete 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.
Inspect the first line: The first physical line ends with a backslash.
Connect the lines: The backslash explicitly joins the first physical line to the following physical line.
Interpret 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 |
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
Counting physical lines as though they always equal logical lines.
Implicit line joining can make several physical lines part of one logical statement.
Fix:
Check whether an unclosed parenthesis, bracket, or brace, or an explicit backslash, continues the statement.Using a backslash when delimiters already provide implicit joining.
The source material identifies implicit joining as the preferred and more readable approach when it is available.
Fix:
Use parentheses, brackets, or braces to continue the statement when the expression naturally supports them.Putting several statements on one physical line with semicolons.
Although semicolons can separate logical lines, the practice makes code harder to read, debug, and understand.
Fix:
Give each statement its own physical line.Assuming every long statement requires a backslash.
Unclosed delimiters already provide implicit line joining.
Fix:
Prefer 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.
Identify the available structure: The expression already contains parentheses.
Compare 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.
Choose 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
- A physical line is what appears in the editor; a logical line is what Python interprets as one statement.
- By default, Python treats each physical line as one logical line.
- Implicit line joining uses unclosed parentheses, brackets, or braces and is the preferred way to break long statements when possible.
- Explicit line joining uses a backslash and is useful for long strings or expressions without suitable delimiters.
- Semicolons can separate multiple logical lines on one physical line, but separate physical lines are recommended for readability and maintenance.
Key Takeaways
- 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.