Syntax, Indentation & Comments

Python's syntax is famously clean: one statement per line, no semicolons, and no curly braces. Instead, indentation defines which lines belong together. This lesson covers those rules, comments, and how Python reports syntax mistakes.

Statements and lines

PY statements.py
price = 20
quantity = 3
total = price * quantity
print(total)
Output
60

Each line is one statement, run from top to bottom. A line ends the statement — no semicolon needed. (You can put two statements on one line separated by ;, but it's considered bad style.)

Indentation defines blocks

A line ending in a colon : starts a block. Every line in the block must be indented by the same amount — the standard is 4 spaces:

PY indentation.py
temperature = 31

if temperature > 30:
    print("It's hot.")
    print("Drink some water.")
print("Have a nice day.")
Output
It's hot.
Drink some water.
Have a nice day.

The two indented lines belong to the if; the last line is back at the left margin, so it runs regardless. In other languages, braces mark blocks and indentation is just for humans. In Python, indentation is the structure.

Indentation errors

PY bad_indent.py
if True:
print("not indented")
Output
  File "main.py", line 2
    print("not indented")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 1

Python notices the mistake before running anything and tells you the exact line. Mixing tabs and spaces causes similar errors — configure your editor to insert 4 spaces when you press Tab.

Comments

PY comments.py
# This whole line is a comment.

speed = 50  # A comment can follow code on the same line.

# print("This line is switched off")
print(speed)
Output
50

Everything after # on a line is ignored. Python has no special multi-line comment syntax; use several # lines. Good comments explain why, not what.

Docstrings

A string placed as the first statement of a module, function or class is a docstring. Tools and editors show it as documentation, and help() displays it:

PY docstring.py
def area(width, height):
    """Return the area of a rectangle."""
    return width * height

print(area.__doc__)
print(area(3, 4))
Output
Return the area of a rectangle.
12

Docstrings use triple quotes so they can span several lines.

Long lines

Inside brackets — (), [], {} — you can break a line anywhere, and Python joins it back together:

PY long_lines.py
total = (
    100
    + 250
    + 75
)

colors = [
    "red",
    "green",
    "blue",
]

print(total, colors)
Output
425 ['red', 'green', 'blue']

This is the preferred way to wrap long lines. A backslash \ at the end of a line also continues it, but it's fragile (a stray space after it breaks the code).

Case sensitivity and names

Python is case-sensitive: total, Total and TOTAL are different names. Keywords like if, for, True and None must be spelled exactly:

PY case.py
score = 10
print(Score)
Output
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    print(Score)
          ^^^^^
NameError: name 'Score' is not defined. Did you mean: 'score'?

Notice the helpful suggestion at the end. Recent Python versions are very good at pointing you to the fix.

Reading a traceback

When an error happens while the program runs, Python prints a traceback: the chain of calls that led to the error, ending with the error type and message. Read it from the bottom up — the last line says what went wrong, and the lines above say where.

Try it yourself

Run this, then delete the spaces before the second print and run it again to see the error:

Try it yourself
Console output