For a system described by the canonical ensemble, the energy is an expectation value since we allow energy to be exchanged with the surroundings (a heat bath with temperature T).
This expectation value, the mean energy, can be calculated using ⟨E⟩=kBT2(∂lnZ∂T)V,N or using the probability distribution Pi as ⟨E⟩=M∑i=1EiPi(β)=1ZM∑i=1Eie−βEi.