Search code examples
pythongrammarterminologysemantics

Is indentation semantically meaningful or syntactically meaningful in Python


I came across the sentence,

In Python, indentation is semantically meaningful.

I'm not sure I understand what "semantically meaningful" means here.

Also, since indentations are used to delimit the if and else blocks of coniditional expressions in Python, wouldn't they be considered to be part of the language grammar and therefore "syntactically meaningful"? (I cannot find mention of them in the docs for conditional expressions.)


Solution

  • I think they should have said "semantically meaningful", but the distinction is somewhat fuzzy. TechDifferences says:

    The syntax of a programming language is a collection of rules to specify the structure or form of code whereas semantics refers to the interpretation of the code or the associated meaning of the symbols, characters or any part of a program.

    Since indentation determines things like whether a line is part of a function or loop, and that impacts things like variable scope, it could be considered to affect the "associated meaning" of the symbols.