Statements and Indentation
Learn about the indentation rules as well as take a look at the different types of Python statements.
We'll cover the following
Indentation rules
Indentation matters in Python. While other languages use braces in statements, Python uses indentation. There are generally some rules that need to be followed when dealing with indentation in Python, which are the following:
Note: Standard indentation is four spaces. You may not mix spaces with tabs. However, any integrated development environment (IDE) or good text editor can be set to add four spaces when you hit the Tab key on your keyboard.
-
Statements at the same level must be indented exactly the same amount.
-
Nested statements, like, statements within a loop body, must be indented relative to the start of the loop.
-
The first line of a program may not be indented.
-
Each statement is written on a line by itself. However, if the line contains an unclosed
(, [, or {
, it may be continued on the next line. -
If you want to put two statements on the same line, you can separate them with a semicolon (
;
).
If you are used to a language in which every statement ends with a semicolon, this will work in Python. It just looks strange.
The following example provides an overview of all of these indentation rules:
Get hands-on with 1200+ tech skills courses.