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
price = 20
quantity = 3
total = price * quantity
print(total)
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:
temperature = 31 if temperature > 30: print("It's hot.") print("Drink some water.") print("Have a nice day.")
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
if True: print("not indented")
File "main.py", line 2
print("not indented")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 1Python 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
# 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)
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:
def area(width, height): """Return the area of a rectangle.""" return width * height print(area.__doc__) print(area(3, 4))
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:
total = (
100
+ 250
+ 75
)
colors = [
"red",
"green",
"blue",
]
print(total, colors)
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:
score = 10
print(Score)
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: