Example 3.47 Let $0 \le p \le 1$. Recall that $X \sim \mathcal{Ber}(p)$ has probability mass function $\mathbb{P}[X=1] = p$ and $\mathbb{P}[X=0] = 1-p$ and the expectation $\mathbb{E}[X]=p$. Hence its variance is $$ \begin{split} \mathbb{Var} X &= \mathbb{E}\left[(X-p)^2\right] \\ &= (1-p)^2 \mathbb{P}[X=1] + (0-p)^2\mathbb{P}[X=0] \\ &= (1-p)^2p + p^2(1-p) \\ &= p(1-p). \end{split} $$
I don’t understand the computation. It seems that we can just assume that we don’t need to expand the inner sum into $X^2-2Xp+p^2$. And $X$ is either one or zero. May we calculate the variance using the formula $E[X^2]-E[X]^2$ instead?
It's true that you don't need to expand that sum, although you certainly could. They're using the Law of the Unconscious Statistician: to find the expected value of a discrete random variable, you add up all its values weighted along with its probabilities of being those values. Here, the values of $(X - p)^2$ are $(1 - p)^2$ and $(0-p)^2$, because $X$ can be either $0$ or $1$.
It would have been fine to expand the inner term by FOILing, and you'd have arrived at the same result.
Yes.