Indentation

Python uses indentation to define code blocks instead of using braces or keywords like “begin” and “end” as in other languages. Code blocks are usually indented using spaces or tabs (eg use four spaces per indentation level).

Consistent indentation is crucial in Python because it determines the grouping and hierarchy of code. It is important to note that mixing spaces and tabs for indentation can lead to errors, so it’s best to choose one and stick to it. In the example below, notice how the indentation is used to define the code blocks within the greet function.

The lines of code inside the if and else blocks are indented, indicating ...

Get Python Made Easy now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.