Indentation

In programming, indentation refers to using spaces or tabs at the beginning of a line of code to visually separate and group code blocks.

In Python, indentation is a fundamental syntax aspect because the language relies on indentation levels to define code blocks. There’s no such thing as curly braces {} or begin and end delimiters for code blocks in Python. Therefore, indentation is not only a practice for improving code readability. It’s also a syntactical requirement that became a tool for achieving code readability.

You must consistently indent code blocks, such as the body of a function, loop, or conditional statement because indentation defines the logical structure of your code.

Use spaces instead of tabs to indent your Python code. Additionally, you must use the same number of spaces —typically four— for each indented code block.

Mixing tabs and spaces can lead to errors, as the Python interpreter strictly enforces indentation levels. If your code isn’t indented properly, you may encounter IndentationError or TabError, which will prevent your program from running.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

You must own this product to join the conversation.