Block F

A great investment of a few minutes Corey Schaefer on string formatting. The first five minutes show you the basics.

Docstrings, preconditions, and postconditions A docstring is a triple-quoted string that appears right after a function header. Its audience is another program, or maybe you, a few months after you wrote the function. It is a brief reference describing what the function does.

A good way to manage this is to have two sections.

Preconditions These describe anything that shhould be true before the function is called. This can include a description of the number and types of inputs the function should have.

Postconditions These describe anything that should be true after the function is called. Here, describe any return value and any side-effects calling the function produces.

My function assignments will be speced in this way.

Whats up __doc__? The standard functions all have docstrings. Here we see some examples.

You can also get information a function by calling the help function on it in an interactive session.

Simple if An if requires a predicate, which is a boolean-valued expression. If the predicate evaluates to True, the CODE executes. Otherwise CODE gets skipped.


if predicate:
    CODE

Fork it: if/else The if-else progression looks like this. If the predicate evaluates to True the block TRUE_CODE executes. Otherwise, the block FALSE_CODE executes. Something is guaranteed to happen. This is a two-tined fork.


if predicate:
    TRUE_CODE
else:
    FALSE_CODE

two-tined fork

Having a good tine with if/else/elif You can have as many tines as you want with this construct.


if predicate1:
    CODE_1
elif predicate2:
    CODE_2

    .
    .
    .


elif predicateN:
    CODE_N
else:
    CODE_DEFAULT