derivation for expected value for variance
The expected value of a random variable is defined as (without entering into probability/measure theory):

For discrete distribution $F$, we have $E(X)=\sum_x P_F(X=x) x$
For (absolutely) continuous distribution $F=\int f(x) dx$, we have $E(X) = \int f(x) dx$

Therefore, the first equality is just the definition of expected value for the discrete case.
And finally, the law of the unconscious statistician (LOTUS) states that
$$E(g(X)) = \int g(x) f(x) dx$$ (for continuous F), or
$$E(g(X)) = \sum g(x) P(X=x)$$ (for discrete F).