Confusion in understanding meaning of $E(X\mid Y)$ and $E(E(x\mid y))=E(x)$ 
Confused in understanding meaning of $E(X\mid Y)$ and $E(E(X\mid Y))=E(X)$

I know $E(X\mid Y=y_i)$ is average value of random variable $X$ at $Y=y_i$. 
Now i did not understanding following - 


*

*what is $E(X\mid Y)$?

*How $E(X\mid Y)$ is function of Y? $E(X\mid Y=y_i) $ is function of $X$ right?(pls correct me if i am wrong)

*How is $E(X)=E(E(X\mid Y))$
$E(X) = \sum_y E(X\mid Y=y_i ) P(Y=y_i) = \sum_y \sum_x xP(X=x\mid Y=y_i)P(Y=y_i)$
I have recently started probability and statistics. Pls elaborate in detail (assume me a layman). I also refered to in wiki. I did not understand here.
 A: I think an example ought to clear your doubts up. Suppose we throw a dice. Whatever number shows up on the dice, we toss a coin that many times. We want to study the number of heads obtained. 
Now let $X$ denote the number of heads obtained and $Y$ denote the number on the dice. 
As you can see, the value that $X$ takes depends on $Y$.
Also note that $X|Y$ is a random variable. 
In general, the mass function of the conditional distribution in the discrete case is given by:
$$P(X=x|Y =y) = \frac{P(X=x, Y=y)}{P(Y=y)} \quad(1)$$
And for a continuous random variable, the density function of the conditional distribution is given by: 
$$f_{X|Y}(x|y) = \frac{f_{X,Y}(x,y)}{f_Y{y}} \quad (2)$$
where $f_{X,Y}(x,y)$ is the joint probability density function and $f_Y(y)$ is the marginal density function of $y$. 


*

*What is the expected number of heads if it is given that a $3$ showed up on the dice? 


Here we are looking at $E(X|Y = 3)$. 


*This question is answered above. 

*Lets prove this for the continuous case: 
$$E(X|Y=y) = \int_{-\infty}^{\infty} x f_{X|Y}(x|y) dx$$
$$E(E(X|Y = y)) = \int_{-\infty}^{\infty} f_Y(y) \int_{-\infty}^{\infty} x f_{X|Y}(x|y) dx dy$$
$$=\int_{-\infty}^{\infty} \int_{-\infty}^{\infty} x f_{X|Y}(x|y) f_Y(y) dx dy$$
$$= \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} x f_{X,Y}(x,y)dx dy \quad \text{Using (2)}$$
$$= \int_{-\infty}^{\infty} x \int_{-\infty}^{\infty} f_{X,Y}(x,y)dy dx$$
$$= \int_{-\infty}^{\infty} x f_{X}(x) dx$$
$$=E(X)$$
