He was elected as a Midlothian Councillor in 1995, serving for six years. He was elected to the House of Commons at the 2001 general election for Midlothian following the retirement of Eric Clarke. Hamilton held the seat with a majority of 9,014 before deciding to step down before the 2015 general election. He made his maiden speech on 12 July 2001, in which he remembered one of his predecessors as the Midlothian MP and former Prime Minister, William Ewart Gladstone.