Setup a virtual environment:
$ python3.6 -m venv modernpython
$ source modernpython/bin/activate
Install the packages used in the course:
(modernpython) $ pyflakes
(modernpython) $ bottle
(modernpython) $ pytest
(modernpython) $ hypothesis
(modernpython) $ mypy
Big Idea:
Statistics modeled in a program are easier to get right and understand than using a formulaic approach. It is also extends to more complicated situations that classic formulas.
- F-strings
- Counter(), most_common, elements
- Statistics
- Random: seed gauss triangular expovariate choice choices sample shuffle
- Review list concatenation, slicing, count/index, sorted()
- Review lambda expressions and chained comparisons
In [1]: # %-formatting .format() f''
In [2]: x = 10
In [3]: print('The answer is %d today' % x)
The answer is 10 today
In [4]: print('The answer is {0} today'.format(x))
The answer is 10 today
In [5]: print('The answer is {x} today'.format(x=x))
The answer is 10 today
In [6]: print(f'The answer is {x} today')
The answer is 10 today
In [7]: print(f'The answer is {x :08d} today')
The answer is 00000010 today
In [8]: print(f'The answer is {x ** 2 :08d} today')
The answer is 00000100 today
In [9]: type(x)
Out[9]: int
In [10]: type(x).__name__
Out[10]: 'int'
In [11]: raise ValueError(f"Expected {x!r} to a float not a {type(x).__name__}")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-b9152af534d8> in <module>
----> 1 raise ValueError(f"Expected {x!r} to a float not a {type(x).__name__}")
ValueError: Expected 10 to a float not a int
In [12]: from collections import Counter
In [13]: d = {}
In [14]: d['dragons']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-14-c67452f26063> in <module>
----> 1 d['dragons']
KeyError: 'dragons'
In [15]: d = Counter()
In [16]: d['dragons']
Out[16]: 0
In [17]: d['dragons'] += 1
In [18]: d
Out[18]: Counter({'dragons': 1})
In [19]: 'red green red blue red blue green'.split()
Out[19]: ['red', 'green', 'red', 'blue', 'red', 'blue', 'green']
In [20]: Counter('red green red blue red blue green'.split())
Out[20]: Counter({'red': 3, 'green': 2, 'blue': 2})
In [21]: c = Counter('red green red blue red blue green'.split())
In [22]: c.most_common(1)
Out[22]: [('red', 3)]
In [23]: c.most_common(2)
Out[23]: [('red', 3), ('green', 2)]
In [24]: c.elements()
Out[24]: <itertools.chain at 0x7f5db3429e80>
In [25]: list(c.elements())
Out[25]: ['red', 'red', 'red', 'green', 'green', 'blue', 'blue']
In [26]: list(c)
Out[26]: ['red', 'green', 'blue']
In [27]: list(c.values())
Out[27]: [3, 2, 2]
In [28]: list(c.items())
Out[28]: [('red', 3), ('green', 2), ('blue', 2)]
In [29]: list(c.elements())
Out[29]: ['red', 'red', 'red', 'green', 'green', 'blue', 'blue']
In [30]: from statistics import mean, median, mode, stdev, pstdev
In [31]: mean([50, 52, 53])
Out[31]: 51.666666666666664
In [33]: median([51, 50, 52, 53])
Out[33]: 51.5
In [34]: mode([51, 50, 52, 53, 51, 51])
Out[34]: 51
In [35]: stdev([51, 50, 52, 53, 51, 51])
Out[35]: 1.0327955589886444
In [36]: pstdev([51, 50, 52, 53, 51, 51])
Out[36]: 0.9428090415820634
In [37]: s = [10, 20, 30]
In [38]: t = [40, 50, 60]
In [39]: u = s + t
In [40]: u
Out[40]: [10, 20, 30, 40, 50, 60]
In [41]: u[:2]
Out[41]: [10, 20]
In [42]: u[-2:]
Out[42]: [50, 60]
In [43]: u[:2] + u[-2:]
Out[43]: [10, 20, 50, 60]
In [45]: s = 'abracadabra'
In [46]: s.index('c')
Out[46]: 4
In [47]: s.count('c')
Out[47]: 1
In [48]: s.count('a')
Out[48]: 5
In [49]: s = [10, 5, 70, 2]
In [50]: s.sort()
In [51]: s
Out[51]: [2, 5, 10, 70]
In [52]: s = [10, 5, 70, 2]
In [53]: t = sorted(s)
In [54]: s
Out[54]: [10, 5, 70, 2]
In [55]: t
Out[55]: [2, 5, 10, 70]
In [56]: sorted('cat')
Out[56]: ['a', 'c', 't']
In [57]: # lambda -> partial, itemgetter, attgetter, ..
In [58]: # ^--- make function()
In [59]: # ^--- make a computation in the future
In [60]: lambda x: x**2
Out[60]: <function __main__.<lambda>(x)>
In [61]: (lambda x: x**2)(5)
Out[61]: 25
In [62]: 100 + (lambda x: x**2)(5) + 50
Out[62]: 175
In [63]: f = lambda x, y: 3 * x + y
In [64]: f(3, 8)
Out[64]: 17
In [65]: x = 10
In [66]: y = 20
In [67]: f = lambda : x ** y
In [68]: f()
Out[68]: 100000000000000000000
In [69]: x = 15
In [70]: x > 6
Out[70]: True
In [71]: x < 10
Out[71]: False
In [72]: x > 6 and x < 20
Out[72]: True
In [73]: 6 < x < 20
Out[73]: True
In [74]: # Chained comparisons
In [1]: from random import *
In [2]: random()
Out[2]: 0.4367594986905675
In [3]: seed(8675309)
In [4]: random()
Out[4]: 0.40224696110279223
In [5]: random()
Out[5]: 0.5102471779215914
In [8]: seed(8675309)
In [9]: random()
Out[9]: 0.40224696110279223
In [10]: random()
Out[10]: 0.5102471779215914
In [11]: from random import choice, choices, sample, shuffle
In [12]: outcomes = ['win', 'lose', 'draw', 'play again', 'double win']
In [13]: choice(outcomes)
Out[13]: 'double win'
In [14]: choice(outcomes)
Out[14]: 'draw'
In [15]: choice(outcomes)
Out[15]: 'lose'
In [16]: choices(outcomes, k=5)
Out[16]: ['play again', 'lose', 'double win', 'double win', 'draw']
In [18]: from collections import Counter
In [20]: Counter(choices(outcomes, k=10))
Out[20]: Counter({'win': 1, 'play again': 2, 'lose': 3, 'draw': 2, 'double win': 2})
In [21]: Counter(choices(outcomes, k=10_000))
Out[21]:
Counter({'play again': 2014,
'double win': 1995,
'win': 2048,
'lose': 1970,
'draw': 1973})
In [22]: Counter(choices(outcomes, [5, 4, 3, 2, 1], k=10_000))
Out[22]:
Counter({'double win': 641,
'play again': 1339,
'lose': 2706,
'draw': 2001,
'win': 3313})
In [23]: outcomes
Out[23]: ['win', 'lose', 'draw', 'play again', 'double win']
In [24]: shuffle(outcomes)
In [25]: outcomes
Out[25]: ['win', 'lose', 'double win', 'draw', 'play again']
In [26]: choices(outcomes, k=5)
Out[26]: ['win', 'win', 'double win', 'draw', 'play again']
In [28]: sample(outcomes, k=4)
Out[28]: ['win', 'play again', 'draw', 'lose']
In [29]: sample(outcomes, k=4)
Out[29]: ['double win', 'lose', 'draw', 'win']
In [30]: # generate lottery number
In [31]: sorted(sample(range(1, 57), k=6))
Out[31]: [9, 11, 16, 23, 42, 49]