Python Language Intro

Agenda

  1. Language overview
  2. Built-in types, operators, and constructs
  3. Functions, Classes, and Modules

Language overview

Note: this is not a language course! Though I'll cover the important bits of the language (and standard library) that are relevant to class material, I expect you to master the language on your own time.

Python ...

  • is interpreted
  • is dynamically-typed
  • is automatically memory-managed
  • supports procedural, object-oriented, imperative and functional programming paradigms
  • is designed (mostly) by one man: Guido van Rossum (aka “benevolent dictator”), and therefore has a fairly opinionated design
  • has a single reference implementation (CPython)
  • version 3 (the most recent version) is not backwards-compatible with version 2, though the latter is still widely used
  • has an interesting programming philosophy: "There should be one — and preferably only one — obvious way to do it." (a.k.a. the "Pythonic" way) — see The Zen of Python

Built in types, operators, and constructs

Numbers

In [112]:
# integers
a = 2
b = 5
c = a + b
d = a * b + 2
e = b // a # integer div
f = b % a  # modulus, remainder
g = a ** b # exponentiation (power)

print(c, d, e, f, g)
7 12 2 1 32
In [113]:
type(2)
Out[113]:
int
In [114]:
type(a)
Out[114]:
int
In [116]:
int()
Out[116]:
0
In [117]:
int(1)
Out[117]:
1
In [118]:
int('25')
Out[118]:
25
In [10]:
dir(1)
Out[10]:
['__abs__',
 '__add__',
 '__and__',
 '__bool__',
 '__ceil__',
 '__class__',
 '__delattr__',
 '__dir__',
 '__divmod__',
 '__doc__',
 '__eq__',
 '__float__',
 '__floor__',
 '__floordiv__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__index__',
 '__init__',
 '__init_subclass__',
 '__int__',
 '__invert__',
 '__le__',
 '__lshift__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__neg__',
 '__new__',
 '__or__',
 '__pos__',
 '__pow__',
 '__radd__',
 '__rand__',
 '__rdivmod__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rfloordiv__',
 '__rlshift__',
 '__rmod__',
 '__rmul__',
 '__ror__',
 '__round__',
 '__rpow__',
 '__rrshift__',
 '__rshift__',
 '__rsub__',
 '__rtruediv__',
 '__rxor__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__truediv__',
 '__trunc__',
 '__xor__',
 'bit_length',
 'conjugate',
 'denominator',
 'from_bytes',
 'imag',
 'numerator',
 'real',
 'to_bytes']
In [15]:
(2).__add__(4)
Out[15]:
6
In [16]:
2 + 4
Out[16]:
6
In [120]:
abs(-20)
Out[120]:
20
In [123]:
(-20).__abs__()
Out[123]:
20
In [124]:
1 + 2
Out[124]:
3
In [132]:
(1).__add__(2)
Out[132]:
3
In [126]:
a, b
Out[126]:
(2, 5)
In [127]:
a + b
Out[127]:
7
In [128]:
a.__add__(b)
Out[128]:
7
In [129]:
a, b
Out[129]:
(2, 5)
In [133]:
class MyInt(int):
    def __add__(self, b):
        return self * b
In [134]:
m = MyInt(10)
In [136]:
m
Out[136]:
10
In [135]:
m + 5
Out[135]:
50
In [137]:
f = 5.0
In [138]:
type(f)
Out[138]:
float
In [139]:
dir(f)
Out[139]:
['__abs__',
 '__add__',
 '__bool__',
 '__class__',
 '__delattr__',
 '__dir__',
 '__divmod__',
 '__doc__',
 '__eq__',
 '__float__',
 '__floordiv__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getformat__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__int__',
 '__le__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__neg__',
 '__new__',
 '__pos__',
 '__pow__',
 '__radd__',
 '__rdivmod__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rfloordiv__',
 '__rmod__',
 '__rmul__',
 '__round__',
 '__rpow__',
 '__rsub__',
 '__rtruediv__',
 '__setattr__',
 '__setformat__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__truediv__',
 '__trunc__',
 'as_integer_ratio',
 'conjugate',
 'fromhex',
 'hex',
 'imag',
 'is_integer',
 'real']

Strings

In [11]:
# strings (`str`)
name = 'John' + " " + 'Doe'

Strings are an example of a sequence type; https://docs.python.org/3.5/library/stdtypes.html#typesseq

Other sequence types are: ranges, tuples (both also immutable), and lists (mutable).

All immutable sequences support the common sequence operations, and mutable sequences additionally support the mutable sequence operations

In [143]:
name
Out[143]:
'John Doe'
In [144]:
"Michael's cat"
Out[144]:
"Michael's cat"
In [145]:
len(name)
Out[145]:
8
In [146]:
name[0]
Out[146]:
'J'
In [147]:
name[1]
Out[147]:
'o'
In [149]:
name[len(name)]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-149-51dd42bda0d1> in <module>()
----> 1 name[len(name)]

IndexError: string index out of range
In [150]:
name[-1]
Out[150]:
'e'
In [151]:
name[-2]
Out[151]:
'o'
In [153]:
name[-8]
Out[153]:
'J'
In [154]:
name[100]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-154-77f74e052296> in <module>()
----> 1 name[100]

IndexError: string index out of range
In [155]:
name[-100]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-155-3d7a0aacd329> in <module>()
----> 1 name[-100]

IndexError: string index out of range

"Slice" syntax: [A:B:C]

  • A = first item to include
  • B = first item to exclude
  • C = "step"
In [156]:
name[0:]
Out[156]:
'John Doe'
In [157]:
name[1:]
Out[157]:
'ohn Doe'
In [159]:
name[5:]
Out[159]:
'Doe'
In [160]:
name[1:4]
Out[160]:
'ohn'
In [161]:
name[1:-1]
Out[161]:
'ohn Do'
In [162]:
name[1:-1:1]
Out[162]:
'ohn Do'
In [163]:
name[1:-1:2]
Out[163]:
'onD'
In [165]:
name
Out[165]:
'John Doe'
In [164]:
name[::2]
Out[164]:
'Jh o'
In [168]:
name[-1:0:-1]
Out[168]:
'eoD nho'
In [13]:
'J' in name
Out[13]:
True
In [14]:
name.__contains__('J')
Out[14]:
True
In [171]:
'z' not in name
Out[171]:
True
In [172]:
dir(name)
Out[172]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']
In [175]:
name.__contains__('J')
Out[175]:
True
In [176]:
min(name)
Out[176]:
' '
In [177]:
max(name)
Out[177]:
'o'
In [178]:
name.index('o')
Out[178]:
1
In [180]:
name.index('o', 2)
Out[180]:
6
In [181]:
name.count('o')
Out[181]:
2
In [182]:
name * 10
Out[182]:
'John DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn Doe'
In [192]:
name
Out[192]:
'John Doe'
In [203]:
it = iter(name)
In [212]:
next(it)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-212-2cdb14c0d4d6> in <module>()
----> 1 next(it)

StopIteration: 
In [213]:
it = iter(name)
while True:
    try:
        print(next(it))
    except StopIteration:
        break
J
o
h
n
 
D
o
e
In [19]:
for c in name:
    print(c)
J
o
h
n
 
D
o
e

Ranges & Tuples

In [22]:
# ranges
r1 = range(10)
r2 = range(10, 20)
r3 = range(100, 5, -10)
In [27]:
for i in range(10, 0, -1):
    print(i)
