The variance of a discrete random variable X is defined as Var(X)=E[(X−μ)2], where μ=E[X]. Expanding the square and applying linearity of expectation gives an equivalent — and usually faster — computational formula:
Var(X)=E[X2]−(E[X])2.
The derivation is short:
Var(X)=E[(X−μ)2]=E[X2−2μX+μ2]=E[X2]−2μE[X]+μ2=E[X2]−2μ2+μ2=E[X2]−μ2.
To illustrate, suppose X has the PMF
xP(X=x)00.510.320.2
Then
E[X]E[X2]Var(X)=0(0.5)+1(0.3)+2(0.2)=0.7,=02(0.5)+12(0.3)+22(0.2)=1.1,=1.1−0.72=1.1−0.49=0.61.