Concepts / Indentation and Code Blocks

Indentation and Code Blocks

When you read Python code, you see lines arranged in an editor.

  • Programming

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.

Physical line 1one statementLogical line 1one statementPhysical line 1continuedLogical line 1one statementPhysical line 2continuedLogical line 1statement onePhysical line 1two statementsLogical line 2statement twointerpreted asjoinsjoinscontainscontains
How can physical lines and logical lines relate in Python?
Definition

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.

Worked example

A simple one-line statement

Determine how Python interprets a simple statement that occupies one physical line.

  1. 1
    Observe the editor

    The statement appears on one physical line.

  2. 2
    Apply the default

    Python normally treats that physical line as one logical line.

  3. 3
    Identify the result

    The complete statement is interpreted as one logical line.

Result

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.

(opening delimiterone logical statementends after closingdelimiterexpression partfirst physical lineexpression partnext physical line)closing delimiterstartscontinues toreachescompletes
What happens to a multi-line expression when an opening delimiter has not yet been closed?
Worked example

A long expression inside parentheses

Explain why an arithmetic expression spread across two physical lines can remain one logical line.

  1. 1
    Start the expression

    The first physical line contains an opening parenthesis.

  2. 2
    Check whether the delimiter is closed

    The opening parenthesis has no matching closing parenthesis on the first line.

  3. 3
    Read the next line

    Python continues reading the next physical line as part of the same logical line.

  4. 4
    Complete the statement

    The logical statement is complete when the closing parenthesis is reached.

Result

Several physical lines can form one logical statement through implicit line joining.

Best practice

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.

Long statement partphysical line 1 withbackslashOne logical statementjoined beforeinterpretationLong statement partphysical line 2joins withjoins with
How does a backslash connect two physical lines so Python interprets them as one logical line?
Worked example

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.

  1. 1
    Inspect the first line

    The first physical line ends with a backslash.

  2. 2
    Connect the lines

    The backslash explicitly joins the first physical line to the following physical line.

  3. 3
    Interpret the result

    Python treats the joined text as one logical line before executing it.

Result

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.

Physical linestatement one; statementtwoLogical statement 1before semicolonLogical statement 2after semicolonPhysical line 1statement onePhysical line 2statement twoseparatesseparatesnext physical line
How does a semicolon differ from using separate physical lines?
ArrangementLogical interpretationGuidance
Two statements separated by a semicolonMultiple logical lines on one physical lineDiscouraged
Two statements on separate physical linesEach physical line normally represents one logical linePreferred for readability

The source material contrasts semicolon-separated statements with separate physical lines.

Best practice

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.

Code blockgroup of statementsStatement 1logical lineStatement 2logical linecontainscontains
How can a code block be viewed as a group containing statements while keeping the line-interpretation question separate?

Common Line-Joining Mistakes

  • Common mistake

    Counting physical lines as though they always equal logical lines.

    What it looks like

    A long expression is displayed across two physical lines, but an unclosed parenthesis connects them.

    Why it fails

    Implicit line joining can make several physical lines part of one logical statement.

    Do this instead

    Check 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 like

    A long expression that naturally contains parentheses is split with explicit joining instead.

    Why it fails

    The source material identifies implicit joining as the preferred and more readable approach when it is available.

    Do this instead

    Use 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 like

    Two separate statements are written on one line with a semicolon between them.

    Why it fails

    Although semicolons can separate logical lines, the practice makes code harder to read, debug, and understand.

    Do this instead

    Give each statement its own physical line.

  • Common mistake

    Assuming every long statement requires a backslash.

    What it looks like

    A multi-line arithmetic expression inside parentheses is treated as needing explicit joining.

    Why it fails

    Unclosed delimiters already provide implicit line joining.

    Do this instead

    Prefer implicit line joining when parentheses, brackets, or braces are available.

Choose the Joining Method

MEDIUM

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.
Worked example

Selecting a technique

Choose a line arrangement for a long expression that naturally contains parentheses.

  1. 1
    Identify the available structure

    The expression already contains parentheses.

  2. 2
    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.

  3. 3
    Choose the clearer layout

    Break the expression inside the parentheses so Python continues reading it as one logical line.

Result

Use implicit line joining for a long expression that naturally contains parentheses.

Key Takeaways

  1. 1
    A physical line is what appears in the editor; a logical line is what Python interprets as one statement.
  2. 2
    By default, Python treats each physical line as one logical line.
  3. 3
    Implicit line joining uses unclosed parentheses, brackets, or braces and is the preferred way to break long statements when possible.
  4. 4
    Explicit line joining uses a backslash and is useful for long strings or expressions without suitable delimiters.
  5. 5
    Semicolons 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.

Related concepts

More concepts starting with I