Syntax and Indentation
Learn why indentation is part of Python syntax.
Python uses indentation to show which statements belong together, so spacing is not merely cosmetic.
Start with the idea
A colon usually introduces a block, and the indented lines under it belong to that block. Consistent indentation makes the program structure visible.
Small working example
Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.
temperature = 28
if temperature > 25:
print("Warm day")
print("Done") A colon usually introduces a block, and the indented lines under it belong to that block. Consistent indentation makes the program structure visible.
- Indentation defines blocks.
- Colons often start blocks.
- Readable formatting and correct structure go together in Python.
Warm day
DoneThe example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.
Common beginner mistake
Mixing indentation levels so code appears visually related but Python treats it differently.
Use consistent indentation, normally four spaces, and let your editor help.
Quick recap
- Indentation defines blocks.
- Colons often start blocks.
- Readable formatting and correct structure go together in Python.
Try it yourself
These are deliberately small. If you can complete them without copying the example, you are ready to continue.
- Add an else block.
- Move print("Done") inside the if block and compare the behavior.