We derive analytical expressions for the grand canonical partition functions of point masses, and of extended masses (e. g., galaxies with halos), which cluster gravitationally in an expanding universe. From the partition functions, we obtain the system's thermodynamic properties, distribution functions ( including voids and counts in cells), and moments of distributions such as their skewness and kurtosis. This also provides an analytical calculation of the evolution of the distribution. Our results apply to both linear and nonlinear regimes of clustering. In the limit of point masses, these results reduce exactly to previous results derived from thermodynamics, thus providing a new, more fundamental foundation for the earlier results.