Python solution for ex 1.1