Python Programming · Foundations

Python Syntax

Want it in plain words first? Jump to Eli explains — the same idea, no jargon.
On this page 9 sections
  1. In 30 seconds
  2. Why this matters
  3. The college version
  4. Eli explains
  5. Worked example
  6. Key takeaway
  7. Quick check
  8. Study tools
  9. Sources & references

In 30 seconds

Python syntax is the set of rules that lets Python turn source text into a program. Characters become tokens, tokens form logical lines, and statements follow those lines. Most whitespace merely separates tokens, but leading whitespace is meaningful: it groups a under a colon-ended header. Comments begin with # outside a string. For long expressions, parentheses, brackets, and braces provide the safest line continuation.

Why this matters

Syntax is the agreement between your source file and the parser. Knowing which spaces carry structure makes code easier to read and prevents a large class of immediate errors. It also helps when you encounter a SyntaxError, IndentationError, or TabError: these messages describe a failure to form valid program text, before Python can carry out the program's intended work. The same conventions make your code legible to classmates, collaborators, and future you.

The college version

From characters to statements

Python does not treat a source file as a bag of English-like sentences. Before the parser can decide whether a program follows the grammar, lexical analysis reads decoded source characters and produces tokens. Common categories include names, literals, operators, delimiters, and special structural markers such as NEWLINE, INDENT, and DEDENT. You do not need to inspect tokens for ordinary programming, but the model explains why apparently small edits can change a program's structure. For example, total=3 and total = 3 have the same essential tokens; spacing is not needed around the equals sign in that case. By contrast, blue sky contains two name tokens, whereas bluesky is one name token. Whitespace is required when omitting it would make adjacent characters a different token.

A is a sequence of characters ending in a line ending. A is what the language uses for a boundary. One logical line may occupy more than one physical line when a continuation rule applies. This distinction is useful because a source file can be formatted over several screen lines without necessarily becoming several statements. A simple statement is contained in one logical line. Several simple statements can technically share a logical line when separated by semicolons, but one statement per line is usually easier to scan and edit.

An is source text that Python evaluates to obtain a value; it can appear as part of a larger statement. A statement is a complete instruction in the grammar. This lesson does not attempt to catalog expression forms or statements such as assignment, import, or return. The useful syntax-level distinction is that expressions are ingredients, while statements establish the program's top-level actions and structure.

Whitespace, comments, and continued lines

Most spaces, tabs, and formfeeds between tokens are interchangeable delimiters. At the beginning of a logical line, however, leading spaces and tabs have another job: Python uses their level to determine how statements are grouped. This is the famous meaningful whitespace rule. It is not a style convention layered on top of braces; indentation is part of the language's structure.

A comment starts with # when that character is outside a string literal and continues through the physical line's end. The parser ignores the comment's text. Comments are for a reader: explain a non-obvious decision, constraint, or consequence rather than narrating a line that already reads clearly. A hash inside a quoted string is ordinary string content, not a comment starter.

There are two ways to continue a logical line. Explicit joining uses a backslash at the end of a physical line. The backslash joins that line to the next one, but it is fragile: it cannot be followed by a comment, and an accidental trailing character defeats the continuation. Implicit joining is usually clearer. An expression inside parentheses, square brackets, or curly braces may span physical lines without a backslash, and those continuation lines may contain comments. Format the pieces vertically so a reader can see what belongs together.

For example, this valid expression uses parentheses for continuation:

message = (
    "syntax gives code structure"
    " before it runs"
)
print(message)

Adjacent string literals inside parentheses are concatenated by the language, so the output is syntax gives code structure before it runs. The point here is formatting and grouping, not string techniques: the parentheses permit line breaks while preserving one logical line.

Suites: indentation that groups work

Some statements are compound statements. A compound statement is made of one or more clauses. Each clause has a header and a suite: the header begins with a keyword, ends in a colon, and its suite is the group of statements that the clause controls. Although a suite can be written as simple statements on the same line as a header, the normal multi-line form is the one that supports nested compound statements and is much more readable.

A deliberately minimal example shows only the shape:

if ready:
    print("begin")
print("outside the suite")

The indented print is in the if header's suite. The final print returns to the surrounding indentation level, so it is outside that suite. This lesson is about that structural boundary, not about how conditions work. A suite must have at least one statement; when a structure is intentionally empty during drafting, Python provides the pass statement as a syntactic placeholder.

The tokenizer compares leading indentation levels across logical lines and emits INDENT and DEDENT markers to represent changes. A line indented farther than the current level begins an indented block; returning to an earlier level closes one or more blocks. Use one consistent indentation convention in a file—four spaces is a widespread convention—and configure your editor to display whitespace when an indentation problem is hard to see. The language specifically rejects mixes of tabs and spaces when their meaning depends on tab width, raising TabError. Even a visually aligned mixture can therefore be a poor choice.

What syntax-related errors mean

A SyntaxError means Python could not parse the source as valid Python grammar. It can arise from many forms of invalid source: a missing colon after a compound-statement header, an unclosed delimiter, a misplaced keyword, or source that cannot be decoded. The caret and location in the message are clues, not always a proof that the indicated character is the original mistake; an earlier unclosed construct can make a later location look surprising.

