Data Science from Scratch: First Principles with Python
Rate it:
Kindle Notes & Highlights
6%
Flag icon
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.
7%
Flag icon
Python functions are first-class, which means that we can assign them to variables and pass them into functions just like any other arguments:
7%
Flag icon
It is also easy to create short anonymous functions, or lambdas:
7%
Flag icon
Function parameters can also be given default arguments,
7%
Flag icon
Strings can be delimited by single or double quotation marks
7%
Flag icon
You can also use square brackets to “slice” lists:
8%
Flag icon
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]
8%
Flag icon
If you don’t want to modify x you can use list addition:
8%
Flag icon
Tuples are lists’ immutable cousins.
8%
Flag icon
Tuples are a convenient way to return multiple values from functions:
8%
Flag icon
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
9%
Flag icon
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:
9%
Flag icon
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.
9%
Flag icon
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:
10%
Flag icon
sort (and sorted)
10%
Flag icon
# 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)
10%
Flag icon
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:
11%
Flag icon
an underscore
11%
Flag icon
zeroes = [0 for _ in even_numbers]      # has the same length as
11%
Flag icon
A generator is something that you can iterate over (for us, usually using for) but whose values are produced only as needed (lazily).
11%
Flag icon
import random four_uniform_randoms = [random.random() for _ in range(4)]