How to write a generator function

yield is a keyword that is used like return, except the function will return a generator. When you call the function, the code of the function body does not run. The function only returns the generator object, which you can iterate on! The first time you iterate the generator object created from your function, it will run the code from the beginning until it reaches yield, and return the first value. Then the next call will continue running the code. This will continue until the function runs without hitting yield.

Expected value

Let $X$ be a discrete random variable. The expected value of $X$ is $$ \mathbb{E}[X]=\sum\limits_{x\in X}{x\mathbb{P}(X=x)}. $$ Example: Calculate the expected value of the sum of the numbers on two dice!

Do experiments for the expected value by simulation!

About the first homework

A possible solution

3. St. Petersburg paradox