IndentationError is the exception family associated with indentation problems. A common instance reports an unexpected indent when an indentation increase does not follow a construct that starts a suite. Another reports an expected indented block after a colon-ended header when the required suite is absent. TabError is a more specific indentation-related error for a problematic mixture of tabs and spaces. These errors occur while Python is processing source structure, not because a later computation produced an unwanted result.

When you read one, first compare the line with the surrounding structural pattern: does a header end in a colon; does its following suite align consistently; did a closing parenthesis or bracket go missing above? This is source reading, not a full debugging workflow. Fix the program's structure, then let Python parse it again. Keeping code short, using one statement per line, and preferring implicit continuation reduce opportunities for structural ambiguity.

Eli, the EliExplains learning guide

Eli explains

The same idea, in plain words

Explain it like I’m 10

Python reads a program a little like a music reader reads a score. The words and symbols matter, but so does where each line begins. Most spaces merely keep words from running together. The spaces at the left edge are different: they show which instructions belong in the same group. A line ending in a colon is like a label that says, “the next indented lines are together.” A # begins a note for people; Python skips that note when it follows the program's rules.

Long lines do not need to become messy. Put an expression inside parentheses, then lay its pieces out over several lines. Python knows the closing parenthesis means the thought is still continuing. If the punctuation or indentation cannot form a valid score, Python stops before playing anything and reports a syntax-related error.

Picture it like this

Imagine a stage script. The words are the dialogue, indentation shows which directions belong under the same scene cue, and a comment is a pencil note for the crew that is not spoken aloud.

Where the picture stops working

A stage script is interpreted by people who may infer intent; Python follows its grammar mechanically. Also, indentation has formal token-level consequences in Python, whereas a printed script's indentation is usually only visual design.

Worked example

Suppose you want a readable label built across lines. Write and run this exact code:

label = (
    "one logical line, "
    "four physical lines"
)
print(label)  # show the completed text

It prints one logical line, four physical lines. The opening parenthesis permits implicit continuation, so the four physical lines that make the expression are treated as one logical line. The aligned string literals are adjacent and therefore form one string value. The print line begins at the left margin, so it is a separate statement rather than part of any indented suite. Finally, the text after # is a comment; it helps a reader but does not change the output.

Key takeaway

Python source is structured before it runs: tokens form logical lines, and leading indentation groups suites. Use comments for readers, parentheses for multi-line expressions, and consistent indentation so the structure is both valid and visible.

Quick check

3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.

Question 1 of 3foundational

Which character begins a Python comment when it appears outside a string literal?

Choose an answer, then check it.
Question 2 of 3intermediate

What is the clearest standard way to split a long Python expression across lines?

Choose an answer, then check it.
Question 3 of 3foundational

In this shape, what is the indented group after the colon called? if ready: followed by indented statements

Choose an answer, then check it.
Practice all 5

Keep learning

Ready to build on this? Continue to the next lesson.

Practice this lesson
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related

You’ll learn to

  • Distinguish physical lines, logical lines, tokens, statements, and suites.
  • Explain why leading indentation is structural in Python while most internal whitespace is not.
  • Apply implicit line joining to format a multi-line expression clearly.
  • Distinguish a SyntaxError from indentation-related errors at a basic level.
  • Recognize comments and continuations that are valid Python source.

Common mistakes

  • Treating every space as either meaningless or structural.

    Whitespace between most tokens is only a separator, but leading whitespace on a logical line determines grouping. Check its position before deciding its role.

  • Using a backslash continuation when parentheses would show the grouping.

    Prefer implicit continuation inside parentheses, brackets, or braces. It is less fragile and permits comments on continuation lines.

  • Forgetting the colon before an indented suite.

    A compound-statement clause header ends in a colon. The following indented statements form its suite.

  • Mixing tabs and spaces because they look aligned.

    Use one consistent indentation method. A mix whose meaning depends on tab width raises TabError.

  • Assuming a hash always begins a comment.

    A hash starts a comment only when it is outside a string literal; inside quoted text it is a character in that text.

Easily confused

Physical line vs. Logical line

A physical line ends at a source-file line ending; a logical line may combine physical lines under continuation rules and serves as a syntax boundary.

Explicit line joining vs. Implicit line joining

Explicit joining uses a line-final backslash; implicit joining occurs inside parentheses, brackets, or braces and is usually clearer.

SyntaxError vs. Indentation-related error

SyntaxError broadly reports source that cannot satisfy Python grammar; indentation-related errors specifically concern source structure involving indentation, with TabError covering a problematic tab/space mix.

Key vocabulary

token
A categorized unit of source text produced during lexical analysis, such as a name, literal, operator, or delimiter.
physical line
Characters in source code ending at a line-ending sequence.
logical line
A line as recognized by Python's syntax; it may be formed from one or more physical lines.
statement
A complete grammatical instruction in Python source.
expression
Source text that Python evaluates to obtain a value and that can be part of a statement.
suite
A group of statements controlled by a clause of a compound statement.
indentation
Leading whitespace on a logical line that Python uses to determine statement grouping.
implicit line joining
Continuing an expression across physical lines inside parentheses, brackets, or braces without a backslash.

Sources & references

  1. The Python Language Reference — 2.7 Operators and delimiters — Python Software Foundation
  2. 8. Compound statements - The Python Language Reference (while, for, break, continue) — Python Software Foundation
  3. The Python Language Reference — Simple statements (assignment statements) — Python Software Foundation

EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.

Researched 2026-08-19

Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.