Code for the extension question

Written in python