More on this book
Kindle Notes & Highlights
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.
Python functions are first-class, which means that we can assign them to variables and pass them into functions just like any other arguments:
It is also easy to create short anonymous functions, or lambdas:
Function parameters can also be given default arguments,
Strings can be delimited by single or double quotation marks
You can also use square brackets to “slice” lists:
It is easy to concatenate lists together: x = [1, 2, 3] x.extend([4, 5, 6]) # x is now [1,2,3,4,5,6]
If you don’t want to modify x you can use list addition:
Tuples are lists’ immutable cousins.
Tuples are a convenient way to return multiple values from functions:
x, y = 1, 2 # now x is 1, y is 2 x, y = y, x # Pythonic way to swap variables; now x is 2, y is 1
A defaultdict is like a regular dictionary, except that when you try to look up a key it doesn’t contain, it first adds a value for it using a zero-argument function you provided when you created it. In order to use defaultdicts, you have to import them from collections:
These will be useful when we’re using dictionaries to “collect” results by some key and don’t want to have to check every time to see if the key exists yet.
A Counter turns a sequence of values into a defaultdict(int)-like object mapping keys to counts. We will primarily use it to create histograms:
sort (and sorted)
# sort the list by absolute value from largest to smallest x = sorted([-4,1,-2,3], key=abs, reverse=True) # is [-4,3,-2,1] # sort the words and counts from highest count to lowest wc = sorted(word_counts.items(), key=lambda (word, count): count, reverse=True)
List Comprehensions Frequently, you’ll want to transform a list into another list, by choosing only certain elements, or by transforming elements, or both. The Pythonic way of doing this is list comprehensions:
an underscore
zeroes = [0 for _ in even_numbers] # has the same length as
A generator is something that you can iterate over (for us, usually using for) but whose values are produced only as needed (lazily).
import random four_uniform_randoms = [random.random() for _ in range(4)]