Recall that for a discrete random variable X with mean μ=E[X], the variance is defined as Var(X)=E[(X−μ)2]. The same definition applies when X is continuous with probability density function f(x); expectations are simply computed by integration:
Var(X)=E[(X−μ)2]=∫−∞∞(x−μ)2f(x)dx.
Expanding (X−μ)2 and using linearity of expectation yields the computational formula
Var(X)=E[X2]−μ2,
which is almost always easier to evaluate than the definition directly. The two ingredients are the first and second moments
μ=E[X]=∫−∞∞xf(x)dx,E[X2]=∫−∞∞x2f(x)dx.
Example. Let X have density f(x)=31 on [0,3] (and 0 elsewhere). Then
μE[X2]=∫03x⋅31dx=31⋅29=23,=∫03x2⋅31dx=31⋅327=3.
Therefore
Var(X)=E[X2]−μ2=3−49=43.