sage: for k in range(1001):

The percent signs are telling you to append a string, and that the string will the string from the int k, respectively.
I don't think you have to use the f=function('f') syntax in this case; someone correct me if that's not true.