# Code Blocks and Indentation

> How Python groups lines into blocks with spaces at the start. What a module is, and how files make up the codebase of a project.

**Course:** [Programming Basics](https://learn.benibix.com/learn/development-concepts)  
**Time:** 10 min

**After this lesson you can:**

- Indent a block the way Python expects
- Explain what a module is
- Describe what a codebase holds

## How Python knows which lines belong together

A code block is a group of lines that run together, as one unit. Python marks a block with indentation: the space at the start of each line. Python's style guide asks for 4 spaces per level. Every line in the block must be pushed in by the same amount.

A block always follows a line that ends in a colon. The line if age >= 18: ends in a colon, so the lines under it that are pushed in form its block. They run only when the test is true. The first line that is not pushed in is back outside the block.

Most languages mark blocks with curly brackets, { and }, and use indentation only to make the code easier to read. Python uses the indentation itself. That keeps Python code tidy, because messy spacing is not just ugly. It changes what the program does.

That is also why Python is strict about it. Mix 4 spaces on one line with 3 on the next, and Python stops with an IndentationError. Mix tabs and spaces, and the lines can look the same on screen while Python sees them as different. Set your editor to put in 4 spaces when you press Tab.

As programs grow, one file gets too long. So code is split into modules. A module is just a .py file whose code other files can use. One file can say import prices and then use what prices.py holds. Python also comes with many modules ready to import, such as math and random.

All the files of one project, kept together, make up its codebase. A small one might be 3 files. A big one can be thousands of files written by hundreds of people. The codebase is what a new team member has to learn their way around.

The mistake that catches most beginners is a line that slips out of its block. The code still runs, but that line now runs every time rather than only when it should. When a program does something at the wrong moment, check the indentation first.

```python
age = 20
if age >= 18:
    print("You can vote.")      # inside the block
    print("Bring your card.")   # inside the block
print("Done.")                  # outside, always runs
```
The two lines pushed in by 4 spaces belong to the if. The last line is outside it and always runs.

![On the left, a codebase called shop-app holding files such as prices.py, cart.py and a tests folder. One file, cart.py, is opened on the right. Inside it, a block of lines under an if is pushed in by 4 spaces and marked as running only when the cart is empty.](https://learn.benibix.com/diagrams/dev7-code-parts.svg)

### Example: Jack's greeting prints too often

Jack's script should print Welcome back only for users who have logged in before. Instead it prints it for all 50 test users.

He looks at the lines and sees the print has no space in front. It sat outside the if block. He pushes it in by 4 spaces, and now only the 12 returning users see it.

**Words in this lesson:** [Code Block](https://learn.benibix.com/term/development/code-block), [Indentation](https://learn.benibix.com/term/development/indentation), [Module (Programming)](https://learn.benibix.com/term/development/module), [Codebase](https://learn.benibix.com/term/development/codebase)

**Question:** How does Python know which lines are inside an if block?

**Answer:** The lines are pushed in by the same amount of space. Python uses indentation to mark a block. Every line pushed in under the colon belongs to it. Curly brackets do that job in many other languages.

---

Source: https://learn.benibix.com/learn/development-concepts/code-blocks-and-indentation
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
