Structural Basics
Whitespace formatting and proper module imports.
Whitespace Formatting
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.
Helpful Whitespace Rules
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 + \
3Copy and Pasting
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 this 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.
Modules
Certain features of Python are not loaded by default. These include both features included as part of the language as well as third-party features that you download yourself. In order to use these features, you’ll need to import the modules that contain them.
One approach is to simply import the module itself:
import re
my_regex = re.compile("[0-9]+", re.I)Here re is the module containing functions and constants for working with regular expressions. After this type of import you can only access those functions by prefixing them with re..
If you already had a different re in your code you could use an alias:
import re as regex
my_regex = regex.compile("[0-9]+", regex.I)You might also do this if your module has an unwieldy name or if you’re going to be typing it a lot. For example, when visualizing data with matplotlib, a standard convention is:
import matplotlib.pyplot as pltIf you need a few specific values from a module, you can import them explicitly and use them without qualification:
from collections import defaultdict, Counter
lookup = defaultdict(int)
my_counter = Counter()If you were a bad person, you could import the entire contents of a module into your namespace, which might inadvertently overwrite variables you’ve already defined:
match = 10
from re import *
print match
# uh oh, re has a match function
# "<function re.match>"However, since you are not a bad person, you won’t ever do this.