StudyToCert

All certifications / PCEP / Lessons

PCEP – Certified Entry-Level Python Programmer PCEP-30-02 · Domain 1: Computer programming and Python fundamentals

Python logic and structure: keywords, instructions, indentation and comments

▶ Watch the overview video

Last reviewed September 30, 2026 · Leer en español

A Python program is a sequence of instructions (statements) that the interpreter executes from top to bottom. Normally each statement sits on its own line, and the end of the line ends the statement; there is no semicolon at the end as in some other languages. You can put several short statements on one line separated by semicolons, and you can continue a long statement across lines inside parentheses, brackets or braces, or with a backslash at the end of a line, but the plain one-statement-per-line style is what you should write and what the exam usually shows. For example, a long sum written inside parentheses may continue onto the next line, because Python keeps reading until the bracket closes. Remember too that statements run strictly in order: a line cannot use a name that is only assigned further down the file.

Keywords are reserved words with a fixed meaning in the language, such as if, else, elif, while, for, in, def, return, pass, break, continue, import, global, try, except, and, or, not, is, None, True and False. You cannot use a keyword as a variable or function name: for = 3 is a syntax error. Keywords are case sensitive: True is a keyword but true is an ordinary, and usually undefined, name. You can see the full list with import keyword followed by print(keyword.kwlist).

Indentation is part of Python's syntax, not just style. A compound statement such as if, while, for, def or try ends its header line with a colon, and the lines that belong to it (its block, also called a suite) must be indented more than the header. The block ends when indentation returns to the earlier level. All lines in one block must use exactly the same indentation. PEP 8 (Python Enhancement Proposal 8, the official style guide) recommends four spaces per level. Mixing tabs and spaces inconsistently leads to TabError, and a wrong level leads to IndentationError; both are kinds of SyntaxError.

x = 7
if x > 5:
    print("big")      # inside the if block
    print("still in") # same block, same indent
print("always runs")  # back at the outer level
# output: big / still in / always runs

Comments start with # and run to the end of the line. The interpreter ignores them entirely, so they are for human readers: explain why the code does something, not what each obvious line does. A # inside a string literal is just a character, not a comment. Python has no special multi-line comment syntax; you put # on each line or, by convention, use a triple-quoted string, which is really a string literal that is evaluated and discarded. When a triple-quoted string is the first statement in a function or module it becomes its docstring, a piece of built-in documentation.

Consider a worked example. A learner writes a loop that should print a running total. The header for n in range(3): is followed by two lines, the first indented four spaces and the second three. Python refuses to run the file and reports IndentationError: unindent does not match any outer indentation level. After fixing that, the learner moves print(total) back to the left margin; now it runs once after the loop instead of three times inside it. Both versions contain identical words, yet indentation alone changes whether the program runs and what it prints.

Common mistakes: forgetting the colon at the end of a header, which is a SyntaxError; indenting a line that has no header above it, which gives IndentationError: unexpected indent; ending a header with a colon and indenting nothing below it, which gives IndentationError: expected an indented block (the reason the pass keyword exists); writing true or none in lower case; and believing that a comment can serve as a block body. It cannot, because comments are discarded before the grammar is checked.

Exam questions in this area usually show a short snippet and ask whether it runs, what it prints, or which error appears. Clue words: "reserved word" or "cannot be used as a name" means keyword; "belongs to the block" means indentation level; "ignored by the interpreter" means comment; "unexpected indent" and "expected an indented block" name the two classic indentation errors. When a line's indentation is the only difference between answer choices, trace the columns carefully, because that is exactly what is being tested.

Key terms

Statement (instruction)
A single command the interpreter executes, normally one per line.
Keyword
A reserved word with fixed meaning, such as if, for or def, that cannot be used as an identifier.
Block (suite)
The group of indented statements belonging to a header line that ends with a colon.
Comment
Text after # to the end of the line, ignored by the interpreter.
IndentationError
The SyntaxError subclass raised when indentation is missing, unexpected or inconsistent.
Docstring
A string literal placed first in a module or function, kept as its documentation.
Real-world example

A learner writes a loop whose second body line is indented with three spaces instead of four. Python reports an IndentationError (unindent does not match any outer indentation level) before running anything. Re-indenting both lines to four spaces makes the program run, and moving the final print back to the margin makes it run once after the loop instead of on every pass.

Exam tip: True, False and None are capitalised keywords; lowercase true is just an undefined name. A # inside quotes is not a comment, and a comment alone can never be a block body.

Check yourself

What happens if you write if x > 3: and the next line is not indented?

Python raises IndentationError: expected an indented block, because a compound statement header must be followed by an indented block.

Can you name a variable pass?

No. pass is a keyword, so pass = 1 is a SyntaxError.

What does print("a # b") output?

a # b. The # is part of the string literal, not the start of a comment.

How many spaces per indentation level does PEP 8 recommend, and is it enforced?

Four spaces. The interpreter only requires consistency within a block; four spaces is the style convention.

Study PCEP for free
A week-by-week plan with every lesson, quizzes, checkpoint tests, a practice exam and hands-on labs.
Open the PCEP study plan