I’m studying Lagrangian and Hamiltonian mechanics for the first time. In the Lagrangian approach, that is the one I’ve studied first, a fundamental point is the definition of the degree of freedom of the system. If the system has DOF = n, than n independent coordinates (the Lagrangian or generalised coordinates) are all you need to completely describe the configuration of the system. Any other coordinate must depend on the others.
Moving to the Hamiltonian framework though, we consider 2n independent variables, the n generalised coordinates and the n generalised momenta. How is this compatible with the system having a degree of freedom equal to n?
The idea is essentially this: in the Lagrangian setting we consider 2nd order ODE, i.e. of the form $$\frac{d^2}{dt^2}q = F(q,\dot q)$$ and $q$ lives in a space of dimension $n$. In the Hamiltonian setting, we consider a 1st order ODE, in a space that has dimension 2n. The example to keep in mind is passing from the equation above to the two equations $$\frac d {dt} x = y$$ $$\frac d {dt} y = F(x,y).$$
So the idea is to introduce a new variable but that will play the role of the velocity (or more precisely momentum).