Python Syntax
Learn the core rules of Python syntax — how indentation replaces curly braces, how colons begin code blocks, and how to avoid the dreaded IndentationError.
Introduction
Every programming language has syntax — a set of rules that define how code must be written to be valid. Python's syntax is famous for one specific choice: it uses indentation (whitespace) itself to define the structure of a program, rather than symbols like curly braces.
This design decision is not just cosmetic — it is core to how Python works, and understanding it now will prevent a lot of confusion later, especially once you start writing conditions, loops, and functions.
- Why Python has no curly braces or semicolons.
- How indentation defines a block of code.
- What role the colon (:) plays in Python syntax.
- Why consistent indentation matters, and what an IndentationError looks like.
- The rules around writing one statement per line.
No Curly Braces, No Semicolons
Many programming languages, like Java, C, and JavaScript, use curly braces { } to group statements into blocks, and semicolons ; to end each statement. Python uses neither of these by requirement.
Instead, Python relies on two things: indentation (the whitespace at the start of a line) to show which statements belong together, and a colon (:) to signal that an indented block is about to begin.
Other Languages (e.g. JavaScript)
- if (age >= 18) {
- console.log("Adult");
- }
Python
- if age >= 18:
- print("Adult")
Indentation Defines Blocks
In Python, a block is a group of statements that belong together, such as the body of an if statement, a loop, or a function. Python determines which lines belong to a block purely by how far they are indented from the left margin.
All statements in the same block must be indented by the same amount. The moment the indentation decreases back to the previous level, Python understands that the block has ended.
Whitespace Matters
The number of spaces before a line of code is not decoration — it is part of the code's meaning.
Groups Statements
Indentation shows Python which lines belong inside a block, like an if or a loop.
4 Spaces (PEP 8)
The Python style guide, PEP 8, recommends exactly 4 spaces per indentation level.
Example: Indentation in Action
Here, the two print() calls inside the if block are indented by 4 spaces, showing they belong to that condition. The final print() has no indentation, so it runs regardless of the condition.
age = 20
if age >= 18:
print("You are an adult.")
print("You can vote.")
print("This always runs.")You are an adult.
You can vote.
This always runs.The Colon (:)
A colon at the end of a line is Python's signal that an indented block follows on the next lines. You will see a colon used with if, for, while, def, and class statements — all covered in detail in upcoming lessons.
if condition:
Begins a conditional block that runs only when the condition is true.
for item in items:
Begins a loop block that repeats for each item.
def function_name():
Begins a function's block of instructions.
class ClassName:
Begins a class definition's block.
Forgetting the colon is one of the most common early syntax mistakes, and Python will immediately raise a SyntaxError if it is missing.
Why Consistent Indentation Matters
Because indentation carries actual meaning in Python, mixing tabs and spaces, or using a different number of spaces within the same block, will cause your program to fail. PEP 8, Python's official style guide, recommends using 4 spaces per indentation level and avoiding tabs entirely.
Most modern editors, including VS Code with the Python extension, automatically insert 4 spaces when you press Tab inside a Python file, which helps you stay consistent without thinking about it.
- Use 4 spaces per indentation level.
- Never mix tabs and spaces in the same file.
- Configure your editor to insert spaces when you press Tab.
Example: IndentationError
When indentation is inconsistent within the same block, Python raises an IndentationError instead of guessing what you meant.
age = 20
if age >= 18:
print("You are an adult.")
print("You can vote.")IndentationError: unindent does not match any outer indentation levelThe second print() line uses 2 spaces while the first uses 4, so Python cannot tell whether it belongs to the same block, a new block, or the outer level — and refuses to guess.
Case Sensitivity
Python is case-sensitive, meaning it treats uppercase and lowercase letters as completely different. Variable names, function names, and keywords must be typed with exactly matching case every time.
name = "Alex"
print(Name)NameError: name 'Name' is not definedEven though name and Name look similar, Python treats them as entirely different identifiers — this is why name and Name would be two separate variables.
One Statement Per Line
Python's convention, and PEP 8's recommendation, is to write exactly one statement per line, with no semicolon needed at the end. This is the cleanest and most readable style, and the one you should default to.
Python does technically allow putting multiple statements on one line using a semicolon (;) as a separator, but this is discouraged because it hurts readability. For a single logical line that is too long to read comfortably, Python allows a backslash (\) at the end of a line to continue onto the next line.
Example: Semicolons and Line Continuation
The semicolon-separated version below works but is not recommended style. The line-continuation example shows the preferred way to split a long expression.
a = 5; b = 10; print(a + b)15total = 10 + 20 + 30 + \
40 + 50
print(total)150The backslash tells Python that the statement continues on the next line, which is useful for keeping very long expressions readable.
Common Mistakes
- Mixing tabs and spaces for indentation within the same file.
- Forgetting the colon (:) after if, for, while, or def.
- Using inconsistent indentation levels within the same block.
- Assuming Python ignores letter case like some other languages might.
- Overusing semicolons to cram multiple statements onto one line.
Best Practices
- Always indent with exactly 4 spaces per level, per PEP 8.
- Configure your editor to insert spaces, not tab characters.
- Write one statement per line for readability.
- Use a backslash or parentheses to break up long lines cleanly.
- Double-check that every if, for, while, def, and class line ends with a colon.
Frequently Asked Questions
Why does Python use indentation instead of curly braces?
Guido van Rossum designed Python so that visual structure (indentation) and actual code structure always match, which makes code more readable and reduces certain classes of bugs.
How many spaces should I indent with?
PEP 8, Python's official style guide, recommends exactly 4 spaces per indentation level.
Can I use tabs instead of spaces?
Python allows tabs, but mixing tabs and spaces in the same file causes errors, and the community strongly prefers spaces. Most editors can be configured to insert spaces automatically.
What causes an IndentationError?
It happens when indentation within a block is inconsistent, or when an indented block is expected (after a colon) but not provided.
Is it ever okay to use semicolons in Python?
Semicolons work but are discouraged by PEP 8, since separating statements onto their own lines is more readable.
Key Takeaways
- Python uses indentation, not curly braces, to define code blocks.
- A colon (:) signals that an indented block follows.
- PEP 8 recommends 4 spaces per indentation level, never mixed with tabs.
- Python is case-sensitive — name and Name are different identifiers.
- Write one statement per line; use \ for line continuation when needed.
Summary
Python's syntax rules center on one core idea: whitespace is meaningful. Indentation defines blocks, colons introduce them, and consistency is not optional — it is required for your code to run at all.
In this lesson, you learned how indentation and colons structure Python code, why consistent indentation is required, and the rules around statements and line length. Next, you will learn how to use comments to document and explain your code.
- You understand how indentation defines blocks in Python.
- You know when and why to use a colon.
- You can recognize and fix an IndentationError.
- You are ready to learn how to write comments.