Indentation is leading whitespace (spaces and tabs) at the beginning of a line of code. In the case of Python, they are used to determine the grouping of statements.
Spaces should be preferred over tabs. To be clear, this is in reference to the character itself, not the keys on a keyboard. Your editor/IDE should be configured to insert spaces when the TAB key is pressed. The amount of spaces should be a multiple of 4, except optionally in the case of continuation lines.
The first line is not indented. The next two lines are indented to be inside of the function definition. They will only run when the function is called. The fourth line is indented to be inside the
def foo(): bar = 'baz' # indented one level if bar == 'baz': print('ham') # indented two levels return bar # indented one level
ifstatement, and will only run if the
ifstatement evaluates to
True. The fifth and last line is like the 2nd and 3rd and will always run when the function is called. It effectively closes the
ifstatement above as no more lines can be inside the
ifstatement below that line.