Your sentence is correct.
The other sentence is correct with a small change ('the countryside, not 'countryside')
"I prefer living in town than in the countryside".
Saying ('town' instead of 'a town') is more often said when you are referring to the town that you are currently living in, though it can be said in other circumstances as well.