One of the first things that stands out about Python compared to languages like Java or C is that it doesn't use curly braces { } to mark the start and end of a block of code. Instead, Python uses indentation — whitespace at the start of a line — to decide what belongs inside a function, loop, or conditional. Getting this right is essential, because in Python, indentation isn't just a style choice; it's part of the syntax itself.

Why Python Uses Indentation

In most languages, braces define a block, and indentation is just there to make it readable for humans — the compiler doesn't care how it's indented. Python's creator decided to make indentation mandatory instead, which forces all Python code to be visually consistent and removes an entire category of "which closing brace does this belong to" bugs.

age = 20

if age >= 18:
    print("You are an adult.")
    print("You can vote.")
else:
    print("You are a minor.")

Output:

You are an adult.
You can vote.

Both lines under the if are indented by the same amount (4 spaces, by convention), telling Python they both belong to the if block. The line starting with else lines back up with if, marking the start of a new block.

How Many Spaces? Tabs or Spaces?

The official Python style guide, PEP 8, recommends 4 spaces per indentation level, and that is what the vast majority of Python code in the wild uses. You technically can use a different consistent number of spaces, or even tabs, but mixing tabs and spaces in the same file causes Python to throw a TabError — most code editors are configured to insert spaces automatically when you press Tab, which avoids this entirely.

Common Indentation Mistakes

Because indentation is meaningful, a misplaced space causes a real error rather than just an ugly line of code. The most common one beginners see is IndentationError:

if True:
print("This line should be indented")

Output:

IndentationError: expected an indented block after 'if' statement on line 1

The fix is simple — every line that belongs inside the if needs at least one consistent indentation level:

if True:
    print("This line should be indented")

Another common mistake is inconsistent indentation within the same block — for example, indenting one line with 4 spaces and the next with 5. Python will raise an IndentationError: unindent does not match any outer indentation level in that case. Sticking to 4 spaces everywhere, and letting your editor handle it automatically, avoids this entirely.

Now that you understand how Python groups code into blocks, the next logical step is learning how to store and work with data using Python variables.