10
9
8
7
6
5
4
3
2
1
In [28]:
5 in range(10)
Out[28]:
True
In [29]:
range(1) + range(3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-6f5f6d32664c> in <module>()
----> 1 range(1) + range(3)

TypeError: unsupported operand type(s) for +: 'range' and 'range'
In [30]:
range(100000000)
Out[30]:
range(0, 100000000)
In [32]:
tup = ('lions', 'tigers', 'bears', (0, 1, 2), True, False)
In [33]:
len(tup)
Out[33]:
6
In [34]:
len(tup[3])
Out[34]:
3
In [36]:
tup[3][0]
Out[36]:
0
In [38]:
5+(5)
Out[38]:
10
In [40]:
5 + (5,)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-40-184d1f5d8d4e> in <module>()
----> 1 5 + (5,)

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
In [41]:
5, 6, 7
Out[41]:
(5, 6, 7)
In [42]:
a, b = 1, 2
In [45]:
a, b
Out[45]:
(1, 2)
In [50]:
a, (b1, b2), c = 1, (4, 5), "hello" # destructuring assignment
In [51]:
a, b1, b2, c
Out[51]:
(1, 4, 5, 'hello')
In [53]:
a, *rest = 1, 2, 3, 4, 5, 6
In [54]:
a
Out[54]:
1
In [56]:
rest
Out[56]:
[2, 3, 4, 5, 6]
In [57]:
a, *rest = 1, 2
In [59]:
a, rest
Out[59]:
(1, [2])
In [60]:
a, *rest = 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-60-97d4ff41311c> in <module>()
----> 1 a, *rest = 1

TypeError: 'int' object is not iterable
In [61]:
tup = ("hello", "world", 1, 2, 3)
In [62]:
tup[1]
Out[62]:
'world'
In [63]:
tup[1] = "planet"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-63-1cca7a601561> in <module>()
----> 1 tup[1] = "planet"

TypeError: 'tuple' object does not support item assignment
In [64]:
'hello'[1] = 'a'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-64-99fad38f0974> in <module>()
----> 1 'hello'[1] = 'a'

TypeError: 'str' object does not support item assignment

Tuples and Strings are immutable data containers

In [65]:
s1 = 'hello'
s2 = 'world'
In [66]:
s1 + ' some ' + s2
Out[66]:
'hello some world'

The almighty list!

In [67]:
l = []
In [68]:
len(l)
Out[68]:
0
In [69]:
100 in l
Out[69]:
False
In [70]:
l[0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-70-44e56f8a6e9f> in <module>()
----> 1 l[0]

IndexError: list index out of range
In [71]:
l.append(100)
In [72]:
l
Out[72]:
[100]
In [73]:
for i in range(10):
    l.append(i)
In [74]:
l
Out[74]:
[100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [75]:
l = list(range(10))
In [76]:
l
Out[76]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [77]:
type(l)
Out[77]:
list
In [78]:
dir(list)
Out[78]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']
In [80]:
l
Out[80]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [115]:
l = ["apples", "bananas", "pears", "lions", "tigers", "bears"]
In [116]:
l[1:2] = 100
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-116-775af42cb49b> in <module>()
----> 1 l[1:2] = 100

TypeError: can only assign an iterable
In [114]:
l
Out[114]:
['apples', 100, 'pears', 'lions', 'tigers', 'bears']
In [103]:
l[1:3] = [1, 2, 3, 4, 5]
In [104]:
l
Out[104]:
['apples', 1, 2, 3, 4, 5, 'lions', 'tigers', 'bears']
In [117]:
l = ["apples", "bananas", "pears", "lions", "tigers", "bears"]
In [119]:
del l[0]
In [120]:
l
Out[120]:
['bananas', 'pears', 'lions', 'tigers', 'bears']
In [122]:
del l[3]
In [123]:
l
Out[123]:
['bananas', 'pears', 'lions', 'bears']
In [124]:
l.remove('pears')
In [125]:
l
Out[125]:
['bananas', 'lions', 'bears']
In [126]:
l.remove('whale')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-126-82374966c105> in <module>()
----> 1 l.remove('whale')

ValueError: list.remove(x): x not in list

List comprehensions

In [130]:
l = []
for n in range(1, 11):
    l.append(2*n)
l
Out[130]:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
In [131]:
[2*x for x in range(1, 11)]
Out[131]:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
In [132]:
[2**x for x in range(10)]
Out[132]:
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
In [133]:
n = 10
l = []
for a in range(1, n+1):
    for b in range(1, n+1):
        for c in range(1, n+1):
            if a**2 + b**2 == c**2:
                l.append((a, b, c))
In [134]:
l
Out[134]:
[(3, 4, 5), (4, 3, 5), (6, 8, 10), (8, 6, 10)]
In [173]:
l =[(a, b, c) for a in range(1, n+1)
           for b in range(1, n+1)
           for c in range(1, n+1)
           if a**2 + b**2 == c**2]
l
Out[173]:
[(3, 4, 5), (4, 3, 5), (6, 8, 10), (8, 6, 10)]
In [140]:
l = [(x, y) for x in range(1,4) for y in range(100,103)]
In [142]:
l[0] = "hello"
In [143]:
l
Out[143]:
['hello',
 (1, 101),
 (1, 102),
 (2, 100),
 (2, 101),
 (2, 102),
 (3, 100),
 (3, 101),
 (3, 102)]
In [146]:
l = [[]] * 5
In [147]:
l
Out[147]:
[[], [], [], [], []]
In [149]:
l[0].append("hello")
In [150]:
l
Out[150]:
[['hello'], ['hello'], ['hello'], ['hello'], ['hello']]
In [151]:
l = [[], [], [], [], []]
In [152]:
l[0].append("hello")
In [153]:
l
Out[153]:
[['hello'], [], [], [], []]
In [154]:
l = [[] for x in range(10)]
In [156]:
l
Out[156]:
[[], [], [], [], [], [], [], [], [], []]
In [157]:
l[0].append('hello')
In [158]:
l
Out[158]:
[['hello'], [], [], [], [], [], [], [], [], []]
In [159]:
from random import random
[random()] * 10
Out[159]:
[0.5186276507357199,
 0.5186276507357199,
 0.5186276507357199,
 0.5186276507357199,
 0.5186276507357199,
 0.5186276507357199,
 0.5186276507357199,
 0.5186276507357199,
 0.5186276507357199,
 0.5186276507357199]
In [170]:
[random() for _ in range(10)]
Out[170]:
[0.5158381902597979,
 0.5239756762817014,
 0.684357669474381,
 0.49342537577111034,
 0.9886514487414778,
 0.8440603151959953,
 0.43850712068093634,
 0.5698557034556718,
 0.25343501774283583,
 0.30331132918895765]
In [174]:
l
Out[174]:
[(3, 4, 5), (4, 3, 5), (6, 8, 10), (8, 6, 10)]
In [175]:
for x in l:
    print(x)
(3, 4, 5)
(4, 3, 5)
(6, 8, 10)
(8, 6, 10)
In [179]:
for a, b, c in l:
    print(a, b, c)
3 4 5
4 3 5
6 8 10
8 6 10

Iteration is based on two functions: iter and next

In [180]:
l
Out[180]:
[(3, 4, 5), (4, 3, 5), (6, 8, 10), (8, 6, 10)]
In [182]:
it = iter(l)
In [187]:
next(it)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-187-2cdb14c0d4d6> in <module>()
----> 1 next(it)

StopIteration: 
In [194]:
l1 = [1, 2, 3, 4]
l2 = ["hello", "world"]
In [195]:
l1 + l2
Out[195]:
[1, 2, 3, 4, 'hello', 'world']
In [196]:
l3 = l1 + l2
In [197]:
l3
Out[197]:
[1, 2, 3, 4, 'hello', 'world']
In [198]:
l1, l2
Out[198]:
([1, 2, 3, 4], ['hello', 'world'])
In [191]:
l1.extend(l2)
In [192]:
l1
Out[192]:
[1, 2, 3, 4, 'hello', 'world']
In [193]:
l2
Out[193]:
['hello', 'world']
In [199]:
ll = [list(range(10)) for _ in range(10)]
In [200]:
ll
Out[200]:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
In [201]:
ll[4][4] = 100
In [202]:
ll
Out[202]:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 100, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

Dictionaries (aka Maps)

In [203]:
d = dict()
In [204]:
d
Out[204]:
{}
In [205]:
d = {}
In [206]:
d
Out[206]:
{}
In [207]:
d[0] = 'one'
In [208]:
d
Out[208]:
{0: 'one'}
In [209]:
d[0]
Out[209]:
'one'
In [210]:
d[2] = 'two'
In [212]:
d
Out[212]:
{0: 'one', 2: 'two'}
In [213]:
d['name'] = 'Michael'
d['color'] = 'Blue'
In [214]:
d
Out[214]:
{0: 'one', 2: 'two', 'name': 'Michael', 'color': 'Blue'}
In [215]:
d[0] = 'zero'
In [216]:
d
Out[216]:
{0: 'zero', 2: 'two', 'name': 'Michael', 'color': 'Blue'}
In [217]:
del d[2]
In [218]:
d
Out[218]:
{0: 'zero', 'name': 'Michael', 'color': 'Blue'}
In [220]:
list(d.keys())
Out[220]:
[0, 'name', 'color']
In [221]:
list(d.values())
Out[221]:
['zero', 'Michael', 'Blue']
In [222]:
list(d.items())
Out[222]:
[(0, 'zero'), ('name', 'Michael'), ('color', 'Blue')]
In [223]:
for k in d.keys():
    print(k)
0
name
color
In [224]:
for k in d:
    print(k)
0
name
color
In [227]:
for k, v in d.items():
    print(k, '=>', v)
0 => zero
name => Michael
color => Blue
In [245]:
d
Out[245]:
{0: 'zero', 'name': 'Michael', 'color': 'Blue'}
In [247]:
d[100]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-247-cb47c7152e2b> in <module>()
----> 1 d[100]

KeyError: 100
In [249]:
0 in d
Out[249]:
True
In [250]:
100 in d
Out[250]:
False
In [228]:
import urllib.request
In [232]:
peter_pan_text = urllib.request.urlopen('https://www.gutenberg.org/files/16/16-0.txt').read().decode()
In [233]:
peter_pan_text[:100]
Out[233]:
'\ufeffThe Project Gutenberg EBook of Peter Pan, by James M. Barrie\r\n\r\nThis eBook is for the use of anyone'
In [236]:
peter_pan_words = peter_pan_text.split()
In [237]:
peter_pan_words[1000:1025]
Out[237]:
['excitement',
 'over',
 'John,',
 'and',
 'Michael',
 'had',
 'even',
 'a',
 'narrower',
 'squeak;',
 'but',
 'both',
 'were',
 'kept,',
 'and',
 'soon,',
 'you',
 'might',
 'have',
 'seen',
 'the',
 'three',
 'of',
 'them',
 'going']
In [238]:
peter_pan_words.count('Peter')
Out[238]:
238
In [239]:
peter_pan_words.count('Tink')
Out[239]:
26
In [241]:
peter_pan_words.index('Hook')
Out[241]:
12432
In [251]:
peter_pan_word_count = {}
for w in peter_pan_words:
    if w in peter_pan_word_count:
        peter_pan_word_count[w] += 1
    else:
        peter_pan_word_count[w] = 1
In [253]:
peter_pan_word_count['the']
Out[253]:
2331
In [254]:
peter_pan_word_count['The']
Out[254]:
150
In [255]:
peter_pan_word_count
Out[255]:
{'\ufeffThe': 1,
 'Project': 79,
 'Gutenberg': 22,
 'EBook': 2,
 'of': 929,
 'Peter': 238,
 'Pan,': 4,
 'by': 175,
 'James': 8,
 'M.': 4,
 'Barrie': 4,
 'This': 32,
 'eBook': 9,
 'is': 350,
 'for': 377,
 'the': 2331,
 'use': 19,
 'anyone': 4,
 'anywhere': 2,
 'at': 322,
 'no': 136,
 'cost': 2,
 'and': 1396,
 'with': 361,
 'almost': 36,
 'restrictions': 2,
 'whatsoever.': 2,
 'You': 36,
 'may': 49,
 'copy': 8,
 'it,': 63,
 'give': 26,
 'it': 473,
 'away': 29,
 'or': 133,
 're-use': 2,
 'under': 28,
 'terms': 21,
 'License': 8,
 'included': 3,
 'this': 195,
 'online': 4,
 'www.gutenberg.org': 2,
 '**': 4,
 'a': 962,
 'COPYRIGHTED': 1,
 'eBook,': 2,
 'Details': 1,
 'Below': 1,
 'Please': 2,
 'follow': 8,
 'copyright': 16,
 'guidelines': 1,
 'in': 683,
 'file.': 1,
 'Title:': 1,
 'Pan': 13,
 'Wendy': 200,
 'Author:': 1,
 'Posting': 1,
 'Date:': 2,
 'June': 1,
 '25,': 1,
 '2008': 1,
 '[EBook': 1,
 '#16]': 1,
 'Release': 1,
 'July,': 1,
 '1991': 1,
 'Last': 2,
 'Updated:': 1,
 'October': 1,
 '14,': 1,
 '2016': 1,
 'Language:': 1,
 'English': 4,
 'Character': 1,
 'set': 16,
 'encoding:': 1,
 'UTF-8': 1,
 '***': 8,
 'START': 1,
 'OF': 10,
 'THIS': 7,
 'PROJECT': 4,
 'GUTENBERG': 3,
 'EBOOK': 2,
 'PETER': 5,
 'PAN': 3,
 '[PETER': 1,
 'AND': 2,
 'WENDY]': 1,
 'By': 12,
 'J.': 1,
 '[James': 1,
 'Matthew': 1,
 'Barrie]': 1,
 'A': 19,
 'Millennium': 1,
 'Fulcrum': 1,
 'Edition': 1,
 '(c)1991': 1,
 'Duncan': 1,
 'Research': 1,
 'Contents:': 1,
 'Chapter': 34,
 '1': 2,
 'BREAKS': 2,
 'THROUGH': 2,
 '2': 2,
 'THE': 29,
 'SHADOW': 2,
 '3': 4,
 'COME': 6,
 'AWAY,': 2,
 'AWAY!': 2,
 '4': 3,
 'FLIGHT': 2,
 '5': 2,
 'ISLAND': 2,
 'TRUE': 2,
 '6': 2,
 'LITTLE': 2,
 'HOUSE': 2,
 '7': 2,
 'HOME': 6,
 'UNDER': 3,
 'GROUND': 2,
 '8': 2,
 'MERMAID’S': 1,
 'LAGOON': 2,
 '9': 2,
 'NEVER': 2,
 'BIRD': 2,
 '10': 2,
 'HAPPY': 2,
 '11': 2,
 'WENDY’S': 2,
 'STORY': 2,
 '12': 2,
 'CHILDREN': 2,
 'ARE': 2,
 'CARRIED': 2,
 'OFF': 2,
 '13': 2,
 'DO': 2,
 'YOU': 8,
 'BELIEVE': 2,
 'IN': 3,
 'FAIRIES?': 2,
 '14': 4,
 'PIRATE': 2,
 'SHIP': 2,
 '15': 2,
 '“HOOK': 2,
 'OR': 8,
 'ME': 2,
 'TIME”': 2,
 '16': 2,
 'RETURN': 2,
 '17': 2,
 'WHEN': 2,
 'WENDY': 2,
 'GREW': 2,
 'UP': 2,
 'All': 17,
 'children,': 6,
 'except': 19,
 'one,': 15,
 'grow': 8,
 'up.': 14,
 'They': 102,
 'soon': 26,
 'know': 65,
 'that': 564,
 'they': 465,
 'will': 84,
 'up,': 18,
 'way': 60,
 'knew': 63,
 'was': 898,
 'this.': 6,
 'One': 9,
 'day': 11,
 'when': 152,
 'she': 465,
 'two': 36,
 'years': 3,
 'old': 25,
 'playing': 10,
 'garden,': 1,
 'plucked': 2,
 'another': 24,
 'flower': 2,
 'ran': 17,
 'to': 1214,
 'her': 361,
 'mother.': 9,
 'I': 253,
 'suppose': 7,
 'must': 68,
 'have': 247,
 'looked': 33,
 'rather': 41,
 'delightful,': 1,
 'Mrs.': 72,
 'Darling': 93,
 'put': 40,
 'hand': 32,
 'heart': 13,
 'cried,': 42,
 '“Oh,': 24,
 'why': 13,
 'can’t': 24,
 'you': 403,
 'remain': 5,
 'like': 86,
 'ever!”': 1,
 'all': 221,
 'passed': 12,
 'between': 13,
 'them': 165,
 'on': 329,
 'subject,': 2,
 'but': 378,
 'henceforth': 2,
 'always': 50,
 'after': 45,
 'are': 188,
 'two.': 3,
 'Two': 2,
 'beginning': 8,
 'end.': 3,
 'Of': 37,
 'course': 55,
 'lived': 6,
 '[their': 1,
 'house': 25,
 'number': 7,
 'their': 215,
 'street],': 1,
 'until': 21,
 'came': 71,
 'mother': 33,
 'chief': 3,
 'one.': 11,
 'She': 109,
 'lovely': 14,
 'lady,': 7,
 'romantic': 4,
 'mind': 11,
 'such': 60,
 'sweet': 7,
 'mocking': 4,
 'mouth.': 5,
 'Her': 10,
 'tiny': 5,
 'boxes,': 1,
 'one': 170,
 'within': 11,
 'other,': 5,
 'come': 50,
 'from': 145,
 'puzzling': 2,
 'East,': 1,
 'however': 3,
 'many': 25,
 'discover': 4,
 'there': 111,
 'more;': 2,
 'mouth': 15,
 'had': 498,
 'kiss': 8,
 'could': 139,
 'never': 65,
 'get,': 1,
 'though': 46,
 'was,': 14,
 'perfectly': 6,
 'conspicuous': 1,
 'right-hand': 2,
 'corner.': 2,
 'The': 150,
 'Mr.': 46,
 'won': 1,
 'this:': 2,
 'gentlemen': 3,
 'who': 134,
 'been': 135,
 'boys': 61,
 'girl': 10,
 'discovered': 7,
 'simultaneously': 2,
 'loved': 13,
 'her,': 37,
 'propose': 1,
 'Darling,': 10,
 'took': 24,
 'cab': 2,
 'nipped': 2,
 'first,': 7,
 'so': 197,
 'he': 866,
 'got': 33,
 'her.': 41,
 'He': 163,
 'innermost': 1,
 'box': 2,
 'kiss.': 3,
 'about': 106,
 'box,': 2,
 'time': 81,
 'gave': 36,
 'up': 113,
 'trying': 10,
 'thought': 69,
 'Napoleon': 1,
 'can': 50,
 'picture': 1,
 'him': 186,
 'trying,': 2,
 'then': 67,
 'going': 31,
 'off': 31,
 'passion,': 1,
 'slamming': 1,
 'door.': 4,
 'used': 17,
 'boast': 2,
 'not': 375,
 'only': 84,
 'respected': 1,
 'him.': 66,
 'those': 17,
 'deep': 2,
 'ones': 13,
 'stocks': 3,
 'shares.': 1,
 'really': 44,
 'knows,': 1,
 'quite': 62,
 'seemed': 23,
 'know,': 11,
 'often': 18,
 'said': 218,
 'were': 243,
 'shares': 1,
 'down': 51,
 'would': 211,
 'made': 49,
 'any': 68,
 'woman': 7,
 'respect': 3,
 'married': 6,
 'white,': 3,
 'first': 64,
 'kept': 12,
 'books': 1,
 'perfectly,': 1,
 'gleefully,': 3,
 'as': 315,
 'if': 125,
 'game,': 3,
 'much': 30,
 'Brussels': 1,
 'sprout': 1,
 'missing;': 1,
 'whole': 14,
 'cauliflowers': 1,
 'dropped': 12,
 'out,': 15,
 'instead': 13,
 'pictures': 1,
 'babies': 4,
 'without': 41,
 'faces.': 1,
 'drew': 11,
 'should': 47,
 'totting': 2,
 'Darling’s': 7,
 'guesses.': 2,
 'John,': 21,
 'Michael.': 6,
 'For': 28,
 'week': 6,
 'doubtful': 2,
 'whether': 18,
 'be': 249,
 'able': 10,
 'keep': 14,
 'feed.': 1,
 'frightfully': 10,
 'proud': 7,
 'very': 64,
 'honourable,': 1,
 'sat': 34,
 'edge': 3,
 'bed,': 11,
 'holding': 5,
 'calculating': 1,
 'expenses,': 3,
 'while': 25,
 'imploringly.': 2,
 'wanted': 14,
 'risk': 1,
 'what': 105,
 'might,': 1,
 'his': 455,
 'way;': 1,
 'pencil': 1,
 'piece': 4,
 'paper,': 2,
 'confused': 2,
 'suggestions': 1,
 'begin': 1,
 'again.': 22,
 '“Now': 8,
 'don’t': 54,
 'interrupt,”': 1,
 'beg': 2,
 '“I': 159,
 'pound': 2,
 'seventeen': 1,
 'here,': 6,
 'six': 9,
 'office;': 1,
 'cut': 13,
 'my': 69,
 'coffee': 1,
 'office,': 3,
 'say': 36,
 'ten': 8,
 'shillings,': 1,
 'making': 10,
 'nine': 13,
 'six,': 2,
 'your': 80,
 'eighteen': 1,
 'three': 24,
 'makes': 7,
 'seven,': 2,
 'five': 3,
 'naught': 2,
 'cheque-book': 3,
 'eight': 3,
 'seven--who': 1,
 'moving?--eight': 1,
 'dot': 1,
 'carry': 10,
 'seven--don’t': 1,
 'speak,': 2,
 'own--and': 1,
 'lent': 1,
 'man': 33,
 'door--quiet,': 1,
 'child--dot': 1,
 'child--there,': 1,
 'you’ve': 1,
 'done': 10,
 'it!--did': 1,
 'seven?': 1,
 'yes,': 5,
 'seven;': 1,
 'question': 7,
 'is,': 6,
 'we': 138,
 'try': 2,
 'year': 6,
 'seven?”': 1,
 '“Of': 8,
 'can,': 1,
 'George,”': 5,
 'cried.': 18,
 'But': 65,
 'prejudiced': 2,
 'Wendy’s': 26,
 'favour,': 1,
 'grander': 1,
 'character': 2,
 '“Remember': 1,
 'mumps,”': 1,
 'warned': 3,
 'threateningly,': 1,
 'went': 48,
 '“Mumps': 1,
 'pound,': 1,
 'down,': 7,
 'daresay': 2,
 'more': 62,
 'thirty': 3,
 'shillings--don’t': 1,
 'speak--measles': 1,
 'five,': 1,
 'German': 1,
 'measles': 2,
 'half': 8,
 'guinea,': 1,
 'fifteen': 2,
 'six--don’t': 1,
 'waggle': 1,
 'finger--whooping-cough,': 1,
 'shillings”--and': 1,
 'went,': 5,
 'added': 5,
 'differently': 1,
 'each': 31,
 'time;': 6,
 'last': 49,
 'just': 74,
 'through,': 3,
 'mumps': 1,
 'reduced': 1,
 'twelve': 1,
 'kinds': 1,
 'treated': 7,
 'There': 43,
 'same': 22,
 'excitement': 1,
 'over': 57,
 'Michael': 69,
 'even': 42,
 'narrower': 1,
 'squeak;': 1,
 'both': 17,
 'kept,': 1,
 'soon,': 2,
 'might': 28,
 'seen': 20,
 'row': 4,
 'Miss': 2,
 'Fulsom’s': 2,
 'Kindergarten': 1,
 'school,': 3,
 'accompanied': 4,
 'nurse.': 4,
 'everything': 6,
 'so,': 9,
 'passion': 2,
 'being': 26,
 'exactly': 13,
 'neighbours;': 1,
 'course,': 12,
 'As': 24,
 'poor,': 1,
 'owing': 4,
 'amount': 1,
 'milk': 2,
 'children': 75,
 'drank,': 1,
 'nurse': 5,
 'prim': 1,
 'Newfoundland': 1,
 'dog,': 2,
 'called': 31,
 'Nana,': 9,
 'belonged': 1,
 'particular': 7,
 'Darlings': 2,
 'engaged': 1,
 'important,': 1,
 'however,': 17,
 'become': 9,
 'acquainted': 1,
 'Kensington': 3,
 'Gardens,': 1,
 'where': 42,
 'spent': 3,
 'most': 40,
 'spare': 1,
 'peeping': 6,
 'into': 101,
 'perambulators,': 1,
 'hated': 5,
 'careless': 5,
 'nursemaids,': 1,
 'whom': 11,
 'followed': 14,
 'homes': 1,
 'complained': 1,
 'mistresses.': 1,
 'proved': 2,
 'treasure': 1,
 'How': 10,
 'thorough': 1,
 'bath-time,': 2,
 'moment': 48,
 'night': 37,
 'charges': 2,
 'slightest': 3,
 'cry.': 5,
 'kennel': 6,
 'nursery.': 4,
 'genius': 3,
 'knowing': 7,
 'cough': 1,
 'thing': 33,
 'patience': 1,
 'needs': 1,
 'stocking': 2,
 'around': 17,
 'throat.': 1,
 'believed': 7,
 'old-fashioned': 1,
 'remedies': 1,
 'rhubarb': 1,
 'leaf,': 2,
 'sounds': 3,
 'contempt': 3,
 'new-fangled': 1,
 'talk': 6,
 'germs,': 1,
 'on.': 11,
 'It': 121,
 'lesson': 2,
 'propriety': 1,
 'see': 85,
 'escorting': 1,
 'walking': 3,
 'sedately': 1,
 'side': 12,
 'well': 24,
 'behaved,': 1,
 'butting': 1,
 'back': 31,
 'line': 4,
 'strayed.': 1,
 'On': 12,
 'John’s': 5,
 'footer': 1,
 '[in': 1,
 'England': 1,
 'soccer': 1,
 'football,': 1,
 '“footer”': 1,
 'short]': 1,
 'days': 18,
 'once': 40,
 'forgot': 9,
 'sweater,': 1,
 'usually': 3,
 'carried': 11,
 'an': 102,
 'umbrella': 1,
 'case': 2,
 'rain.': 1,
 'room': 20,
 'basement': 1,
 'school': 5,
 'nurses': 1,
 'wait.': 3,
 'forms,': 1,
 'Nana': 34,
 'lay': 17,
 'floor,': 7,
 'difference.': 1,
 'affected': 2,
 'ignore': 1,
 'inferior': 3,
 'social': 2,
 'status': 5,
 'themselves,': 7,
 'despised': 3,
 'light': 26,
 'talk.': 1,
 'resented': 2,
 'visits': 1,
 'nursery': 23,
 'friends,': 3,
 'did': 115,
 'whipped': 4,
 'Michael’s': 7,
 'pinafore': 1,
 'blue': 4,
 'braiding,': 1,
 'smoothed': 1,
 'out': 115,
 'dash': 1,
 'hair.': 2,
 'No': 12,
 'possibly': 2,
 'conducted': 2,
 'correctly,': 1,
 'yet': 14,
 'sometimes': 22,
 'wondered': 2,
 'uneasily': 1,
 'neighbours': 1,
 'talked.': 1,
 'position': 3,
 'city': 1,
 'consider.': 1,
 'also': 18,
 'troubled': 4,
 'way.': 10,
 'feeling': 15,
 'admire': 2,
 'admires': 1,
 'tremendously,': 2,
 'assure': 1,
 'him,': 53,
 'sign': 3,
 'specially': 4,
 'nice': 12,
 'father.': 2,
 'Lovely': 1,
 'dances': 2,
 'followed,': 1,
 'which': 122,
 'other': 69,
 'servant,': 1,
 'Liza,': 2,
 'allowed': 5,
 'join.': 1,
 'Such': 4,
 'midget': 1,
 'long': 50,
 'skirt': 1,
 'maid’s': 1,
 'cap,': 1,
 'sworn,': 1,
 'engaged,': 1,
 'gaiety': 2,
 'romps!': 1,
 'And': 27,
 'gayest': 1,
 'pirouette': 1,
 'wildly': 3,
 'kiss,': 3,
 'dashed': 2,
 'it.': 56,
 'simpler': 1,
 'happier': 1,
 'family': 3,
 'coming': 9,
 'Pan.': 5,
 'heard': 43,
 'tidying': 2,
 'children’s': 4,
 'minds.': 1,
 'nightly': 1,
 'custom': 3,
 'every': 39,
 'good': 25,
 'asleep': 9,
 'rummage': 1,
 'minds': 4,
 'things': 29,
 'straight': 7,
 'next': 10,
 'morning,': 3,
 'repacking': 1,
 'proper': 4,
 'places': 2,
 'articles': 1,
 'wandered': 2,
 'during': 2,
 'day.': 1,
 'If': 33,
 'awake': 4,
 '(but': 2,
 'can’t)': 1,
 'own': 19,
 'doing': 11,
 'this,': 18,
 'find': 15,
 'interesting': 2,
 'watch': 8,
 'drawers.': 1,
 'knees,': 4,
 'expect,': 1,
 'lingering': 2,
 'humorously': 1,
 'some': 30,
 'contents,': 1,
 'wondering': 3,
 'earth': 3,
 'picked': 2,
 'discoveries': 1,
 'sweet,': 4,
 'pressing': 3,
 'cheek': 2,
 'kitten,': 1,
 'hurriedly': 1,
 'stowing': 1,
 'sight.': 5,
 'When': 18,
 'wake': 5,
 'naughtiness': 1,
 'evil': 5,
 'passions': 1,
 'bed': 25,
 'folded': 2,
 'small': 11,
 'placed': 2,
 'bottom': 2,
 'top,': 2,
 'beautifully': 2,
 'aired,': 3,
 'spread': 2,
 'prettier': 1,
 'thoughts,': 1,
 'ready': 3,
 'ever': 37,
 'map': 5,
 'person’s': 1,
 'mind.': 5,
 'Doctors': 1,
 'draw': 9,
 'maps': 2,
 'parts': 2,
 'you,': 24,
 'intensely': 1,
 'interesting,': 2,
 'catch': 4,
 'child’s': 1,
 'mind,': 1,
 'confused,': 1,
 'keeps': 2,
 'round': 43,
 'time.': 9,
 'zigzag': 1,
 'lines': 2,
 'temperature': 1,
 'card,': 1,
 'these': 26,
 'probably': 8,
 'roads': 1,
 'island,': 9,
 'Neverland': 11,
 'less': 9,
 'astonishing': 2,
 'splashes': 1,
 'colour': 5,
 'here': 16,
 'there,': 9,
 'coral': 2,
 'reefs': 1,
 'rakish-looking': 2,
 'craft': 3,
 'offing,': 1,
 'savages': 2,
 'lonely': 4,
 'lairs,': 1,
 'gnomes': 1,
 'mostly': 3,
 'tailors,': 1,
 'caves': 2,
 'through': 46,
 'river': 1,
 'runs,': 2,
 'princes': 1,
 'elder': 1,
 'brothers,': 1,
 'hut': 1,
 'fast': 4,
 'decay,': 1,
 'lady': 15,
 'hooked': 1,
 'nose.': 1,
 'easy': 5,
 'all,': 11,
 'religion,': 1,
 'fathers,': 1,
 'pond,': 1,
 'needle-work,': 1,
 'murders,': 1,
 'hangings,': 1,
 'verbs': 1,
 'take': 31,
 'dative,': 1,
 'chocolate': 2,
 'pudding': 1,
 'day,': 6,
 'getting': 11,
 'braces,': 1,
 'ninety-nine,': 1,
 'three-pence': 1,
 'pulling': 5,
 'tooth': 1,
 'yourself,': 1,
 'on,': 10,
 'either': 4,
 'part': 21,
 'island': 18,
 'showing': 9,
 'confusing,': 1,
 'especially': 7,
 'nothing': 15,
 'stand': 6,
 'still.': 3,
 'Neverlands': 2,
 'vary': 1,
 'deal.': 1,
 'John’s,': 1,
 'instance,': 5,
 'lagoon': 13,
 'flamingoes': 1,
 'flying': 14,
 'John': 76,
 'shooting,': 1,
 'Michael,': 17,
 'small,': 1,
 'flamingo': 2,
 'lagoons': 1,
 'boat': 3,
 'turned': 12,
 'upside': 1,
 'sands,': 1,
 'wigwam,': 1,
 'leaves': 9,
 'deftly': 2,
 'sewn': 2,
 'together.': 3,
 'friends': 1,
 'night,': 10,
 'pet': 3,
 'wolf': 2,
 'forsaken': 1,
 'its': 28,
 'parents,': 2,
 'resemblance,': 1,
 'stood': 22,
 'still': 51,
 'other’s': 6,
 'nose,': 1,
 'forth.': 3,
 'magic': 2,
 'shores': 1,
 'play': 8,
 'beaching': 1,
 'coracles': 1,
 '[simple': 1,
 'boat].': 1,
 'We': 16,
 'too': 34,
 'there;': 3,
 'hear': 32,
 'sound': 28,
 'surf,': 1,
 'shall': 25,
 'land': 5,
 'more.': 8,
 'delectable': 1,
 'islands': 1,
 'snuggest': 1,
 'compact,': 1,
 'large': 13,
 'sprawly,': 1,
 'tedious': 1,
 'distances': 1,
 'adventure': 10,
 'another,': 8,
 'nicely': 2,
 'crammed.': 1,
 'chairs': 2,
 'table-cloth,': 1,
 'least': 11,
 'alarming,': 1,
 'minutes': 5,
 'before': 40,
 'go': 56,
 'sleep': 13,
 'becomes': 1,
 'real.': 1,
 'That': 14,
 'night-lights.': 2,
 'Occasionally': 1,
 'travels': 1,
 'found': 40,
 'understand,': 3,
 'perplexing': 1,
 'word': 8,
 'Peter.': 27,
 'Peter,': 50,
 'minds,': 1,
 'began': 17,
 'scrawled': 1,
 'name': 8,
 'bolder': 1,
 'letters': 2,
 'than': 60,
 'words,': 8,
 'gazed': 5,
 'felt': 22,
 'oddly': 2,
 'cocky': 1,
 'appearance.': 1,
 '“Yes,': 20,
 'cocky,”': 1,
 'admitted': 3,
 'regret.': 1,
 'questioning': 1,
 '“But': 17,
 'he,': 2,
 'pet?”': 1,
 '“He': 16,
 'mother.”': 7,
 'At': 18,
 'thinking': 14,
 'childhood': 1,
 ...}

Functions, Classes, and Modules

Functions

In [258]:
def foo():
    pass
In [259]:
foo()
In [260]:
foo(1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-260-9e45007b2b59> in <module>()
----> 1 foo(1)

TypeError: foo() takes 0 positional arguments but 1 was given
In [274]:
def say_hi(name):
    print('hello', name)
In [275]:
'hello {}'.format('Michael')
Out[275]:
'hello Michael'
In [271]:
s = 'hel{}lo{}'
s.format('Michael', 'Jane')
Out[271]:
'helMichaelloJane'
In [272]:
def say_hi(name):
    print('hello {}'.format(name))
In [273]:
say_hi('Michael')
hello Michael
In [276]:
def say_hi(n1, n2):
    print('hello', n1, 'and', n2)
In [277]:
say_hi('Michael', 'Jane')
hello Michael and Jane
In [278]:
say_hi(n1='Michael', n2='Jane')
hello Michael and Jane
In [279]:
say_hi(n2='Michael', n1='Jane')
hello Jane and Michael
In [281]:
args = {'n1': 'Michael', 'n2': 'Jane'}
say_hi(**args) # '**' unpacks args from dictionary
hello Michael and Jane
In [282]:
def say_hi(names):
    for n in names:
        print('hello', n)
In [283]:
say_hi(['Michael', 'Jane', 'Tarzan'])
hello Michael
hello Jane
hello Tarzan
In [284]:
say_hi('Michael', 'Jane', 'Tarzan')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-284-4d9462520e5c> in <module>()
----> 1 say_hi('Michael', 'Jane', 'Tarzan')

TypeError: say_hi() takes 1 positional argument but 3 were given
In [285]:
def say_hi(*names):
    for n in names:
        print('hello', n)
In [286]:
say_hi('Michael', 'Jane', 'Tarzan')
hello Michael
hello Jane
hello Tarzan
In [287]:
say_hi('Jane', 'Tarzan')
hello Jane
hello Tarzan
In [288]:
say_hi()
In [289]:
say_hi
Out[289]:
<function __main__.say_hi>
In [290]:
type(say_hi)
Out[290]:
function
In [291]:
dir(say_hi)
Out[291]:
['__annotations__',
 '__call__',
 '__class__',
 '__closure__',
 '__code__',
 '__defaults__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__get__',
 '__getattribute__',
 '__globals__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__kwdefaults__',
 '__le__',
 '__lt__',
 '__module__',
 '__name__',
 '__ne__',
 '__new__',
 '__qualname__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__']
In [292]:
say_hi()
In [293]:
say_hi.__call__()
In [294]:
say_hi.__call__('Michael', 'Jane')
hello Michael
hello Jane
In [295]:
f = say_hi
In [296]:
f('Joe')
hello Joe

We refer to functions that can be passed around and stored as "first class functions"

In [297]:
sorted(['i', 'like', 'cakes'])
Out[297]:
['cakes', 'i', 'like']
In [298]:
?sorted
In [299]:
sorted(['i', 'like', 'cakes'], reverse=True)
Out[299]:
['like', 'i', 'cakes']
In [301]:
sorted(['i', 'like', 'cakes'], key=len, reverse=True)
Out[301]:
['cakes', 'like', 'i']
In [302]:
coords = [(0, 0), (-5, 0), (1, -100), (2, 2), (50, 50)]
In [303]:
sorted(coords)
Out[303]:
[(-5, 0), (0, 0), (1, -100), (2, 2), (50, 50)]
In [307]:
import math
def dist(coord):
    return math.sqrt(coord[0]**2 + coord[1]**2)
In [309]:
dist((1, 0))
Out[309]:
1.0
In [310]:
dist((1, 1))
Out[310]:
1.4142135623730951
In [311]:
sorted(coords, key=dist)
Out[311]:
[(0, 0), (2, 2), (-5, 0), (50, 50), (1, -100)]
In [312]:
sorted(coords, key=dist, reverse=True)
Out[312]:
[(1, -100), (50, 50), (-5, 0), (2, 2), (0, 0)]
In [313]:
def sum_of_x_and_y(coord):
    return coord[0] + coord[1]
In [314]:
sorted(coords, key=sum_of_x_and_y)
Out[314]:
[(1, -100), (-5, 0), (0, 0), (2, 2), (50, 50)]
In [316]:
f = lambda x: x+1
In [317]:
f(10)
Out[317]:
11
In [318]:
(lambda x: x+1)(10)
Out[318]:
11
In [319]:
(lambda x, y: x+y)(2, 5)
Out[319]:
7
In [320]:
sorted(coords, key=lambda c: c[0]+c[1])
Out[320]:
[(1, -100), (-5, 0), (0, 0), (2, 2), (50, 50)]
In [321]:
coords
Out[321]:
[(0, 0), (-5, 0), (1, -100), (2, 2), (50, 50)]
In [322]:
dist((1, 0))
Out[322]:
1.0
In [323]:
distances = [] # NOT PYTHONIC!
for c in coords:
    distances.append(dist(c))
In [324]:
distances
Out[324]:
[0.0, 5.0, 100.00499987500625, 2.8284271247461903, 70.71067811865476]
In [326]:
[dist(c) for c in coords]
Out[326]:
[0.0, 5.0, 100.00499987500625, 2.8284271247461903, 70.71067811865476]
In [332]:
def mymap(f, lst): # higher order function (HOF) — it takes a function as an argument
    return [f(x) for x in lst]
In [329]:
mymap(dist, coords)
Out[329]:
[0.0, 5.0, 100.00499987500625, 2.8284271247461903, 70.71067811865476]
In [331]:
list(map(dist, coords))
Out[331]:
[0.0, 5.0, 100.00499987500625, 2.8284271247461903, 70.71067811865476]
In [334]:
sorted(peter_pan_word_count.items(), key=lambda t: t[1], reverse=True)
Out[334]:
[('the', 2331),
 ('and', 1396),
 ('to', 1214),
 ('a', 962),
 ('of', 929),
 ('was', 898),
 ('he', 866),
 ('in', 683),
 ('that', 564),
 ('had', 498),
 ('it', 473),
 ('they', 465),
 ('she', 465),
 ('his', 455),
 ('you', 403),
 ('but', 378),
 ('for', 377),
 ('not', 375),
 ('with', 361),
 ('her', 361),
 ('is', 350),
 ('on', 329),
 ('at', 322),
 ('as', 315),
 ('I', 253),
 ('be', 249),
 ('have', 247),
 ('were', 243),
 ('Peter', 238),
 ('all', 221),
 ('said', 218),
 ('their', 215),
 ('would', 211),
 ('Wendy', 200),
 ('so', 197),
 ('this', 195),
 ('are', 188),
 ('him', 186),
 ('by', 175),
 ('one', 170),
 ('them', 165),
 ('He', 163),
 ('“I', 159),
 ('when', 152),
 ('The', 150),
 ('from', 145),
 ('could', 139),
 ('we', 138),
 ('no', 136),
 ('been', 135),
 ('who', 134),
 ('or', 133),
 ('if', 125),
 ('which', 122),
 ('It', 121),
 ('did', 115),
 ('out', 115),
 ('up', 113),
 ('there', 111),
 ('She', 109),
 ('said,', 108),
 ('about', 106),
 ('what', 105),
 ('They', 102),
 ('an', 102),
 ('into', 101),
 ('do', 98),
 ('Darling', 93),
 ('little', 91),
 ('like', 86),
 ('see', 85),
 ('will', 84),
 ('only', 84),
 ('Hook', 84),
 ('time', 81),
 ('your', 80),
 ('Project', 79),
 ('John', 76),
 ('children', 75),
 ('now', 75),
 ('just', 74),
 ('Mrs.', 72),
 ('cried', 72),
 ('came', 71),
 ('thought', 69),
 ('my', 69),
 ('Michael', 69),
 ('other', 69),
 ('must', 68),
 ('any', 68),
 ('then', 67),
 ('him.', 66),
 ('know', 65),
 ('never', 65),
 ('But', 65),
 ('first', 64),
 ('very', 64),
 ('it,', 63),
 ('knew', 63),
 ('quite', 62),
 ('more', 62),
 ('boys', 61),
 ('way', 60),
 ('such', 60),
 ('than', 60),
 ('how', 60),
 ('over', 57),
 ('it.', 56),
 ('go', 56),
 ('course', 55),
 ('don’t', 54),
 ('Wendy,', 54),
 ('him,', 53),
 ('Gutenberg-tm', 53),
 ('down', 51),
 ('still', 51),
 ('always', 50),
 ('come', 50),
 ('can', 50),
 ('long', 50),
 ('Peter,', 50),
 ('may', 49),
 ('made', 49),
 ('last', 49),
 ('because', 49),
 ('went', 48),
 ('moment', 48),
 ('should', 47),
 ('them,', 47),
 ('think', 47),
 ('me', 47),
 ('though', 46),
 ('Mr.', 46),
 ('through', 46),
 ('Then', 46),
 ('after', 45),
 ('really', 44),
 ('let', 44),
 ('am', 44),
 ('There', 43),
 ('heard', 43),
 ('round', 43),
 ('cried,', 42),
 ('even', 42),
 ('where', 42),
 ('rather', 41),
 ('her.', 41),
 ('without', 41),
 ('saw', 41),
 ('put', 40),
 ('most', 40),
 ('once', 40),
 ('before', 40),
 ('found', 40),
 ('has', 40),
 ('every', 39),
 ('get', 39),
 ('“It', 38),
 ('tell', 38),
 ('work', 38),
 ('Of', 37),
 ('her,', 37),
 ('night', 37),
 ('ever', 37),
 ('them.', 37),
 ('pirates', 37),
 ('almost', 36),
 ('You', 36),
 ('two', 36),
 ('gave', 36),
 ('say', 36),
 ('“What', 36),
 ('looking', 35),
 ('look', 35),
 ('us', 35),
 ('upon', 35),
 ('“You', 35),
 ('Chapter', 34),
 ('sat', 34),
 ('Nana', 34),
 ('too', 34),
 ('looked', 33),
 ('mother', 33),
 ('got', 33),
 ('man', 33),
 ('thing', 33),
 ('If', 33),
 ('asked', 33),
 ('again', 33),
 ('In', 33),
 ('told', 33),
 ('This', 32),
 ('hand', 32),
 ('hear', 32),
 ('going', 31),
 ('off', 31),
 ('each', 31),
 ('called', 31),
 ('back', 31),
 ('take', 31),
 ('much', 30),
 ('some', 30),
 ('away', 29),
 ('THE', 29),
 ('things', 29),
 ('sure', 29),
 ('now,', 29),
 ('fell', 29),
 ('under', 28),
 ('For', 28),
 ('might', 28),
 ('its', 28),
 ('sound', 28),
 ('want', 28),
 ('Smee', 28),
 ('And', 27),
 ('Peter.', 27),
 ('said.', 27),
 ('himself', 27),
 ('“And', 27),
 ('make', 27),
 ('Peter’s', 27),
 ('Tinker', 27),
 ('electronic', 27),
 ('give', 26),
 ('soon', 26),
 ('Wendy’s', 26),
 ('being', 26),
 ('light', 26),
 ('these', 26),
 ('our', 26),
 ('Tink', 26),
 ('face', 26),
 ('Slightly', 26),
 ('old', 25),
 ('house', 25),
 ('many', 25),
 ('while', 25),
 ('good', 25),
 ('bed', 25),
 ('shall', 25),
 ('eyes', 25),
 ('To', 25),
 ('home', 25),
 ('another', 24),
 ('“Oh,', 24),
 ('can’t', 24),
 ('took', 24),
 ('three', 24),
 ('As', 24),
 ('well', 24),
 ('you,', 24),
 ('replied', 24),
 ('Wendy.', 24),
 ('Tootles', 24),
 ('seemed', 23),
 ('nursery', 23),
 ('something', 23),
 ('again,', 23),
 ('against', 23),
 ('great', 23),
 ('works', 23),
 ('Gutenberg', 22),
 ('again.', 22),
 ('same', 22),
 ('sometimes', 22),
 ('stood', 22),
 ('felt', 22),
 ('till', 22),
 ('tried', 22),
 ('pirate', 22),
 ('terms', 21),
 ('until', 21),
 ('John,', 21),
 ('part', 21),
 ('“No,', 21),
 ('suddenly', 21),
 ('lost', 21),
 ('flew', 21),
 ('Hook’s', 21),
 ('seen', 20),
 ('room', 20),
 ('“Yes,', 20),
 ('“and', 20),
 ('boy', 20),
 ('window', 20),
 ('new', 20),
 ('help', 20),
 ('save', 20),
 ('use', 19),
 ('A', 19),
 ('except', 19),
 ('own', 19),
 ('“If', 19),
 ('me,', 19),
 ('What', 19),
 ('“The', 19),
 ('others', 19),
 ('fly', 19),
 ('redskins', 19),
 ('up,', 18),
 ('often', 18),
 ('whether', 18),
 ('cried.', 18),
 ('days', 18),
 ('also', 18),
 ('this,', 18),
 ('When', 18),
 ('island', 18),
 ('At', 18),
 ('full', 18),
 ('tree', 18),
 ('head', 18),
 ('out.', 18),
 ('So', 18),
 ('longer', 18),
 ('Now', 18),
 ('Hook,', 18),
 ('All', 17),
 ('ran', 17),
 ('used', 17),
 ('those', 17),
 ('both', 17),
 ('however,', 17),
 ('around', 17),
 ('lay', 17),
 ('Michael,', 17),
 ('began', 17),
 ('“But', 17),
 ('thus', 17),
 ('does', 17),
 ('already', 17),
 ('copyright', 16),
 ('set', 16),
 ('here', 16),
 ('We', 16),
 ('“He', 16),
 ('strange', 16),
 ('best', 16),
 ('real', 16),
 ('flung', 16),
 ('far', 16),
 ('“O', 16),
 ('His', 16),
 ('arms', 16),
 ('Bell', 16),
 ('“She', 16),
 ('terrible', 16),
 ('Nibs', 16),
 ('bird', 16),
 ('one,', 15),
 ('mouth', 15),
 ('out,', 15),
 ('feeling', 15),
 ('find', 15),
 ('lady', 15),
 ('nothing', 15),
 ('him;', 15),
 ('perhaps', 15),
 ('water', 15),
 ('it?”', 15),
 ('indeed', 15),
 ('says', 15),
 ('then,', 15),
 ('“Yes.”', 15),
 ('voice', 15),
 ('“Then', 15),
 ('Hook.', 15),
 ('up.', 14),
 ('lovely', 14),
 ('was,', 14),
 ('whole', 14),
 ('keep', 14),
 ('wanted', 14),
 ('followed', 14),
 ('yet', 14),
 ('flying', 14),
 ('That', 14),
 ('thinking', 14),
 ('time,', 14),
 ('“Oh', 14),
 ('bed.', 14),
 ('“It’s', 14),
 ('“Let', 14),
 ('left', 14),
 ('glad', 14),
 ('“Are', 14),
 ('boys,', 14),
 ('you.”', 14),
 ('answered', 14),
 ('“that', 14),
 ('fairy', 14),
 ('“Do', 14),
 ('liked', 14),
 ('among', 14),
 ('Peter,”', 14),
 ('iron', 14),
 ('hat', 14),
 ('ground', 14),
 ('Starkey', 14),
 ('need', 14),
 ('Foundation', 14),
 ('Pan', 13),
 ('heart', 13),
 ('why', 13),
 ('between', 13),
 ('loved', 13),
 ('ones', 13),
 ('instead', 13),
 ('cut', 13),
 ('nine', 13),
 ('exactly', 13),
 ('lagoon', 13),
 ('large', 13),
 ('sleep', 13),
 ('remember', 13),
 ('near', 13),
 ('hand,', 13),
 ('returned', 13),
 ('leave', 13),
 ('having', 13),
 ('dear', 13),
 ('white', 13),
 ('say,', 13),
 ('gone', 13),
 ('wish', 13),
 ('words', 13),
 ('cannot', 13),
 ('brought', 13),
 ('water,', 13),
 ('end', 13),
 ('whispered', 13),
 ('right', 13),
 ('them;', 13),
 ('pretty', 13),
 ('“Ay,', 13),
 ('Smee,', 13),
 ('Tiger', 13),
 ('Jane', 13),
 ('Literary', 13),
 ('Archive', 13),
 ('By', 12),
 ('passed', 12),
 ('kept', 12),
 ('dropped', 12),
 ('course,', 12),
 ('side', 12),
 ('On', 12),
 ('No', 12),
 ('nice', 12),
 ('turned', 12),
 ('isn’t', 12),
 ('father', 12),
 ('comes', 12),
 ('saying', 12),
 ('answer', 12),
 ('black', 12),
 ('back.', 12),
 ('better', 12),
 ('hands', 12),
 ('“How', 12),
 ('showed', 12),
 ('taken', 12),
 ('kind', 12),
 ('behind', 12),
 ('himself,', 12),
 ('anything', 12),
 ('awfully', 12),
 ('open', 12),
 ('big', 12),
 ('rose', 12),
 ('forgotten', 12),
 ('merely', 12),
 ('see,', 12),
 ('return', 12),
 ('story', 12),
 ('cry', 12),
 ('air', 12),
 ('me.”', 12),
 ('Slightly,', 12),
 ('crocodile', 12),
 ('rock', 12),
 ('day', 11),
 ('one.', 11),
 ('mind', 11),
 ('within', 11),
 ('know,', 11),
 ('drew', 11),
 ('bed,', 11),
 ('whom', 11),
 ('on.', 11),
 ('carried', 11),
 ('doing', 11),
 ('small', 11),
 ('Neverland', 11),
 ('all,', 11),
 ('getting', 11),
 ('least', 11),
 ('meant', 11),
 ('adventures', 11),
 ('believe', 11),
 ('Nana’s', 11),
 ('door', 11),
 ('shadow', 11),
 ('“That', 11),
 ('also,', 11),
 ('rushed', 11),
 ('became', 11),
 ('Even', 11),
 ('“They', 11),
 ('“We', 11),
 ('“I’m', 11),
 ('fear', 11),
 ('few', 11),
 ('“Peter', 11),
 ('Not', 11),
 ('away,', 11),
 ('now.', 11),
 ('With', 11),
 ('across', 11),
 ('Tootles,', 11),
 ('boys.', 11),
 ('person', 11),
 ('forth', 11),
 ('public', 11),
 ('dogs', 11),
 ('sitting', 11),
 ('written', 11),
 ('spring', 11),
 ('donations', 11),
 ('OF', 10),
 ('playing', 10),
 ('Her', 10),
 ('girl', 10),
 ('Darling,', 10),
 ('trying', 10),
 ('able', 10),
 ('frightfully', 10),
 ('making', 10),
 ('carry', 10),
 ('done', 10),
 ('How', 10),
 ('way.', 10),
 ('next', 10),
 ('on,', 10),
 ('night,', 10),
 ('adventure', 10),
 ('grown', 10),
 ('window.', 10),
 ('and,', 10),
 ('hung', 10),
 ('it;', 10),
 ('mother,', 10),
 ('“but', 10),
 ('mean', 10),
 ('hour', 10),
 ('place', 10),
 ('asked,', 10),
 ('fall', 10),
 ('close', 10),
 ('you?”', 10),
 ('floor', 10),
 ('short', 10),
 ('“Don’t', 10),
 ('voice,', 10),
 ('broke', 10),
 ('keeping', 10),
 ('“you', 10),
 ('Tink,', 10),
 ('“Who', 10),
 ('Do', 10),
 ('reached', 10),
 ('dark', 10),
 ('pass', 10),
 ('hook', 10),
 ('Thus', 10),
 ('gay', 10),
 ('Curly', 10),
 ('Never', 10),
 ('turn', 10),
 ('ship', 10),
 ('United', 10),
 ('access', 10),
 ('paragraph', 10),
 ('eBook', 9),
 ('One', 9),
 ('mother.', 9),
 ('six', 9),
 ('so,', 9),
 ('Nana,', 9),
 ('become', 9),
 ('forgot', 9),
 ('coming', 9),
 ('asleep', 9),
 ('draw', 9),
 ('time.', 9),
 ('island,', 9),
 ('less', 9),
 ('there,', 9),
 ('showing', 9),
 ('leaves', 9),
 ('Some', 9),
 ('explained', 9),
 ('“My', 9),
 ('happened', 9),
 ('evening', 9),
 ('mothers', 9),
 ('quickly,', 9),
 ('medicine', 9),
 ('dreadful', 9),
 ('herself', 9),
 ('mother,”', 9),
 ('nearly', 9),
 ('tied', 9),
 ('feel', 9),
 ('know,”', 9),
 ('life', 9),
 ('it,”', 9),
 ('John.', 9),
 ('“Well,', 9),
 ('“the', 9),
 ('inside', 9),
 ('rest', 9),
 ('woke', 9),
 ('“Perhaps', 9),
 ('me!”', 9),
 ('slowly', 9),
 ('fairies', 9),
 ('struck', 9),
 ('silly', 9),
 ('immediately', 9),
 ('birds', 9),
 ('themselves', 9),
 ('“There', 9),
 ('Wendy,”', 9),
 ('alone', 9),
 ('brave', 9),
 ('ground,', 9),
 ('raised', 9),
 ('nest', 9),
 ('agreement', 9),
 ('mother’s', 9),
 ('laws', 9),
 ('form', 9),
 ('James', 8),
 ('copy', 8),
 ('License', 8),
 ('follow', 8),
 ('***', 8),
 ('YOU', 8),
 ('OR', 8),
 ('grow', 8),
 ('beginning', 8),
 ('kiss', 8),
 ('“Now', 8),
 ('ten', 8),
 ('“Of', 8),
 ('half', 8),
 ('watch', 8),
 ('probably', 8),
 ('play', 8),
 ('more.', 8),
 ('another,', 8),
 ('word', 8),
 ('name', 8),
 ('words,', 8),
 ('live', 8),
 ('“it', 8),
 ('met', 8),
 ('foot', 8),
 ('While', 8),
 ('blew', 8),
 ('cry,', 8),
 ('trees', 8),
 ('leapt', 8),
 ('ought', 8),
 ('bad', 8),
 ('call', 8),
 ('love', 8),
 ('wearing', 8),
 ('happy', 8),
 ('ask', 8),
 ('times', 8),
 ('unless', 8),
 ('do,', 8),
 ('knows', 8),
 ('“I’ll', 8),
 ('in.', 8),
 ('silence', 8),
 ('show', 8),
 ('watching', 8),
 ('beds', 8),
 ('way,', 8),
 ('moment,', 8),
 ('shut', 8),
 ('life.', 8),
 ('“Wendy,', 8),
 ('I’ll', 8),
 ('held', 8),
 ('toward', 8),
 ('captain', 8),
 ('back,', 8),
 ('fly.”', 8),
 ('sea', 8),
 ('air,', 8),
 ('bit', 8),
 ('certain', 8),
 ('several', 8),
 ('once,', 8),
 ('spoke', 8),
 ('meet', 8),
 ('pirates,', 8),
 ('cast', 8),
 ('associated', 8),
 ('trees,', 8),
 ('hollow', 8),
 ('claw', 8),
 ('voice.', 8),
 ('waiting', 8),
 ('away.', 8),
 ('lagoon,', 8),
 ('walk', 8),
 ('“No.”', 8),
 ('agree', 8),
 ('Slightly’s', 8),
 ('fee', 8),
 ('including', 8),
 ('THIS', 7),
 ('suppose', 7),
 ('number', 7),
 ('lady,', 7),
 ('sweet', 7),
 ('discovered', 7),
 ('first,', 7),
 ('woman', 7),
 ('Darling’s', 7),
 ('proud', 7),
 ('makes', 7),
 ('question', 7),
 ('down,', 7),
 ('treated', 7),
 ('particular', 7),
 ('knowing', 7),
 ('believed', 7),
 ('floor,', 7),
 ('themselves,', 7),
 ('Michael’s', 7),
 ('straight', 7),
 ('especially', 7),
 ('mother.”', 7),
 ('remembered', 7),
 ('sort', 7),
 ('dead', 7),
 ('Wendy?”', 7),
 ('“why', 7),
 ('grew', 7),
 ('speak', 7),
 ('fire', 7),
 ('lit', 7),
 ('faces', 7),
 ('mouth,', 7),
 ('out;', 7),
 ('hanging', 7),
 ('all.', 7),
 ('true,', 7),
 ('it’s', 7),
 ('won’t', 7),
 ('dear,', 7),
 ('wants', 7),
 ('that.', 7),
 ('wasn’t', 7),
 ('story,', 7),
 ('father,”', 7),
 ('it.”', 7),
 ('ready,', 7),
 ('funny', 7),
 ('whispered,', 7),
 ('house,', 7),
 ('unhappy', 7),
 ('low', 7),
 ('figure', 7),
 ('blown', 7),
 ('stick', 7),
 ('crying', 7),
 ('name.', 7),
 ('I’m', 7),
 ('frightful', 7),
 ('“A', 7),
 ('“Wendy,”', 7),
 ('Peter?”', 7),
 ('people', 7),
 ('run', 7),
 ('“Peter,”', 7),
 ('forward', 7),
 ('that?”', 7),
 ('using', 7),
 ('not.', 7),
 ('“Peter,', 7),
 ('signed', 7),
 ('since', 7),
 ('sight', 7),
 ('right,', 7),
 ('pretend', 7),
 ('smoke', 7),
 ('above', 7),
 ('kill', 7),
 ('known', 7),
 ('fight', 7),
 ('hoped', 7),
 ('present', 7),
 ('Nibs,', 7),
 ('ho,', 7),
 ('to,', 7),
 ('Starkey,', 7),
 ('trees.', 7),
 ('“Hullo,', 7),
 ('feet', 7),
 ('true', 7),
 ('tree.', 7),
 ('lagoon.', 7),
 ('mermaids', 7),
 ('Had', 7),
 ('forget', 7),
 ('feelings', 7),
 ('complain', 7),
 ('hope', 7),
 ('cabin', 7),
 ('free', 7),
 ('paid', 7),
 ('domain', 7),
 ('permission', 7),
 ('copies', 7),
 ('Section', 7),
 ('work,', 7),
 ('posted', 7),
 ('-', 7),
 ('COME', 6),
 ('HOME', 6),
 ('children,', 6),
 ('this.', 6),
 ('lived', 6),
 ('perfectly', 6),
 ('married', 6),
 ('Michael.', 6),
 ('week', 6),
 ('here,', 6),
 ('is,', 6),
 ('year', 6),
 ('time;', 6),
 ('everything', 6),
 ('peeping', 6),
 ('kennel', 6),
 ('talk', 6),
 ('day,', 6),
 ('stand', 6),
 ('other’s', 6),
 ('stories', 6),
 ('sense', 6),
 ('time.”', 6),
 ('dog', 6),
 ('window,', 6),
 ('mother?”', 6),
 ('slept', 6),
 ('Perhaps', 6),
 ('Neverland,', 6),
 ('darted', 6),
 ('boy,', 6),
 ('Again', 6),
 ('ordinary', 6),
 ('telling', 6),
 ('afterwards', 6),
 ('pretended', 6),
 ('master', 6),
 ('thought,', 6),
 ('carrying', 6),
 ('Oh', 6),
 ('in,', 6),
 ('given', 6),
 ('danced', 6),
 ('me,”', 6),
 ('third', 6),
 ('Sometimes', 6),
 ('“Not', 6),
 ('men', 6),
 ('boy.', 6),
 ('him,”', 6),
 ('there.', 6),
 ('Liza', 6),
 ('“Why', 6),
 ('“Look', 6),
 ('you,”', 6),
 ('red', 6),
 ('sorry', 6),
 ('house.”', 6),
 ('“That’s', 6),
 ('right,”', 6),
 ('calling', 6),
 ('dragged', 6),
 ('nor', 6),
 ('asleep,', 6),
 ('fond', 6),
 ('second', 6),
 ('breathing', 6),
 ('“Is', 6),
 ('“Yes,”', 6),
 ('“is', 6),
 ('do.', 6),
 ('man,”', 6),
 ('“if', 6),
 ('listening', 6),
 ('continued,', 6),
 ('touch', 6),
 ('sent', 6),
 ('none', 6),
 ('However,', 6),
 ('“Where', 6),
 ('“To', 6),
 ('go.”', 6),
 ('“Oo!”', 6),
 ('sharp', 6),
 ('right.', 6),
 ('Every', 6),
 ('Here', 6),
 ('Once', 6),
 ('cold', 6),
 ('gaily', 6),
 ('waited', 6),
 ('along', 6),
 ('front', 6),
 ('change', 6),
 ('“There’s', 6),
 ('redskin', 6),
 ('beasts', 6),
 ('make-believe', 6),
 ('other.', 6),
 ('circle', 6),
 ('killed', 6),
 ('twins', 6),
 ('hair', 6),
 ('once.', 6),
 ('Smee.', 6),
 ('caught', 6),
 ('“Ay,”', 6),
 ('listened', 6),
 ('gathered', 6),
 ('Tink,”', 6),
 ('arrow', 6),
 ('step', 6),
 ('Peter.”', 6),
 ('sound,', 6),
 ('best,', 6),
 ('state', 6),
 ('that,', 6),
 ('remained', 6),
 ('After', 6),
 ('parents', 6),
 ('Or', 6),
 ('stayed', 6),
 ('surprise', 6),
 ('innocent', 6),
 ('tree,', 6),
 ('dagger', 6),
 ('board', 6),
 ('Cecco', 6),
 ('cleaning', 6),
 ('Jane.', 6),
 ('distributing', 6),
 ('charge', 6),
 ('distribution', 6),
 ('comply', 6),
 ('agreement,', 6),
 ('refund', 6),
 ('work.', 6),
 ('provide', 6),
 ('tax', 6),
 ('eBooks', 6),
 ('PETER', 5),
 ('remain', 5),
 ('mouth.', 5),
 ('tiny', 5),
 ('other,', 5),
 ('holding', 5),
 ('yes,', 5),
 ('George,”', 5),
 ('went,', 5),
 ('added', 5),
 ('nurse', 5),
 ('hated', 5),
 ('careless', 5),
 ('cry.', 5),
 ('John’s', 5),
 ('school', 5),
 ('status', 5),
 ('allowed', 5),
 ('Pan.', 5),
 ('sight.', 5),
 ('wake', 5),
 ('evil', 5),
 ('map', 5),
 ('mind.', 5),
 ...]
In [336]:
list(filter(lambda wc: wc[0][0].isupper(),
            sorted(peter_pan_word_count.items(), key=lambda t: t[1], reverse=True)))
Out[336]:
[('I', 253),
 ('Peter', 238),
 ('Wendy', 200),
 ('He', 163),
 ('The', 150),
 ('It', 121),
 ('She', 109),
 ('They', 102),
 ('Darling', 93),
 ('Hook', 84),
 ('Project', 79),
 ('John', 76),
 ('Mrs.', 72),
 ('Michael', 69),
 ('But', 65),
 ('Wendy,', 54),
 ('Gutenberg-tm', 53),
 ('Peter,', 50),
 ('Mr.', 46),
 ('Then', 46),
 ('There', 43),
 ('Of', 37),
 ('You', 36),
 ('Chapter', 34),
 ('Nana', 34),
 ('If', 33),
 ('In', 33),
 ('This', 32),
 ('THE', 29),
 ('For', 28),
 ('Smee', 28),
 ('And', 27),
 ('Peter.', 27),
 ('Peter’s', 27),
 ('Tinker', 27),
 ('Wendy’s', 26),
 ('Tink', 26),
 ('Slightly', 26),
 ('To', 25),
 ('As', 24),
 ('Wendy.', 24),
 ('Tootles', 24),
 ('Gutenberg', 22),
 ('John,', 21),
 ('Hook’s', 21),
 ('A', 19),
 ('What', 19),
 ('When', 18),
 ('At', 18),
 ('So', 18),
 ('Now', 18),
 ('Hook,', 18),
 ('All', 17),
 ('Michael,', 17),
 ('We', 16),
 ('His', 16),
 ('Bell', 16),
 ('Nibs', 16),
 ('Hook.', 15),
 ('That', 14),
 ('Peter,”', 14),
 ('Starkey', 14),
 ('Foundation', 14),
 ('Pan', 13),
 ('Smee,', 13),
 ('Tiger', 13),
 ('Jane', 13),
 ('Literary', 13),
 ('Archive', 13),
 ('By', 12),
 ('On', 12),
 ('No', 12),
 ('Slightly,', 12),
 ('Neverland', 11),
 ('Nana’s', 11),
 ('Even', 11),
 ('Not', 11),
 ('With', 11),
 ('Tootles,', 11),
 ('OF', 10),
 ('Her', 10),
 ('Darling,', 10),
 ('How', 10),
 ('Tink,', 10),
 ('Do', 10),
 ('Thus', 10),
 ('Curly', 10),
 ('Never', 10),
 ('United', 10),
 ('One', 9),
 ('Nana,', 9),
 ('Some', 9),
 ('John.', 9),
 ('Wendy,”', 9),
 ('James', 8),
 ('License', 8),
 ('YOU', 8),
 ('OR', 8),
 ('While', 8),
 ('I’ll', 8),
 ('Slightly’s', 8),
 ('THIS', 7),
 ('Darling’s', 7),
 ('Michael’s', 7),
 ('Wendy?”', 7),
 ('I’m', 7),
 ('Peter?”', 7),
 ('Nibs,', 7),
 ('Starkey,', 7),
 ('Had', 7),
 ('Section', 7),
 ('COME', 6),
 ('HOME', 6),
 ('Michael.', 6),
 ('Perhaps', 6),
 ('Neverland,', 6),
 ('Again', 6),
 ('Oh', 6),
 ('Sometimes', 6),
 ('Liza', 6),
 ('However,', 6),
 ('Every', 6),
 ('Here', 6),
 ('Once', 6),
 ('Smee.', 6),
 ('Tink,”', 6),
 ('Peter.”', 6),
 ('After', 6),
 ('Or', 6),
 ('Cecco', 6),
 ('Jane.', 6),
 ('PETER', 5),
 ('George,”', 5),
 ('John’s', 5),
 ('Pan.', 5),
 ('Nana.', 5),
 ('John,”', 5),
 ('Wendy!”', 5),
 ('John?”', 5),
 ('Wendy.”', 5),
 ('Barbecue', 5),
 ('Bell.', 5),
 ('Which', 5),
 ('Curly.', 5),
 ('Was', 5),
 ('Nibs?”', 5),
 ('Marooners’', 5),
 ('Lily', 5),
 ('Jane,', 5),
 ('Information', 5),
 ('Pan,', 4),
 ('M.', 4),
 ('Barrie', 4),
 ('English', 4),
 ('PROJECT', 4),
 ('Such', 4),
 ('Peter;', 4),
 ('Tink’s', 4),
 ('None', 4),
 ('Their', 4),
 ('Long', 4),
 ('Next', 4),
 ('Bill', 4),
 ('Jukes', 4),
 ('These', 4),
 ('Johnny', 4),
 ('Nibs.', 4),
 ('Mother’s', 4),
 ('Describe', 4),
 ('Now,', 4),
 ('Starkey.', 4),
 ('Hook,”', 4),
 ('Saturday', 4),
 ('Class', 4),
 ('Jane’s', 4),
 ('States', 4),
 ('Gutenberg”', 4),
 ('States.', 4),
 ('Foundation,', 4),
 ('GUTENBERG', 3),
 ('PAN', 3),
 ('UNDER', 3),
 ('IN', 3),
 ('Kensington', 3),
 ('Unfortunately', 3),
 ('Certainly', 3),
 ('Look', 3),
 ('Darling.', 3),
 ('I,', 3),
 ('George', 3),
 ('Many', 3),
 ('Alas,', 3),
 ('Father', 3),
 ('Mother', 3),
 ('Still', 3),
 ('O', 3),
 ('Everything', 3),
 ('Instead', 3),
 ('Indeed', 3),
 ('Yes,', 3),
 ('Nothing', 3),
 ('Tom', 3),
 ('Let', 3),
 ('Would', 3),
 ('Cecco,', 3),
 ('Jukes,', 3),
 ('Skylights', 3),
 ('Piccaninny', 3),
 ('Great', 3),
 ('Lily’s', 3),
 ('Captain', 3),
 ('Curly,', 3),
 ('Twice', 3),
 ('Bell,', 3),
 ('I?”', 3),
 ('Then,', 3),
 ('Twin?”', 3),
 ('Nevertheless', 3),
 ('Smee’s', 3),
 ('First', 3),
 ('Nibs.”', 3),
 ('Michael,”', 3),
 ('Tootles.', 3),
 ('Its', 3),
 ('Thursday', 3),
 ('See', 3),
 ('General', 3),
 ('FULL', 3),
 ('LICENSE', 3),
 ('LIMITED', 3),
 ('FOR', 3),
 ('ANY', 3),
 ('Foundation’s', 3),
 ('U.S.', 3),
 ('Web', 3),
 ('EBook', 2),
 ('Please', 2),
 ('Date:', 2),
 ('Last', 2),
 ('EBOOK', 2),
 ('AND', 2),
 ('BREAKS', 2),
 ('THROUGH', 2),
 ('SHADOW', 2),
 ('AWAY,', 2),
 ('AWAY!', 2),
 ('FLIGHT', 2),
 ('ISLAND', 2),
 ('TRUE', 2),
 ('LITTLE', 2),
 ('HOUSE', 2),
 ('GROUND', 2),
 ('LAGOON', 2),
 ('NEVER', 2),
 ('BIRD', 2),
 ('HAPPY', 2),
 ('WENDY’S', 2),
 ('STORY', 2),
 ('CHILDREN', 2),
 ('ARE', 2),
 ('CARRIED', 2),
 ('OFF', 2),
 ('DO', 2),
 ('BELIEVE', 2),
 ('FAIRIES?', 2),
 ('PIRATE', 2),
 ('SHIP', 2),
 ('ME', 2),
 ('TIME”', 2),
 ('RETURN', 2),
 ('WHEN', 2),
 ('WENDY', 2),
 ('GREW', 2),
 ('UP', 2),
 ('Two', 2),
 ('Miss', 2),
 ('Fulsom’s', 2),
 ('Darlings', 2),
 ('Liza,', 2),
 ('Neverlands', 2),
 ('Oh,', 2),
 ('England.', 2),
 ('Friday.', 2),
 ('MEA', 2),
 ('George,', 2),
 ('Michael?”', 2),
 ('Nobody', 2),
 ('Yet', 2),
 ('No.', 2),
 ('Peter!”', 2),
 ('Bell,”', 2),
 ('Moira', 2),
 ('Pan.”', 2),
 ('Besides,', 2),
 ('Fortunately', 2),
 ('Gardens', 2),
 ('Tink.', 2),
 ('Tink?”', 2),
 ('Cinderella,', 2),
 ('No,', 2),
 ('Bring', 2),
 ('Why,', 2),
 ('Will', 2),
 ('Neverland;', 2),
 ('Did', 2),
 ('Strange', 2),
 ('That’s', 2),
 ('Hook?”', 2),
 ('Presently', 2),
 ('Fairies', 2),
 ('Feeling', 2),
 ('Poor', 2),
 ('Take', 2),
 ('Italian', 2),
 ('Flint', 2),
 ('Irish', 2),
 ('Noodler,', 2),
 ('Mullins', 2),
 ('Alf', 2),
 ('Spanish', 2),
 ('Jas.', 2),
 ('Sea-Cook', 2),
 ('Little', 2),
 ('Panther,', 2),
 ('Piccaninnies,', 2),
 ('Cinderella.”', 2),
 ('Davy', 2),
 ('Mermaids’', 2),
 ('Where', 2),
 ('Neverland.', 2),
 ('Any', 2),
 ('Usually', 2),
 ('Well,', 2),
 ('Only', 2),
 ('Tootles?”', 2),
 ('Rock,', 2),
 ('Rock', 2),
 ('Lily.', 2),
 ('An', 2),
 ('JOLLY', 2),
 ('Each', 2),
 ('Soon', 2),
 ('Already', 2),
 ('Night', 2),
 ('Above,', 2),
 ('Ah,', 2),
 ('Bell?”', 2),
 ('Through', 2),
 ('From', 2),
 ('Most', 2),
 ('Feared', 2),
 ('Slightly?”', 2),
 ('We’ll', 2),
 ('Wendy;', 2),
 ('Very', 2),
 ('Thus,', 2),
 ('Without', 2),
 ('Jukes,”', 2),
 ('Hook;', 2),
 ('Cecco?”', 2),
 ('Slightly.', 2),
 ('Starkey,”', 2),
 ('Cookson', 2),
 ('Mullins,', 2),
 ('Pan,”', 2),
 ('Something', 2),
 ('Margaret', 2),
 ('END', 2),
 ('Terms', 2),
 ('Use', 2),
 ('States,', 2),
 ('License.', 2),
 ('Vanilla', 2),
 ('ASCII”', 2),
 ('Royalty', 2),
 ('DAMAGES', 2),
 ('Except', 2),
 ('AGREE', 2),
 ('THAT', 2),
 ('NO', 2),
 ('BREACH', 2),
 ('NOT', 2),
 ('TO', 2),
 ('WARRANTIES', 2),
 ('Dr.', 2),
 ('S.', 2),
 ('Donations', 2),
 ('EBooks', 2),
 ('November', 2),
 ('COPYRIGHTED', 1),
 ('Details', 1),
 ('Below', 1),
 ('Title:', 1),
 ('Author:', 1),
 ('Posting', 1),
 ('June', 1),
 ('Release', 1),
 ('July,', 1),
 ('Updated:', 1),
 ('October', 1),
 ('Language:', 1),
 ('Character', 1),
 ('UTF-8', 1),
 ('START', 1),
 ('WENDY]', 1),
 ('J.', 1),
 ('Matthew', 1),
 ('Barrie]', 1),
 ('Millennium', 1),
 ('Fulcrum', 1),
 ('Edition', 1),
 ('Duncan', 1),
 ('Research', 1),
 ('Contents:', 1),
 ('MERMAID’S', 1),
 ('East,', 1),
 ('Napoleon', 1),
 ('Brussels', 1),
 ('German', 1),
 ('Kindergarten', 1),
 ('Newfoundland', 1),
 ('Gardens,', 1),
 ('England', 1),
 ('Lovely', 1),
 ('Doctors', 1),
 ('John’s,', 1),
 ('Occasionally', 1),
 ('Leave', 1),
 ('Children', 1),
 ('But,', 1),
 ('Ah', 1),
 ('Friday,”', 1),
 ('CULPA,', 1),
 ('CULPA.”', 1),
 ('Friday,', 1),
 ('George.”', 1),
 ('He,', 1),
 ('Round', 1),
 ('George?”', 1),
 ('Strong', 1),
 ('Michael.”', 1),
 ('Immediately', 1),
 ('It’s', 1),
 ('I--I', 1),
 ('Nana.”', 1),
 ('Somehow', 1),
 ('Danger!', 1),
 ('Stars', 1),
 ('Milky', 1),
 ('Way', 1),
 ('EMBONPOINT.', 1),
 ('Angela', 1),
 ('Darling,”', 1),
 ('Angela.', 1),
 ('Kings', 1),
 ('Still,', 1),
 ('Tedious', 1),
 ('Isn’t', 1),
 ('Really,', 1),
 ('Cinderella]', 1),
 ('Those', 1),
 ('Think', 1),
 ('Hide!', 1),
 ('Quick!”', 1),
 ('Christmas', 1),
 ('Listen', 1),
 ('Liza’s', 1),
 ('Nana,”', 1),
 ('Z.', 1),
 ('Fortunately,', 1),
 ('Up', 1),
 ('Heavenly', 1),
 ('Sunday', 1),
 ('That,', 1),
 ('Eventually', 1),
 ('Also', 1),
 ('Leader.”', 1),
 ('Follow', 1),
 ('Leader,', 1),
 ('Indeed,', 1),
 ('Wendy,’', 1),
 ('Show', 1),
 ('Mysterious', 1),
 ('River.”', 1),
 ('Nana?', 1),
 ('Having', 1),
 ('Blackbeard’s', 1),
 ('Is', 1),
 ('Twins,', 1),
 ('A-pirating', 1),
 ('We’re', 1),
 ('Execution', 1),
 ('Here,', 1),
 ('Gao.', 1),
 ('Guadjo-mo.', 1),
 ('WALRUS', 1),
 ('Cookson,', 1),
 ('Black', 1),
 ('Murphy’s', 1),
 ('Gentleman', 1),
 ('Skylights);', 1),
 ('Non-conformist', 1),
 ('Robt.', 1),
 ('Mason', 1),
 ('Main.', 1),
 ('RACONTEUR', 1),
 ('Charles', 1),
 ('II,', 1),
 ('Stuarts;', 1),
 ('Strung', 1),
 ('Delawares', 1),
 ('Hurons.', 1),
 ('Big', 1),
 ('Bringing', 1),
 ('Lily,', 1),
 ('Dianas', 1),
 ('Observe', 1),
 ('Jones.”', 1),
 ('Rabbits', 1),
 ('Captain,”', 1),
 ('Corkscrew?”', 1),
 ('Corkscrew,', 1),
 ('Smee,”', 1),
 ('Scatter', 1),
 ('Anon', 1),
 ('Since', 1),
 ('Stranger', 1),
 ('Pan’s', 1),
 ('Corkscrew.', 1),
 ('Lagoon.', 1),
 ('Nought’s', 1),
 ('Have', 1),
 ('Hook.”', 1),
 ('Tick', 1),
 ('Almost', 1),
 ('Wendy.’”', 1),
 ('Wendies.”', 1),
 ('Foolish', 1),
 ('Overhead', 1),
 ('Tootles’', 1),
 ('Wonderful', 1),
 ('Tootles,’”', 1),
 ('Ay,', 1),
 ('Begone', 1),
 ('Gut', 1),
 ('Be', 1),
 ('Away', 1),
 ('Immediately,', 1),
 ('Gay', 1),
 ('Quickly', 1),
 ('Babies?', 1),
 ('Just', 1),
 ('Absolutely', 1),
 ('Come', 1),
 ('Queen', 1),
 ('Mab,', 1),
 ('Puss-in-Boots,', 1),
 ('Pie-crust', 1),
 ('Charming', 1),
 ('Sixth,', 1),
 ('Margery', 1),
 ('Robin.', 1),
 ('Tiddlywinks', 1),
 ('Really', 1),
 ('Make-believe', 1),
 ('Mother?', 1),
 ('Answer', 1),
 ('Write', 1),
 ('Holidays,', 1),
 ('Characters', 1),
 ('Father’s', 1),
 ('Party', 1),
 ('Dress;', 1),
 ('Kennel', 1),
 ('Inmate.”', 1),
 ('Adventures,', 1),
 ('English-Latin,', 1),
 ('Latin-English', 1),
 ('Dictionary,', 1),
 ('Should', 1),
 ('Gulch?', 1),
 ('Gulch,', 1),
 ('Lagoon,', 1),
 ('Bell’s', 1),
 ('MERMAIDS’', 1),
 ('Rock.', 1),
 ('So,', 1),
 ('Wendy?', 1),
 ('Smee’s;', 1),
 ('Quite', 1),
 ('Wendy’s.', 1),
 ('Lily:', 1),
 ('Starkey’s', 1),
 ('Affrighted', 1),
 ('No.”', 1),
 ('Swear.”', 1),
 ('Speak!”', 1),
 ('ROGER.”', 1),
 ('Against', 1),
 ('Suddenly', 1),
 ('England?”', 1),
 ('Pan!', 1),
 ('Farther', 1),
 ('Peter?', 1),
 ('Strangely,', 1),
 ('Neither', 1),
 ('Quick', 1),
 ('Wendy”', 1),
 ('Pale', 1),
 ('Steadily', 1),
 ('Bird’s', 1),
 ('White', 1),
 ('Father,', 1),
 ('Lily,”', 1),
 ('Me', 1),
 ('Always', 1),
 ('Secretly', 1),
 ('Nights,', 1),
 ('Nibs,”', 1),
 ('John!”', 1),
 ('Tootles.”', 1),
 ('Curly.”', 1),
 ('My', 1),
 ('Fancy', 1),
 ('Twins?”', 1),
 ('Darling.”', 1),
 ('Twin.”', 1),
 ('John?', 1),
 ('Nana;', 1),
 ('Hurrah,', 1),
 ('London', 1),
 ('Station?”', 1),
 ('Can', 1),
 ('Michael?', 1),
 ('Off', 1),
 ('Panic-stricken', 1),
 ('Grandly,', 1),
 ('Tootles,”', 1),
 ('Wake', 1),
 ('Go', 1),
 ('Novelty', 1),
 ('Crediting', 1),
 ('Thursdays.”', 1),
 ('Peter--”', 1),
 ('Wendy;”', 1),
 ('Below,', 1),
 ('Mouths', 1),
 ('Around', 1),
 ('Fell', 1),
 ('Lean', 1),
 ('Wolf', 1),
 ('Mason,', 1),
 ('Main', 1),
 ('Geo.', 1),
 ('Scourie,', 1),
 ('Chas.', 1),
 ('Turley,', 1),
 ('Alsatian', 1),
 ('Foggerty.', 1),
 ('Turley', 1),
 ('Fain', 1),
 ('Elation', 1),
 ('True', 1),
 ('Never,', 1),
 ('Indian', 1),
 ('Rapidly', 1),
 ('DISTINGUE', 1),
 ('Madly', 1),
 ('Sufficient', 1),
 ('Hunched', 1),
 ('Dark', 1),
 ('Intently', 1),
 ('Unaware', 1),
 ('Sometimes,', 1),
 ('Mastered', 1),
 ('Though', 1),
 ('Lest', 1),
 ('Five', 1),
 ('Donning', 1),
 ('Soft', 1),
 ('Unlike', 1),
 ('Who', 1),
 ('Nature', 1),
 ('Kidd’s', 1),
 ('Creek,', 1),
 ('ROGER,', 1),
 ('Good', 1),
 ('However', 1),
 ('Barbecue.”', 1),
 ('Flint--what', 1),
 ('Ofttimes', 1),
 ('Smee!', 1),
 ('Instead,', 1),
 ('Pop', 1),
 ('Eton].', 1),
 ('Ever', 1),
 ('Didst', 1),
 ('Red-handed', 1),
 ('Jack,”', 1),
 ('Joe.”', 1),
 ('King?”', 1),
 ('King.’”', 1),
 ('Britannia!”', 1),
 ('Get', 1),
 ('Fine', 1),
 ('Left', 1),
 ('Fate.', 1),
 ('Nights;', 1),
 ('Odd', 1),
 ('Ed', 1),
 ('Teynte', 1),
 ('Four', 1),
 ('Slowly', 1),
 ('Plank!”', 1),
 ('Till', 1),
 ('Jones', 1),
 ('Seizing', 1),
 ('Mullins.', 1),
 ('Noodler.', 1),
 ('Open', 1),
 ('There’s', 1),
 ('Jonah', 1),
 ('Flint’s.', 1),
 ('Man', 1),
 ('Rio;', 1),
 ('Hitherto', 1),
 ('This,', 1),
 ('Abandoning', 1),
 ('Misguided', 1),
 ('Seeing', 1),
 ('Fifteen', 1),
 ('Tom.', 1),
 ('Rio', 1),
 ('Gold', 1),
 ('Coast,', 1),
 ('Azores', 1),
 ('June,', 1),
 ('Cpt.', 1),
 ('Hook].', 1),
 ('Instant', 1),
 ('Why', 1),
 ('Inwardly', 1),
 ('Quixotic,', 1),
 ('Crowds', 1),
 ('George’s', 1),
 ('Suppose,', 1),
 ('Let’s.', 1),
 ('Outside,', 1),
 ('Liza.', 1),
 ('Social', 1),
 ('George?', 1),
 ('Sweet', 1),
 ('Home,”', 1),
 ('Wendy”;', 1),
 ('I!”', 1),
 ('George!”', 1),
 ('Because,', 1),
 ('Curly?”', 1),
 ('Twin,', 1),
 ('Hoop', 1),
 ('Tink.”', 1),
 ('Funny.', 1),
 ('III,', 1),
 ('IV', 1),
 ('V.', 1),
 ('Before', 1),
 ('Jenkins', 1),
 ('Jenkins].', 1),
 ('Want', 1),
 ('Years', 1),
 ('I.', 1),
 ('Woman,', 1),
 ('Neverland.”', 1),
 ('I.”', 1),
 ('Our', 1),
 ('Margaret;', 1),
 ('End', 1),
 ('Updated', 1),
 ('Creating', 1),
 ('Special', 1),
 ('GUTENBERG-tm', 1),
 ('ANYTHING', 1),
 ('Redistribution', 1),
 ('START:', 1),
 ('PLEASE', 1),
 ('READ', 1),
 ('BEFORE', 1),
 ('DISTRIBUTE', 1),
 ('USE', 1),
 ('WORK', 1),
 ('Gutenberg”),', 1),
 ('Full', 1),
 ('Redistributing', 1),
 ('Foundation”', 1),
 ('PGLAF),', 1),
 ('Nearly', 1),
 ('Copyright', 1),
 ('Unless', 1),
 ('Gutenberg:', 1),
 ('Additional', 1),
 ('Gutenberg-tm.', 1),
 ('Foundation.', 1),
 ('Foundation.”', 1),
 ('Hart,', 1),
 ('Contact', 1),
 ('Despite', 1),
 ('WARRANTY,', 1),
 ('DISCLAIMER', 1),
 ('Replacement', 1),
 ('Refund”', 1),
 ('HAVE', 1),
 ('REMEDIES', 1),
 ('NEGLIGENCE,', 1),
 ('STRICT', 1),
 ('LIABILITY,', 1),
 ('WARRANTY', 1),
 ('CONTRACT', 1),
 ('EXCEPT', 1),
 ('THOSE', 1),
 ('PROVIDED', 1),
 ('PARAGRAPH', 1),
 ('F3.', 1),
 ('FOUNDATION,', 1),
 ('TRADEMARK', 1),
 ('OWNER,', 1),
 ('DISTRIBUTOR', 1),
 ('AGREEMENT', 1),
 ('WILL', 1),
 ('BE', 1),
 ('LIABLE', 1),
 ('ACTUAL,', 1),
 ('DIRECT,', 1),
 ('INDIRECT,', 1),
 ('CONSEQUENTIAL,', 1),
 ('PUNITIVE', 1),
 ('INCIDENTAL', 1),
 ('EVEN', 1),
 ('IF', 1),
 ('GIVE', 1),
 ('NOTICE', 1),
 ('POSSIBILITY', 1),
 ('SUCH', 1),
 ('DAMAGE.', 1),
 ('RIGHT', 1),
 ('REPLACEMENT', 1),
 ('REFUND', 1),
 ('WITH', 1),
 ('OTHER', 1),
 ('KIND,', 1),
 ('EXPRESS', 1),
 ('IMPLIED,', 1),
 ('INCLUDING', 1),
 ('BUT', 1),
 ('MERCHANTIBILITY', 1),
 ('FITNESS', 1),
 ('PURPOSE.', 1),
 ('INDEMNITY', 1),
 ('Defect', 1),
 ('Mission', 1),
 ('Volunteers', 1),
 ('Gutenberg-tm’s', 1),
 ('Sections', 1),
 ('Mississippi', 1),
 ('Internal', 1),
 ('Revenue', 1),
 ('Service.', 1),
 ('EIN', 1),
 ('Contributions', 1),
 ('Melan', 1),
 ('Fairbanks,', 1),
 ('AK,', 1),
 ('North', 1),
 ('West,', 1),
 ('Salt', 1),
 ('Lake', 1),
 ('City,', 1),
 ('UT', 1),
 ('Email', 1),
 ('Gregory', 1),
 ('B.', 1),
 ('Newby', 1),
 ('Chief', 1),
 ('Executive', 1),
 ('Director', 1),
 ('IRS.', 1),
 ('Compliance', 1),
 ('SEND', 1),
 ('DONATIONS', 1),
 ('International', 1),
 ('About', 1),
 ('Professor', 1),
 ('Hart', 1),
 ('Public', 1),
 ('Domain', 1),
 ('ASCII,', 1),
 ('HTML', 1),
 ('Corrected', 1),
 ('EDITIONS', 1),
 ('VERSIONS', 1),
 ('PG', 1),
 ('Gutenberg-tm,', 1),
 ('BELOW', 1),
 ('OVER', 1),
 ('END:', 1)]
In [337]:
f = lambda x: x + 1
In [338]:
f(10)
Out[338]:
11
In [341]:
def foo(x):
    x += 1
    return (lambda y : y + x) # "closure": "closes over" all variables it uses --- keeps them around
In [342]:
f = foo(10)
In [343]:
f(20)
Out[343]:
31
In [344]:
f(30)
Out[344]:
41
In [345]:
g = foo(100)
In [346]:
g(10)
Out[346]:
111
In [347]:
f(10)
Out[347]:
21
In [348]:
def foo(a):
    return a + 10
In [349]:
foo(100)
Out[349]:
110
In [350]:
foo()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-350-624891b0d01a> in <module>()
----> 1 foo()

TypeError: foo() missing 1 required positional argument: 'a'
In [351]:
def foo(a=-10):
    return a + 10
In [362]:
foo()
Out[362]:
[5, 5]
In [353]:
foo(100)
Out[353]:
110
In [368]:
def foo(l="hello"):
    return l
In [369]:
foo()
Out[369]:
'hello'
In [370]:
s = foo()
In [371]:
s
Out[371]:
'hello'
In [373]:
s = 'goodbye'
In [374]:
foo()
Out[374]:
'hello'

Classes

In [375]:
class Foo:
    pass
In [376]:
Foo()
Out[376]:
<__main__.Foo at 0x104941828>
In [377]:
f = Foo()
In [378]:
f.attrib
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-378-8d612db652e4> in <module>()
----> 1 f.attrib

AttributeError: 'Foo' object has no attribute 'attrib'
In [379]:
f.attrib = 10
In [380]:
f.attrib
Out[380]:
10
In [381]:
f.x = f.y = f.z = 100
In [382]:
f.x, f.y, f.z
Out[382]:
(100, 100, 100)
In [383]:
dir(f)
Out[383]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'attrib',
 'x',
 'y',
 'z']
In [384]:
dir(Foo())
Out[384]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__']
In [385]:
Foo.w = 100
In [386]:
Foo
Out[386]:
__main__.Foo
In [387]:
f.w
Out[387]:
100
In [388]:
f.w = 150
In [389]:
f.w
Out[389]:
150
In [390]:
Foo.w
Out[390]:
100
In [391]:
Foo.w = 170
In [392]:
f.w
Out[392]:
150
In [393]:
class Foo:
    def bar():
        pass
In [394]:
f = Foo()
In [395]:
f.bar()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-395-30fa523d4af0> in <module>()
----> 1 f.bar()

TypeError: bar() takes 0 positional arguments but 1 was given
In [396]:
Foo.bar()
In [397]:
class Foo:
    def bar(x):
        print(x)
In [398]:
f = Foo()
In [399]:
f.bar()
<__main__.Foo object at 0x104c2dbe0>
In [400]:
f
Out[400]:
<__main__.Foo at 0x104c2dbe0>
In [401]:
Foo.bar()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-401-bb3335dac614> in <module>()
----> 1 Foo.bar()

TypeError: bar() missing 1 required positional argument: 'x'
In [402]:
Foo.bar(f) # equiv to f.bar()
<__main__.Foo object at 0x104c2dbe0>
In [428]:
class Foo:
    def bar(self):
        try:
            self.x += 1
        except AttributeError:
            self.x = 1
        return self.x
In [429]:
f = Foo()
In [441]:
f.bar()
Out[441]:
12
In [442]:
class Shape:
    def __init__(self):
        print('I got constructed')
In [443]:
Shape()
I got constructed
Out[443]:
<__main__.Shape at 0x104c40e10>
In [444]:
class Shape:
    def __init__(self, name):
        self.name = name
In [445]:
s = Shape('Circle')
In [446]:
s.name
Out[446]:
'Circle'
In [447]:
s
Out[447]:
<__main__.Shape at 0x104c42400>
In [457]:
class Shape:
    def __init__(self, name):
        self.name = name
        
    def __repr__(self):
        return self.name
    
    def __str__(self):
        return self.name.upper()
In [458]:
Shape('Circle')
Out[458]:
Circle
In [459]:
str(Shape('Circle'))
Out[459]:
'CIRCLE'
In [460]:
print(Shape('Circle'))
CIRCLE
In [463]:
class Shape:
    def __init__(self, name):
        self.name = name
        
    def __repr__(self):
        return self.name
    
    def __str__(self):
        return self.name.upper()
    
    def area(self):
        raise NotImplementedError()
In [465]:
Shape('Circle').area()
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-465-ea85454f7b71> in <module>()
----> 1 Shape('Circle').area()

<ipython-input-463-6769065d7b69> in area(self)
     10 
     11     def area(self):
---> 12         raise NotImplementedError()

NotImplementedError: 
In [469]:
class Circle(Shape):
    def __init__(self, radius):
        super().__init__('Circle')
        self.radius = radius
    
    def area(self):
        return 3.14 * self.radius ** 2
In [470]:
c = Circle(10)
In [471]:
c.name
Out[471]:
'Circle'
In [472]:
c.area()
Out[472]:
314.0
In [473]:
Circle(10.0) == Circle(10.0)
Out[473]:
False
In [474]:
id(Circle(10.0))
Out[474]:
4374930600
In [489]:
class Circle(Shape):
    def __init__(self, radius):
        super().__init__('Circle')
        self.radius = radius
    
    def area(self):
        return 3.14 * self.radius ** 2
    
    def __eq__(self, other):
        return isinstance(other, Circle) and self.radius == other.radius
    
    def __add__(self, other):
        return Circle(self.radius + other.radius)
    
    def __repr__(self):
        return 'Circle(r={})'.format(self.radius)
    
    @staticmethod
    def whatever():
        print('hello from a static circle method')
In [490]:
Circle.whatever()
hello from a static circle method
In [492]:
c = Circle(10.0)
In [493]:
c.whatever()
hello from a static circle method
In [476]:
Circle(10.0)
Out[476]:
Circle(r=10.0)
In [484]:
Circle(10) == Circle(10)
Out[484]:
True
In [480]:
Circle(10).__eq__(Circle(10))
Out[480]:
True
In [481]:
Circle(5) + Circle(10)
Out[481]:
Circle(r=15)
In [485]:
Circle(10) == Shape(5)
Out[485]:
False
In [486]:
cs = [Circle(r) for r in range(1,10)]
In [487]:
cs
Out[487]:
[Circle(r=1),
 Circle(r=2),
 Circle(r=3),
 Circle(r=4),
 Circle(r=5),
 Circle(r=6),
 Circle(r=7),
 Circle(r=8),
 Circle(r=9)]
In [488]:
[c.area() for c in cs]
Out[488]:
[3.14, 12.56, 28.26, 50.24, 78.5, 113.04, 153.86, 200.96, 254.34]

Modules

In [496]:
if __name__ == '__main__':
    print('hi!')
hi!
In [497]:
dir()
Out[497]:
['Circle',
 'Foo',
 'In',
 'Out',
 'Shape',
 '_',
 '_10',
 '_100',
 '_102',
 '_104',
 '_107',
 '_110',
 '_112',
 '_114',
 '_118',
 '_12',
 '_120',
 '_121',
 '_123',
 '_125',
 '_127',
 '_128',
 '_129',
 '_13',
 '_130',
 '_131',
 '_132',
 '_134',
 '_135',
 '_136',
 '_138',
 '_139',
 '_14',
 '_141',
 '_143',
 '_145',
 '_147',
 '_148',
 '_15',
 '_150',
 '_153',
 '_155',
 '_156',
 '_158',
 '_159',
 '_16',
 '_160',
 '_161',
 '_162',
 '_163',
 '_164',
 '_165',
 '_166',
 '_167',
 '_168',
 '_169',
 '_170',
 '_171',
 '_173',
 '_174',
 '_180',
 '_181',
 '_183',
 '_184',
 '_185',
 '_186',
 '_189',
 '_190',
 '_192',
 '_193',
 '_195',
 '_197',
 '_198',
 '_2',
 '_200',
 '_202',
 '_204',
 '_206',
 '_208',
 '_209',
 '_211',
 '_212',
 '_214',
 '_216',
 '_218',
 '_219',
 '_220',
 '_221',
 '_222',
 '_229',
 '_230',
 '_231',
 '_233',
 '_235',
 '_237',
 '_238',
 '_239',
 '_240',
 '_241',
 '_244',
 '_245',
 '_246',
 '_248',
 '_249',
 '_250',
 '_252',
 '_253',
 '_254',
 '_255',
 '_266',
 '_267',
 '_268',
 '_269',
 '_270',
 '_271',
 '_275',
 '_28',
 '_289',
 '_290',
 '_291',
 '_297',
 '_299',
 '_3',
 '_30',
 '_300',
 '_301',
 '_303',
 '_304',
 '_305',
 '_306',
 '_309',
 '_310',
 '_311',
 '_312',
 '_314',
 '_315',
 '_317',
 '_318',
 '_319',
 '_320',
 '_321',
 '_322',
 '_324',
 '_325',
 '_326',
 '_328',
 '_329',
 '_33',
 '_330',
 '_331',
 '_333',
 '_334',
 '_335',
 '_336',
 '_338',
 '_34',
 '_340',
 '_343',
 '_344',
 '_346',
 '_347',
 '_349',
 '_35',
 '_352',
 '_353',
 '_355',
 '_356',
 '_357',
 '_36',
 '_361',
 '_362',
 '_365',
 '_367',
 '_369',
 '_37',
 '_371',
 '_374',
 '_376',
 '_38',
 '_380',
 '_382',
 '_383',
 '_384',
 '_386',
 '_387',
 '_389',
 '_39',
 '_390',
 '_392',
 '_4',
 '_400',
 '_41',
 '_415',
 '_416',
 '_420',
 '_43',
 '_430',
 '_431',
 '_432',
 '_433',
 '_434',
 '_435',
 '_436',
 '_437',
 '_438',
 '_439',
 '_44',
 '_440',
 '_441',
 '_443',
 '_446',
 '_447',
 '_449',
 '_45',
 '_450',
 '_451',
 '_454',
 '_455',
 '_458',
 '_459',
 '_468',
 '_47',
 '_471',
 '_472',
 '_473',
 '_474',
 '_476',
 '_477',
 '_478',
 '_480',
 '_481',
 '_484',
 '_485',
 '_487',
 '_488',
 '_495',
 '_5',
 '_51',
 '_54',
 '_55',
 '_56',
 '_58',
 '_59',
 '_6',
 '_62',
 '_66',
 '_68',
 '_69',
 '_7',
 '_72',
 '_74',
 '_76',
 '_77',
 '_78',
 '_79',
 '_8',
 '_80',
 '_82',
 '_84',
 '_86',
 '_88',
 '_89',
 '_9',
 '_90',
 '_91',
 '_92',
 '_93',
 '_94',
 '_95',
 '_96',
 '_97',
 '_98',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_i',
 '_i1',
 '_i10',
 '_i100',
 '_i101',
 '_i102',
 '_i103',
 '_i104',
 '_i105',
 '_i106',
 '_i107',
 '_i108',
 '_i109',
 '_i11',
 '_i110',
 '_i111',
 '_i112',
 '_i113',
 '_i114',
 '_i115',
 '_i116',
 '_i117',
 '_i118',
 '_i119',
 '_i12',
 '_i120',
 '_i121',
 '_i122',
 '_i123',
 '_i124',
 '_i125',
 '_i126',
 '_i127',
 '_i128',
 '_i129',
 '_i13',
 '_i130',
 '_i131',
 '_i132',
 '_i133',
 '_i134',
 '_i135',
 '_i136',
 '_i137',
 '_i138',
 '_i139',
 '_i14',
 '_i140',
 '_i141',
 '_i142',
 '_i143',
 '_i144',
 '_i145',
 '_i146',
 '_i147',
 '_i148',
 '_i149',
 '_i15',
 '_i150',
 '_i151',
 '_i152',
 '_i153',
 '_i154',
 '_i155',
 '_i156',
 '_i157',
 '_i158',
 '_i159',
 '_i16',
 '_i160',
 '_i161',
 '_i162',
 '_i163',
 '_i164',
 '_i165',
 '_i166',
 '_i167',
 '_i168',
 '_i169',
 '_i17',
 '_i170',
 '_i171',
 '_i172',
 '_i173',
 '_i174',
 '_i175',
 '_i176',
 '_i177',
 '_i178',
 '_i179',
 '_i18',
 '_i180',
 '_i181',
 '_i182',
 '_i183',
 '_i184',
 '_i185',
 '_i186',
 '_i187',
 '_i188',
 '_i189',
 '_i19',
 '_i190',
 '_i191',
 '_i192',
 '_i193',
 '_i194',
 '_i195',
 '_i196',
 '_i197',
 '_i198',
 '_i199',
 '_i2',
 '_i20',
 '_i200',
 '_i201',
 '_i202',
 '_i203',
 '_i204',
 '_i205',
 '_i206',
 '_i207',
 '_i208',
 '_i209',
 '_i21',
 '_i210',
 '_i211',
 '_i212',
 '_i213',
 '_i214',
 '_i215',
 '_i216',
 '_i217',
 '_i218',
 '_i219',
 '_i22',
 '_i220',
 '_i221',
 '_i222',
 '_i223',
 '_i224',
 '_i225',
 '_i226',
 '_i227',
 '_i228',
 '_i229',
 '_i23',
 '_i230',
 '_i231',
 '_i232',
 '_i233',
 '_i234',
 '_i235',
 '_i236',
 '_i237',
 '_i238',
 '_i239',
 '_i24',
 '_i240',
 '_i241',
 '_i242',
 '_i243',
 '_i244',
 '_i245',
 '_i246',
 '_i247',
 '_i248',
 '_i249',
 '_i25',
 '_i250',
 '_i251',
 '_i252',
 '_i253',
 '_i254',
 '_i255',
 '_i256',
 '_i257',
 '_i258',
 '_i259',
 '_i26',
 '_i260',
 '_i261',
 '_i262',
 '_i263',
 '_i264',
 '_i265',
 '_i266',
 '_i267',
 '_i268',
 '_i269',
 '_i27',
 '_i270',
 '_i271',
 '_i272',
 '_i273',
 '_i274',
 '_i275',
 '_i276',
 '_i277',
 '_i278',
 '_i279',
 '_i28',
 '_i280',
 '_i281',
 '_i282',
 '_i283',
 '_i284',
 '_i285',
 '_i286',
 '_i287',
 '_i288',
 '_i289',
 '_i29',
 '_i290',
 '_i291',
 '_i292',
 '_i293',
 '_i294',
 '_i295',
 '_i296',
 '_i297',
 '_i298',
 '_i299',
 '_i3',
 '_i30',
 '_i300',
 '_i301',
 '_i302',
 '_i303',
 '_i304',
 '_i305',
 '_i306',
 '_i307',
 '_i308',
 '_i309',
 '_i31',
 '_i310',
 '_i311',
 '_i312',
 '_i313',
 '_i314',
 '_i315',
 '_i316',
 '_i317',
 '_i318',
 '_i319',
 '_i32',
 '_i320',
 '_i321',
 '_i322',
 '_i323',
 '_i324',
 '_i325',
 '_i326',
 '_i327',
 '_i328',
 '_i329',
 '_i33',
 '_i330',
 '_i331',
 '_i332',
 '_i333',
 '_i334',
 '_i335',
 '_i336',
 '_i337',
 '_i338',
 '_i339',
 '_i34',
 '_i340',
 '_i341',
 '_i342',
 '_i343',
 '_i344',
 '_i345',
 '_i346',
 '_i347',
 '_i348',
 '_i349',
 '_i35',
 '_i350',
 '_i351',
 '_i352',
 '_i353',
 '_i354',
 '_i355',
 '_i356',
 '_i357',
 '_i358',
 '_i359',
 '_i36',
 '_i360',
 '_i361',
 '_i362',
 '_i363',
 '_i364',
 '_i365',
 '_i366',
 '_i367',
 '_i368',
 '_i369',
 '_i37',
 '_i370',
 '_i371',
 '_i372',
 '_i373',
 '_i374',
 '_i375',
 '_i376',
 '_i377',
 '_i378',
 '_i379',
 '_i38',
 '_i380',
 '_i381',
 '_i382',
 '_i383',
 '_i384',
 '_i385',
 '_i386',
 '_i387',
 '_i388',
 '_i389',
 '_i39',
 '_i390',
 '_i391',
 '_i392',
 '_i393',
 '_i394',
 '_i395',
 '_i396',
 '_i397',
 '_i398',
 '_i399',
 '_i4',
 '_i40',
 '_i400',
 '_i401',
 '_i402',
 '_i403',
 '_i404',
 '_i405',
 '_i406',
 '_i407',
 '_i408',
 '_i409',
 '_i41',
 '_i410',
 '_i411',
 '_i412',
 '_i413',
 '_i414',
 '_i415',
 '_i416',
 '_i417',
 '_i418',
 '_i419',
 '_i42',
 '_i420',
 '_i421',
 '_i422',
 '_i423',
 '_i424',
 '_i425',
 '_i426',
 '_i427',
 '_i428',
 '_i429',
 '_i43',
 '_i430',
 '_i431',
 '_i432',
 '_i433',
 '_i434',
 '_i435',
 '_i436',
 '_i437',
 '_i438',
 '_i439',
 '_i44',
 '_i440',
 '_i441',
 '_i442',
 '_i443',
 '_i444',
 '_i445',
 '_i446',
 '_i447',
 '_i448',
 '_i449',
 '_i45',
 '_i450',
 '_i451',
 '_i452',
 '_i453',
 '_i454',
 '_i455',
 '_i456',
 '_i457',
 '_i458',
 '_i459',
 '_i46',
 '_i460',
 '_i461',
 '_i462',
 '_i463',
 '_i464',
 '_i465',
 '_i466',
 '_i467',
 '_i468',
 '_i469',
 '_i47',
 '_i470',
 '_i471',
 '_i472',
 '_i473',
 '_i474',
 '_i475',
 '_i476',
 '_i477',
 '_i478',
 '_i479',
 '_i48',
 '_i480',
 '_i481',
 '_i482',
 '_i483',
 '_i484',
 '_i485',
 '_i486',
 '_i487',
 '_i488',
 '_i489',
 '_i49',
 '_i490',
 '_i491',
 '_i492',
 '_i493',
 '_i494',
 '_i495',
 '_i496',
 '_i497',
 '_i5',
 '_i50',
 '_i51',
 '_i52',
 '_i53',
 '_i54',
 '_i55',
 '_i56',
 '_i57',
 '_i58',
 '_i59',
 '_i6',
 '_i60',
 '_i61',
 '_i62',
 '_i63',
 '_i64',
 '_i65',
 '_i66',
 '_i67',
 '_i68',
 '_i69',
 '_i7',
 '_i70',
 '_i71',
 '_i72',
 '_i73',
 '_i74',
 '_i75',
 '_i76',
 '_i77',
 '_i78',
 '_i79',
 '_i8',
 '_i80',
 '_i81',
 '_i82',
 '_i83',
 '_i84',
 '_i85',
 '_i86',
 '_i87',
 '_i88',
 '_i89',
 '_i9',
 '_i90',
 '_i91',
 '_i92',
 '_i93',
 '_i94',
 '_i95',
 '_i96',
 '_i97',
 '_i98',
 '_i99',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 '_sh',
 'a',
 'args',
 'b',
 'b1',
 'b2',
 'c',
 'coords',
 'cs',
 'd',
 'dist',
 'distances',
 'exit',
 'f',
 'foo',
 'g',
 'get_ipython',
 'i',
 'it',
 'k',
 'kv',
 'l',
 'l1',
 'l2',
 'l3',
 'll',
 'math',
 'mymap',
 'n',
 'name',
 'peter_pan_text',
 'peter_pan_word_count',
 'peter_pan_words',
 'quit',
 'r1',
 'r2',
 'r3',
 'random',
 'rest',
 's',
 's1',
 's2',
 'say_hi',
 'sum_of_x_and_y',
 'tup',
 'urllib',
 'v',
 'w',
 'x']
In [499]:
import random
In [500]:
random
Out[500]:
<module 'random' from '/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py'>
In [501]:
dir(random)
Out[501]:
['BPF',
 'LOG4',
 'NV_MAGICCONST',
 'RECIP_BPF',
 'Random',
 'SG_MAGICCONST',
 'SystemRandom',
 'TWOPI',
 '_BuiltinMethodType',
 '_MethodType',
 '_Sequence',
 '_Set',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_acos',
 '_bisect',
 '_ceil',
 '_cos',
 '_e',
 '_exp',
 '_inst',
 '_itertools',
 '_log',
 '_pi',
 '_random',
 '_sha512',
 '_sin',
 '_sqrt',
 '_test',
 '_test_generator',
 '_urandom',
 '_warn',
 'betavariate',
 'choice',
 'choices',
 'expovariate',
 'gammavariate',
 'gauss',
 'getrandbits',
 'getstate',
 'lognormvariate',
 'normalvariate',
 'paretovariate',
 'randint',
 'random',
 'randrange',
 'sample',
 'seed',
 'setstate',
 'shuffle',
 'triangular',
 'uniform',
 'vonmisesvariate',
 'weibullvariate']
In [502]:
?random.random
In [511]:
random.random()
Out[511]:
0.9425189236118384
In [512]:
?random.randrange
In [523]:
random.randrange(1, 100)
Out[523]:
90
In [524]:
from random import random, randrange
In [525]:
random()
Out[525]:
0.6395559135723597
In [526]:
randrange(10, 200)
Out[526]:
109
In [527]:
import random as r
In [528]:
r.random()
Out[528]:
0.4709577701782732