📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
  • Indentation
  • Compound Statements
  • Comparisons to singletons
Edit on GitHub
  1. 4 - Conditionals

PEP 8 Recommendations

Indentation

When the conditional part of an if-statement is long enough to require that it be written across multiple lines, it's worth noting that the combination of a two character keyword (i.e. if), plus a single space, plus an opening parenthesis creates a natural 4-space indent for the subsequent lines of the multiline conditional. This can produce a visual conflict with the indented suite of code nested inside the if-statement, which would also naturally be indented to 4 spaces. The PEP 8 takes no explicit position on how (or whether) to further visually distinguish such conditional lines from the nested suite inside the if-statement. Acceptable options in this situation include, but are not limited to:

# 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 do something. 
    do_something()
    
    
# Add some extra indentation on the conditional continuation 
# line.
if (this_is_one_thing 
        and that_is_another_thing): 
    do_something()

Compound Statements

Compound statements (multiple statements on the same line) are generally discouraged.

# Yes:
if foo == 'blah': 
    do_blah_thing() 
do_one() 
do_two() 
do_three()

# Rather not:
if foo == 'blah': do_blah_thing() 
do_one(); do_two(); do_three()

While sometimes it's okay to put an if with a small body on the same line, never do this for multi-clause statements. Also avoid folding such long lines!

# Rather not:
if foo == 'blah': do_blah_thing()

# Definitely not:
if foo == 'blah': do_blah_thing() 
else: do_non_blah_thing()

# Definitely not:
if foo == 'blah': one(); two(); three()

Comparisons to singletons

  • Comparisons to singletons like None should always be done with is or is not, never the equality operators.

  • Beware of writing if x when you really mean if x is not None - e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context!

  • Use is not operator rather than not ... is. While both expressions are functionally identical, the former is more readable and preferred.

# Yes:
if foo is not None:
    do_something()
    
# No:
if not foo is None: 
    do_something()`
PreviousConditional statementsNext5 - Iteration

Last updated 1 year ago