__AUTHORS__ = {'am': ("Andrea Marino",
"andrea.marino@unifi.it",),
'mn': ("Massimo Nocentini",
"massimo.nocentini@unifi.it",
"https://github.com/massimo-nocentini/",)}
__KEYWORDS__ = ['Python', 'Jupyter', 'language', 'keynote',]
Start at https://docs.python.org/3/index.html for everything.
For the tutorial have a look at https://docs.python.org/3/tutorial/index.html, nicely written and complete.
def increment(a):
return a + 1
increment(0)
1
increment(1)
2
L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
LL = [increment(a) for a in L]
LL
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
LLL = [increment(a) for a in LL]
LLL
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
r = range(10)
r
range(0, 10)
list(r)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
map(lambda i: i + 1, L)
<map at 0x7f0590b631c0>
(lambda i: i + 1)(0)
1
(lambda i: i + 1)(1)
2
list(map(lambda i: i + 1, L))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
M = map(lambda i: i + 1, L)
M
<map at 0x7f0590b630d0>
next(M)
1
next(M)
2
next(M)
3
next(M)
4
next(M)
5
next(M)
6
next(M)
7
next(M)
8
next(M)
9
next(M)
10
next(M)
--------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-96-0666361e9047> in <module> ----> 1 next(M) StopIteration:
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(i for i in range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
N = (i for i in range(10))
N
<generator object <genexpr> at 0x7f0568ef1040>
list(N)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
next(N)
--------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-103-0f4723c98d79> in <module> ----> 1 next(N) StopIteration:
we want to build an object that denotes a Bernoulli random variable.
it is desired to be able to sample from that variable an arbitrary number of times, not known at design time.
from random import random # import the random generator, to be used to sample from the uniform distribution
random() # a quick check that the random function works
0.03068991525009368
int(True) # this is a very quick check to see if a Boolean can be used as integer
1
def Bernoulli(p):
'This is a generator for a Bernoulli random variable of parameter `p` for success.'
while True: # forever we loop
r = random() # get a sample
yield int(r <= p) # if that sample denotes a success or a failure we *yield* that outcome
yield # if we evaluate *yield* not in a context, Python raises an error because it is a construct
File "<ipython-input-112-f8373fab0a85>", line 1 yield # if we evaluate *yield* not in a context, Python raises an error because it is a construct ^ SyntaxError: 'yield' outside function
help(Bernoulli)
Help on function Bernoulli in module __main__: Bernoulli(p) This is a generator for a Bernoulli random variable of parameter `p` for success.
B = Bernoulli(p=0.6) # B is our random variable
B
<generator object Bernoulli at 0x7f0568d4cf20>
next(B)
1
next(B)
0
next(B)
1
sample = [next(B) for _ in range(1000)]
sample[:20] # just for a quick evaluation, we print the first 20 elements
[0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
from collections import Counter
Counter(sample)
Counter({0: 421, 1: 579})
B_flip = map(lambda o: 1-o, B)
B_flip
<map at 0x7f0568efe040>
sample = [next(B_flip) for _ in range(1000)]
sample[:20] # just for a quick evaluation, we print the first 20 elements
[0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
def Bernoulli(p):
'This is a generator for a Bernoulli random variable of parameter `p` for success.'
while True: # forever we loop
r = random() # get a sample
o = int(r <= p) # if that sample denotes a success or a failure we *yield* that outcome
print('B ' + str(o))
yield o
def flip(o):
print('flip')
return 1-o
B_flip = map(flip, Bernoulli(p=0.9))
B_flip
<map at 0x7f0568ef89a0>
sample = [next(B_flip) for _ in range(20)]
Counter(sample)
B 0 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip B 0 flip B 1 flip B 1 flip B 1 flip B 1 flip B 1 flip
Counter({1: 2, 0: 18})
class A(object):
def __init__(self, j):
self.j = j
def __add__(self, i):
return self.j + i
def __radd__(self, i):
return self.j + i
def __lt__(self, i):
return self.j < i
def B(b):
pass
B
<function __main__.B(b)>
B(3) is None
True
def B(b):
...
increment(4)
5
a = A()
increment(a)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-143-a46ef281d6c6> in <module> 1 a = A() ----> 2 increment(a) <ipython-input-69-d2b3a35173e0> in increment(a) 1 def increment(a): ----> 2 return a + 1 TypeError: unsupported operand type(s) for +: 'A' and 'int'
a = A()
increment(a)
2
A(3) + 1
4
1 + A(3)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-148-087a68ad802d> in <module> ----> 1 1 + A(3) TypeError: unsupported operand type(s) for +: 'int' and 'A'
1 + A(3)
4
A(4) < 2
False