The canonical partition function (StatMech) I recently started my course on Statistical Mechanics, where I have been introduced to the partition function of the canonical (and grand canonical) ensemble. My problem is that I struggle (a lot) with understanding it right. The problem really is the notation I guess, since it can be written as $Z=\sum_i e^{-\beta E_i}$ or as $Z=\sum_i g_i e^{-\beta E_i}$. I know that the first is supposed to be the sum over the states and the second is a sum over the energies (hence the degeneracy factor $g_i$), but this then means that the $E_i$'s are different things, or the labels have to be understood differently. And that's where I get lost...
 A: If you write $$Z=\sum_{i\in\text{States}}e^{-\beta E_i}$$
then $E_i$ is the energy of state $i$, noting that if two states $i\neq j$ have the same energy, then $E_i=E_j$ and you're just adding the same number twice.
Alternatively, you could group all possible states by their energies, and then sum over all of those energy levels while taking into account how many states correspond to each.
$$Z=\sum_{i\in\text{ Energy Levels}} g_i e^{-\beta E_i}$$
where now $E_i$ denotes the energy of the $i^{th}$ energy "group", and $g_i$ denotes the number of states in it.

As an example, consider  a system which has three states called $A,B,$ and $C$.  State $A$ has energy $E_A=E_1$ while states $B$ and $C$ have energy $E_B=E_C=E_2$.  
By the first definition, the partition function is
$$Z = \sum_{i\in\{A,B,C\}} e^{-\beta E_i} = e^{-\beta E_A} + e^{-\beta E_B} + E^{-\beta E_C}$$
$$ = e^{-\beta E_1} + e^{-\beta E_2} + e^{-\beta E_2}  = e^{-\beta E_1} + 2e^{-\beta E_2}$$
and by the second, 
$$Z = \sum_{i\in\{1,2\}} g_i e^{-\beta E_i} = g_1 e^{-\beta E_1} + g_2 e^{-\beta E_2}$$
$$ = e^{-\beta E_1} + 2 e^{-\beta E_2}$$
A: The short answer is "forgot about it". The ideal of partition function came from combinatorics, (https://en.wikipedia.org/wiki/Partition_(number_theory) ) but it become a very convenient things for statmec as a tool for counting probably states. Just be aware that $Z$ was most of the time for single particle. Your notation with $g_i$ is probably saying that the states is weighted, that at energy $E_i$ there's $g_i$ fold degeneracy.
