This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 9: The PEP 8 style guide

Breaking conditions

face Josiah Wang

For conditions (such as those in while and if), you can also use parenthesis to break conditions up into separate lines, or backslashes for explicit line breaks if parenthesis cannot be used.

PEP 8 does not prescribe any recommendations for indentation after the statement. All the following pieces of code, taken from the PEP 8 guide, are acceptable. You might find the first one hard to read though!

# No extra indentation.
if (this_is_one_thing and
    that_is_another_thing):
    do_something()

# Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
    that_is_another_thing):
    # Since both conditions are true, we can frobnicate.
    do_something()

# Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
        and that_is_another_thing):
    do_something()