Discuss@GL4L

Whitespace Formatting


#1

Many languages use curly braces to delimit blocks of code. Python uses indentation:

for i in [1, 2, 3, 4, 5]:
print i # first line in “for i” block
for j in [1, 2, 3, 4, 5]:
print j # first line in “for j” block
print i + j # last line in “for j” block
print i # last line in “for i” block
print “done looping”

This makes Python code very readable, but it also means that you have to be very careful
with your formatting. Whitespace is ignored inside parentheses and brackets, which can be
helpful for long-winded computations:

long_winded_computation = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 +
13 + 14 + 15 + 16 + 17 + 18 + 19 + 20)
and for making code easier to read:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
easier_to_read_list_of_lists = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9] ]

You can also use a backslash to indicate that a statement continues onto the next line,
although we’ll rarely do this:

two_plus_three = 2 +
3

One consequence of whitespace formatting is that it can be hard to copy and paste code
into the Python shell. For example, if you tried to paste the code:

for i in [1, 2, 3, 4, 5]:

notice the blank line

print i

into the ordinary Python shell, you would get a:

IndentationError: expected an indented block

because the interpreter thinks the blank line signals the end of the for loop’s block.
IPython has a magic function %paste, which correctly pastes whatever is on your
clipboard, whitespace and all. This alone is a good reason to use IPython.