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, not braces, to define suites after compound statements, so block alignment changes program meaning.
  • PCEP-30-02 code-reading items often turn on separating cosmetic spacing from indentation that changes control flow.
Last updated: June 2026

Why this section matters on the PCEP

The PCEP Certified Entry-Level Python Programmer exam (current edition PCEP-30-02, retiring 31 August 2026 and being replaced by PCEP-30-03) is 30 questions in 40 minutes with a 70% passing score and a $69 starting fee. Section 1, "Computer Programming and Python Fundamentals," carries roughly 18% of the score (about 5-6 questions). Almost every fundamentals item rewards precise vocabulary, so begin with the three language layers.

The three language layers

Lexis (the lexical layer) is where Python recognizes individual words and symbols, called tokens. It decides that while is a keyword, count is an identifier, 12 is an integer literal, + is an operator, and : is a delimiter. Lexis never asks whether a program is useful, only what the pieces are.

Syntax is the grammar for arranging tokens. Python syntax requires an if header to have a condition and a trailing colon, an assignment to have a legal target on the left, and a call to have matching parentheses. Most syntax mistakes are caught before any line runs, raising SyntaxError.

Semantics is the meaning of code that already passed the grammar check. A line can be syntactically perfect yet fail at runtime. result = 10 / 0 is legal syntax, but its meaning is division by zero, so it raises ZeroDivisionError only when executed.

LayerQuestion it answersExample exam clueTypical failure
LexisWhat tokens are present?Is for a keyword or a name?Illegal character
SyntaxIs the arrangement legal?Is the colon missing after if?SyntaxError
SemanticsWhat happens when it runs?Does a name exist yet?NameError, TypeError, ZeroDivisionError

Indentation is structure, not decoration

Python does not use braces for most blocks; it uses indentation. A block, formally a suite, follows compound-statement headers such as if, else, while, for, and def. The indented statements belong to the header above them until the indentation level decreases back.

score = 72
if score >= 70:
    label = 'pass'
    print(label)
else:
    label = 'retry'
    print(label)
print('done')

The two statements under if form one suite; the two under else form another. The final print('done') is outside both branches because it returns to the original left margin. The official style is 4 spaces per level (PEP 8), but Python only requires that a suite be indented consistently relative to its header.

Spacing that matters versus spacing that does not

Spaces around operators improve readability but do not change meaning: x=3+4 and x = 3 + 4 both bind 7 to x. Indentation at the start of a logical line is different, because moving a statement left or right can move it into or out of a block, changing when, or whether, it runs.

Mixing tabs and spaces is the classic trap. Python may raise IndentationError (an indented block was expected but not found, or an unexpected indent appears) or TabError (inconsistent tab/space mixing). The exam usually shows clean, visible indentation, so you must trace alignment literally rather than guessing intent.

  • IndentationError: expected an indented block - a header like if x: had no indented suite.
  • IndentationError: unexpected indent - a line is indented with no header above it.
  • TabError - tabs and spaces conflict in one block.

Logical lines, physical lines, and statement separators

A physical line is one line of source text; a logical line is one complete statement. Usually they match, but a long expression inside parentheses, brackets, or braces may span several physical lines without any continuation marker, because Python knows the bracket is still open. Outside brackets, a trailing backslash \ explicitly joins one physical line to the next. You may also place several short statements on one physical line by separating them with semicolons, as in a = 1; b = 2; print(a + b), though PEP 8 discourages it.

The exam can test whether you recognize that a bracketed expression continuing onto the next line is still a single statement, not two.

How to read exam snippets

When a question shows a short program, first mark every header ending in a colon. Then draw a mental vertical guide for each indentation level. Finally read top to bottom, asking for each statement whether it belongs to the current block, a nested block, or the outer program. This discipline prevents two recurring mistakes: treating a line as conditional when it actually sits outside the if suite, and attaching an else to the wrong header in nested code.

A worked trap: given two equally indented branches and a print at the margin, the print runs unconditionally. Candidates who assume the last print belongs to the else choose the wrong output. Always confirm the indentation level of each line against the headers above it rather than reading the code like prose. Remember the summary: syntax lets code start; semantics tells what it does; indentation tells which statements each header controls, and on PCEP a single shifted line is frequently the entire difference between two answer options.

Test Your Knowledge

In Python terminology, what does the lexical (lexis) layer describe?

A
B
C
D
Test Your Knowledge

Which change can alter the meaning of a Python program even though every word stays the same?

A
B
C
D
Test Your Knowledge

Which line is syntactically valid Python but has a runtime semantic problem?

A
B
C
D