In [1]:
import this
In [7]:
for i in [1, 2, 3, 4, 5]:
print(i)
for j in [1, 2, 3, 4, 5]:
print(j)
print(i + j)
print(i)
print('done looping')
In [8]:
long_winded_computation = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 +
13 + 14 + 15 + 16 + 17 + 18 + 19 + 20)
In [9]:
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [10]:
easier_to_read_list_of_lists = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9] ]
In [11]:
two_plus_three = 2 + \
3
In [13]:
import re
In [14]:
my_regex = re.compile("[0-9]+", re.I)
In [16]:
def double(x):
return x * 2
In [17]:
def apply_to_one(f):
return f(1)
In [18]:
my_double = double
In [19]:
x = apply_to_one(my_double)
In [20]:
x
Out[20]:
In [21]:
def my_print(message='my default message'):
print(message)
In [22]:
my_print('hello world!')
In [23]:
my_print()
In [24]:
def substract(a=0, b=0):
return a - b
In [25]:
substract(10, 5)
Out[25]:
In [26]:
substract(0, 5)
Out[26]:
In [27]:
substract(4)
Out[27]:
In [28]:
substract(b=2)
Out[28]:
In [29]:
single_quoted_string = 'data science'
In [30]:
double_quoted_strng = "data science"
In [31]:
tab_string = '\t'
In [32]:
len(tab_string)
Out[32]:
In [34]:
print(tab_string)
In [35]:
not_tab_string = r'/t'
In [36]:
len(not_tab_string)
Out[36]:
In [37]:
print(not_tab_string)
In [39]:
multi_line_string = """This is the first line.
and this is the second line
and this is the third line"""
In [40]:
multi_line_string
Out[40]:
In [41]:
print(multi_line_string)
In [44]:
try:
print(0 / 0)
except ZeroDivisionError:
print('cannot divide by zero')
In [45]:
integer_list = [1, 2, 3]
heterogeneous_list = ['string', 0.1, True]
In [46]:
list_of_lists = [integer_list, heterogeneous_list, []]
In [47]:
list_length = len(integer_list)
In [48]:
list_sum = sum(integer_list)
In [49]:
1 in [1, 2, 3]
Out[49]:
In [50]:
0 in [1, 2, 3]
Out[50]:
In [51]:
x = [1, 2, 3]
x.extend([4, 5, 6])
In [52]:
x
Out[52]:
In [53]:
y = x + [7, 8, 9]
In [54]:
y
Out[54]:
In [55]:
y.append(0)
In [56]:
y
Out[56]:
In [57]:
x[-1]
Out[57]:
In [58]:
len(x)
Out[58]:
In [59]:
a, b = [1, 3]
In [60]:
b
Out[60]:
In [61]:
a
Out[61]:
In [63]:
_, c =[2, 4]
In [64]:
c
Out[64]:
In [65]:
my_tuple = (1, 2)
In [66]:
my_tuple
Out[66]:
In [67]:
other_tuple = 3, 4
In [68]:
other_tuple
Out[68]:
In [69]:
my_tuple[1]
Out[69]:
In [70]:
try:
my_tuple[0] = 2
except TypeError:
print('cannot modify a tuple')
In [71]:
def sum_and_product(x, y):
return (x + y), (x * y)
In [72]:
sum_and_product(2, 3)
Out[72]:
In [73]:
sum_and_product(5, 10)
Out[73]:
In [74]:
m, n = 2, 3
In [75]:
m, n = n, m
In [76]:
m
Out[76]:
In [77]:
n
Out[77]:
In [78]:
empty_dict = {}
empty_dict2 = dict()
grades = {"Joel": 80, "Tim": 95}
In [79]:
grades["Joel"]
Out[79]:
In [80]:
try:
kates_grade = grades['Kate']
except KeyError:
print('no grade for Kate!')
In [81]:
'Joel' in grades
Out[81]:
In [82]:
'Kate' in grades
Out[82]:
In [83]:
grades.get('Joel', 0)
Out[83]:
In [84]:
grades.get('Kate', 0)
Out[84]:
In [85]:
grades.get('No One')
In [86]:
grades['Tim'] = 99
grades['Kate'] = 95
len(grades)
Out[86]:
In [88]:
tweet = {
'user': 'joelgrus',
'text': 'Data Science is Awesome',
'retweet_count': 100,
'hashtags': ['#data', '#science', '#datascience', '#awesome', '#yolo']
}
In [89]:
tweet_keys = tweet.keys()
tweet_values = tweet.values()
tweet_items = tweet.items()
In [90]:
'user' in tweet_keys
Out[90]:
In [91]:
'user' in tweet
Out[91]:
In [92]:
100 in tweet
Out[92]:
In [93]:
'joelgrus' in tweet_values
Out[93]:
In [96]:
from collections import defaultdict
In [97]:
dd_list = defaultdict(list)
dd_list[2].append(1)
In [98]:
dd_list
Out[98]:
In [99]:
dd_dict = defaultdict(dict)
dd_dict['Joel']['City'] = 'Seattle'
In [100]:
dd_dict
Out[100]:
In [101]:
dd_pair = defaultdict(lambda: [0, 0])
dd_pair[2][1] = 1
In [102]:
dd_pair
Out[102]:
In [103]:
from collections import Counter
In [104]:
c = Counter([0, 1, 2, 0, 2, 2, 4, 1])
In [105]:
c
Out[105]:
In [110]:
for num, count in c.most_common():
print(num, count)
In [111]:
s = set()
s.add(1)
print(s)
s.add(2)
print(s)
s.add(2)
print(s)
x = len(s)
print(x)
In [112]:
2 in s
Out[112]:
In [113]:
3 in s
Out[113]:
In [114]:
all([])
Out[114]:
In [116]:
any([])
Out[116]:
In [117]:
x = [4, 1, 2, 3]
In [118]:
y = sorted(x)
In [119]:
y
Out[119]:
In [120]:
x.sort()
In [121]:
x
Out[121]:
In [122]:
x = sorted([-4, 1, -2, 3], key=abs, reverse=True)
In [123]:
x
Out[123]:
In [124]:
even_numbers = [x for x in range(5) if x % 2 == 0]
In [126]:
even_numbers
Out[126]:
In [127]:
squares = [x * x for x in range(5)]
In [128]:
squares
Out[128]:
In [129]:
even_squares = [x * x for x in range(5) if x % 2 == 0]
In [130]:
even_squares
Out[130]:
In [131]:
square_dict = {x : x * x for x in range(5)}
In [132]:
square_dict
Out[132]:
In [133]:
square_set = {x * x for x in [-1, 1, 2]}
In [134]:
square_set
Out[134]:
In [135]:
zeroes = [0 for _ in even_numbers]
In [136]:
zeroes
Out[136]:
In [137]:
pairs = [(x, y) for x in range(10) for y in range(22)]
In [138]:
pairs
Out[138]:
In [139]:
increasing_pairs = [(x, y) for x in range(10) for y in range(x + 1, 10)]
In [140]:
increasing_pairs
Out[140]:
In [141]:
def lazy_range(n):
"""a lazy version of range"""
i = 0
while i < n:
yield i
i += 1
In [142]:
lazy_range(10)
Out[142]:
In [143]:
for i in lazy_range(10):
print(i + 2)
In [144]:
import random
In [145]:
four_uniform_rand = [random.random() for _ in range(4)]
In [146]:
four_uniform_rand
Out[146]:
In [147]:
random.random()
Out[147]:
In [148]:
random.random()
Out[148]:
In [149]:
random.seed(10)
random.random()
Out[149]:
In [150]:
random.random()
Out[150]:
In [151]:
random.seed(10)
random.random()
Out[151]:
In [152]:
random.randrange(10)
Out[152]:
In [153]:
random.randrange(3,6)
Out[153]:
In [169]:
zero_ten = list(range(11))
In [170]:
random.shuffle(zero_ten)
In [171]:
print(zero_ten)
In [174]:
random.choice(zero_ten)
Out[174]:
In [175]:
lottery_numbers = list(range(60))
In [176]:
winning_numbers = random.sample(lottery_numbers, 6)
In [177]:
winning_numbers
Out[177]:
In [179]:
[random.choice(lottery_numbers) for _ in range(6)]
Out[179]:
In [180]:
import re
In [181]:
print(all([
not re.match('a', 'cat'),
re.search('a', 'cat'),
not re.search('c', 'dog'),
3 == len(re.split('[ab]', 'carbs')),
'R-D-' == re.sub('[0-9]', '-', 'R2D2')
]))
In [1]:
def exp(base, power):
return base ** power
In [2]:
def two_to_the(power):
return exp(2, power)
In [3]:
from functools import partial
In [4]:
two_to_the = partial(exp, 2)
In [5]:
print(two_to_the(3))
In [6]:
square_of = partial(exp, power=2)
In [7]:
print(square_of(3))
In [8]:
def double(x):
return 2 * x
In [9]:
xs = [1, 2, 3, 4]
In [10]:
twice_xs = [double(x) for x in xs]
In [11]:
twice_xs
Out[11]:
In [13]:
list(map(double, xs))
Out[13]:
In [14]:
list_doubler = partial(map, double)
In [16]:
list(list_doubler(xs))
Out[16]:
In [17]:
def multiply(x, y): return x * y
In [18]:
products = map(multiply, [1, 2], [4, 5])
In [20]:
list(products)
Out[20]:
In [21]:
def is_even(x):
return x % 2 == 0
In [22]:
x_evens = [x for x in xs if is_even(x)]
In [23]:
x_evens
Out[23]:
In [25]:
list(filter(is_even, xs))
Out[25]:
In [26]:
list_evener = partial(filter, is_even)
In [27]:
list(list_evener(xs))
Out[27]:
In [29]:
list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
In [32]:
pairs = list(zip(list1, list2))
pairs
Out[32]:
In [35]:
list(zip(*pairs))
Out[35]:
In [36]:
def add(a, b):
return a + b
In [37]:
add(1, 2)
Out[37]:
In [39]:
add(*[1, 2])
Out[39]:
In [40]:
def magic(*args, **kwargs):
print('unnamed args:', args)
print('keyword args:', kwargs)
In [41]:
magic(1, 2, key='word', key2='word2')
In [42]:
def other_way_magic(x, y, z):
return x + y + z
In [43]:
x_y_list = [1, 2]
In [44]:
z_dict = {'z': 3}
In [46]:
other_way_magic(*x_y_list, **z_dict)
Out[46]:
In [47]:
def doubler_correct(f):
def g(*args, **kwargs):
return 2 * f(*args, **kwargs)
return g
In [48]:
def f2(x, y):
return x + y
In [49]:
g = doubler_correct(f2)
In [53]:
g(1, 2)
Out[53]:
In [ ]: