Comments & Indentation
Python uses indentation (spaces at the start of a line) to group code into blocks, so whitespace is part of the language itself. Comments let you leave notes for humans that Python ignores. Mastering both early will save you many confusing errors.
Comments
A comment starts with the # symbol. Everything from # to the end of that line is ignored by Python. Use comments to explain why code does something, record decisions and leave reminders.
# Calculate the cost of a school trip
students = 45
fee_per_student = 1500 # KES, agreed at the parents' meeting
print("Total collected:", students * fee_per_student)
A comment can sit on its own line or at the end of a line of code (an inline comment). PEP 8, Python's official style guide, recommends at least two spaces before an inline # and one space after it.
Comments that span several lines
Python has no special multi-line comment symbol. Simply start each line with #:
# This study tracker was written for Form 4 revision. # It records hours studied each day and prints a weekly total. # Author: Achieng, March 2026
In VS Code, select several lines and press Ctrl+/ (Cmd+/ on a Mac) to comment or uncomment them all at once.
Commenting out code
Putting # in front of a line of code temporarily switches it off. This is handy for testing:
print("This line runs")
# print("This line is switched off")
print("So does this one")
Docstrings
A string in triple quotes ("""...""") placed at the very top of a file, or as the first line inside a function, is called a docstring. It documents what that file or function does, and tools such as VS Code show it when you hover over a name. You'll write docstrings properly on the Defining Functions page.
"""Budget helper: adds up a week's spending in KES."""
spending = 350 + 120 + 800
print("Week total:", spending)
Good comments explain the reason, not the obvious. x = x + 1 # add 1 to x wastes the reader's time. retries = retries + 1 # the M-Pesa API sometimes times out tells them something useful.
Indentation
Many languages use curly braces { } to mark which lines belong together. Python uses indentation instead. A line that ends with a colon : starts a block, and every line in that block must be indented by the same amount. The standard is four spaces.
The example below uses an if statement, which runs a block only when a condition is true. You'll study it fully on the If…Else page; for now, focus on the indentation.
marks = 72
if marks >= 50:
print("Pass") # inside the block
print("Well done!") # inside the block
print("Results printed") # outside: always runs
The two indented lines belong to the if. The last line is back at the left edge, so it is not part of the block and runs no matter what. Change marks to 40 and the program prints only Results printed.
Indentation errors
Because indentation has meaning, getting it wrong stops your program before it runs.
marks = 72
if marks >= 50:
print("Pass")
print("Start")
print("Oops")
(Only the final line of each error report is shown.) The first program is missing the indentation the if requires. The second indents a line for no reason, since there is no colon above it starting a block.
Mixing tabs and spaces. A tab and four spaces can look identical on screen, but Python treats them differently and may raise a TabError. Set your editor to insert spaces when you press Tab. VS Code does this by default for Python files and shows "Spaces: 4" in the status bar at the bottom.
Nested blocks
A block can contain another block. Each level adds four more spaces:
age = 20
has_id = True
if age >= 18:
print("Old enough")
if has_id:
print("ID checked: you may enter")
print("Next person, please")
if age >= 18:; the yellow strip inside it is the block of if has_id:. The last line is outside both.Other whitespace rules
- Blank lines are ignored. Use them to separate sections of code, like paragraphs in an essay.
- Spaces inside a line mostly don't matter:
2+3and2 + 3are the same. PEP 8 recommends one space around operators such as=and+for readability. - Only the spaces at the start of a line carry meaning.
Key points
#starts a comment; Python ignores everything after it on that line.- Comment the why, not the obvious; use docstrings to describe files and functions.
- A colon
:starts a block, and the block's lines must be indented equally, usually by four spaces. - Wrong indentation causes
IndentationErrororTabError; never mix tabs and spaces.
Tip: use the ← and → keys to move between pages.
