1.1 Lexis, Syntax, Semantics, and Indentation
Key Takeaways
- Lexis is the token layer of Python: keywords, identifiers, literals, operators, delimiters, and whitespace.
- Syntax is the grammar that decides whether tokens form a legal Python statement or expression.
- Semantics is the meaning Python gives legal code when it runs, including values, side effects, and errors.
- Python uses indentation to define suites after compound statements, so block alignment is part of program meaning.
- PCEP code-reading questions often turn on separating cosmetic spacing from indentation that changes control flow.
The three language layers
PCEP fundamentals start with precise vocabulary. Lexis is the low-level layer where Python recognizes words and symbols. It decides that while is a keyword, count is an identifier, 12 is an integer literal, + is an operator, and : is a delimiter. Lexis does not decide whether a whole program is useful; it only identifies the pieces.
Syntax is the grammar for arranging those pieces. Python syntax says an if header needs a condition and a colon, an assignment needs a valid target on the left, and a function call needs matching parentheses. Syntax mistakes are usually found before the program can run.
Semantics is the meaning of code that passes the grammar check. A line can be syntactically legal and still fail at runtime or produce a result the reader did not expect. result = 10 / 0 is legal syntax, but its meaning is division by zero, so running it raises ZeroDivisionError.
| Layer | Question it answers | Example exam clue |
|---|---|---|
| Lexis | What tokens are present? | Is for a keyword or a name? |
| Syntax | Is the arrangement legal? | Is the colon missing after if? |
| Semantics | What happens when it runs? | Does a name exist at this point? |
Indentation is structure
Python does not use braces to define most blocks. It uses indentation. A block, also called a suite, follows headers such as if, else, while, for, and def. The indented statements belong to the header above them until the indentation level decreases.
score = 72
if score >= 70:
label = 'pass'
print(label)
else:
label = 'retry'
print(label)
print('done')
In this snippet, the two statements under if form one suite, and the two statements under else form another. The final print('done') is not inside either branch because it returns to the left margin used before the if statement.
Spacing that matters and spacing that does not
Spaces around operators usually improve readability but do not change meaning. x=3+4 and x = 3 + 4 assign the same value. Indentation at the beginning of a logical line is different. Moving a statement left or right can move it into or out of a block.
Tabs and spaces can also cause trouble when mixed inconsistently. PCEP normally focuses on visible indentation, but real Python may raise IndentationError or TabError when block structure is unclear.
How to read exam snippets
When a PCEP question shows a short program, first mark the headers ending in colons. Then draw a vertical guide for each indentation level. Finally, read from top to bottom and ask whether each statement belongs to the current block, a nested block, or the outer program.
A disciplined reading order prevents two common mistakes:
- Treating a line as conditional when it is actually outside the
ifsuite. - Treating an
elseas connected to the wrong header in nested code.
Syntax lets code start. Semantics tells what it does. Indentation tells which statements are controlled by which headers.
In Python terminology, what does lexis describe?
Which change can alter the meaning of a Python program even when the words stay the same?
Which line is syntactically valid Python but has a runtime semantic problem?