Quick Reference


In [1]:
import numpy as np
from datascience import *
from pprint import pprint

Python

Names

Assign values to names with the assignment operator =. Values at the end of a cell are printed.


In [2]:
ten = 3 * 2 + 4
ten


Out[2]:
10

You can also call the print function:


In [3]:
print(ten)


10

In [4]:
# You can also make compound expressions
height = 1.3
the_number_five = abs(-5)
absolute_height_difference = abs(height - 1.688)

Functions


In [5]:
def to_percentage(proportion):
    """Converts a proportion to a percentage."""
    factor = 100
    return proportion * factor
to_percentage(0.19)


Out[5]:
19.0

Help

One can access help and documentation info using Google-fu (i.e., using your favorite internet search engine) or built-in documentation. The cell below shows how to access a quick documentation browser withing a Jupyter notebook. Sometimes, however, the documentation is terse, and you may be better off doing an internet search.


In [6]:
to_percentage?

Strings

A snippet of text is represented by a string in Python. You can create strings with single quotes (') and double quotes (") as delimiters.

Name Example Purpose
"" s = "some text" Create a string
'' s = 'some text' Create a string
+ s1 = 'some'
s2 = 'text'
s = s1 + ' ' + s2
Concatenate strings
.replace 'Hello'.replace('o', 'a') Replace all instances of a substring
.lower 'Hello'.lower() Return a lowercased version of the string
.upper 'hello'.upper() Return an uppercased version of the string
.capitalize 'unIvErSity of cOlOrAdo'.capitalize() Return a version with the first letter capitalized
.title 'unIvErSity of cOlOrAdo'.title() Return a version with the first letter of every word capitalized

Tables and Arrays

Arrays


In [7]:
arr = make_array(0.125, 4.75, -1.3)
arr


Out[7]:
array([ 0.125,  4.75 , -1.3  ])

Index into the array with the .item method:


In [8]:
arr.item(1)


Out[8]:
4.75

Apply arithmetic functions to arrays (provided by NumPy):


In [9]:
2 * (arr + 1.5)


Out[9]:
array([  3.25,  12.5 ,   0.4 ])

In [10]:
np.log10(make_array(1, 2, 10, 1000))


Out[10]:
array([ 0.     ,  0.30103,  1.     ,  3.     ])

In [11]:
np.sum(np.log10(make_array(1, 2, 10, 1000)))


Out[11]:
4.3010299956639813

Tables

Name Example Purpose
Table Table() Create an empty table, usually to extend with data
Table.read_table Table.read_table("my_data.csv") Create a table from a data file
with_columns tbl = Table().with_columns("N", np.arange(5), "2*N", np.arange(0, 10, 2)) Create a copy of a table with more columns
column tbl.column("N") Create an array containing the elements of a column
sort tbl.sort("N") Create a copy of a table sorted by the values in a column
where tbl.where("N", are.above(2)) Create a copy of a table with only the rows that match some predicate
num_rows tbl.num_rows Compute the number of rows in a table
num_columns tbl.num_columns Compute the number of columns in a table
select tbl.select("N") Create a copy of a table with only some of the columns
drop tbl.drop("2*N") Create a copy of a table without some of the columns
take tbl.take(np.arange(0, 6, 2)) Create a copy of the table with only the rows whose indices are in the given array

In [13]:
are?

In [ ]: