Menu
Try in Playground

Python Syntax: Indentation, Lines, Colons, and the Rules That Matter

The minimal set of syntax rules Python cares about — indentation, line breaks, colons, and case sensitivity — explained without jargon.

The Smallest Possible Rulebook

Python has a reputation for being readable, and most of that comes down to how few syntax rules the language actually has. You can fit the ones that matter on a single page:

  • One statement per line.
  • Indentation defines structure — no braces, no end keywords.
  • Case matters. name and Name are different.
  • : opens a block; the next lines indented under it are the block's body.
  • # starts a comment.

That's essentially it. Everything else is variables, functions, and standard library habits, which you'll pick up as you go. Let's walk through those five rules with enough context that they stop feeling like rules and start feeling like the natural shape of the language.

One Statement Per Line

A statement is a complete instruction — "print this," "add these," "assign that." In Python, each statement goes on its own line:

main.py
Output
Click Run to see the output here.

Three statements, three lines. You don't need a semicolon at the end, and most Python code doesn't use them. (You can put two statements on one line with a semicolon, but it's considered poor style. Don't.)

If a line genuinely needs to be long, you can break it at a natural point inside parentheses, brackets, or braces — Python won't complain:

main.py
Output
Click Run to see the output here.

Or, less commonly, end a line with a backslash to continue it. Prefer the parenthesis trick when you can — it's what most codebases use.

Indentation Is Structural

Here's the part that trips up newcomers. In Python, the whitespace at the start of a line is meaningful. It tells Python which lines belong together.

Look at a simple if:

main.py
Output
Click Run to see the output here.

The two lines after if hour < 12: are indented by four spaces. Python reads that indentation as "these two lines are the body of the if." The last print is back at the left margin, so Python sees it as outside the if.

If you remove the indentation, the if breaks. If you over-indent by one extra space, the if breaks. Python is strict here because the indentation is the structure — there's no { or end to back it up.

A few rules that follow from this:

  1. Pick four spaces per indent level and stick with it. That's the PEP 8 convention and what every editor defaults to for .py files.
  2. Don't mix tabs and spaces. Python 3 will reject a file that uses both. Modern editors convert tabs to spaces automatically when you save; make sure yours is set up that way.
  3. Indentation errors will happen. When they do, the error message tells you the line number. Go check that line's leading whitespace is the same as its siblings.

The Colon Opens a Block

Every construct that creates a nested block — if, else, for, while, def, class, with, try — ends its header line with a colon. The next lines, indented, form the block body:

main.py
Output
Click Run to see the output here.

Two things to notice: the colon at the end of the header, and the four-space indent on the body lines. If you forget the colon, Python will tell you something like SyntaxError: expected ':'. That error is unambiguous — fix the missing punctuation and you're good.

Case Sensitivity

Python treats Name, name, and NAME as three entirely separate things. This applies to:

  • Your own variables and functions.
  • Built-ins like print, len, True, False, None.
  • Module and attribute names.

This usually only bites you when you accidentally capitalise something. True is the boolean true value; true is an undefined variable that raises NameError. Print(...) isn't the print function — it's a typo.

If an error says "name 'X' is not defined" and you swear you defined it, check the capitalisation first. It's wrong nine times out of ten.

Comments With #

Anything after # on a line is a comment — Python ignores it entirely. Comments are for notes to future readers (including future you):

main.py
Output
Click Run to see the output here.

Python has no multi-line comment syntax. If you need several lines, use multiple # lines. (You'll sometimes see triple-quoted strings used as pseudo-comments — that's a separate thing called a docstring, and we'll cover it when we talk about functions.)

Reserved Words

A few words are off-limits as variable names because Python uses them itself: if, else, for, while, def, class, return, import, from, as, pass, break, continue, True, False, None, and, or, not, in, is, lambda, try, except, finally, raise, with, yield, global, nonlocal, async, await. You don't need to memorize this list. If you try to use one, Python will immediately complain.

You Know Enough to Read Any Python File

Every rule you just read applies to every Python file — a two-line script and a hundred-thousand-line project alike. The rest of the language is built on top of these five ideas. If a file looks confusing, the fix is almost always to pay attention to the indentation first. The indentation tells you what the code is doing, even before you read what the lines say.

Next up: comments, in a little more depth — plus docstrings, which are how Python handles documentation for functions and modules.

Frequently Asked Questions

Why does Python care about indentation?

Python uses indentation to figure out which lines belong together. Where other languages wrap groups of code in braces { }, Python uses consistent leading whitespace instead. It's less visual noise, but it means you can't be sloppy with spacing.

How many spaces should I indent in Python?

Four spaces per level is the community standard (PEP 8). Most editors insert four spaces when you press Tab in a Python file. The exact number matters less than being consistent — mixing two-space and four-space indents in the same block is a common error.

Is Python case-sensitive?

Yes. Name, name, and NAME are three different variables. Built-in names like print and True must be spelled exactly as shown — True works, true raises an error.

Learn to code with Coddy

GET STARTED