Hej Steven, thanks for the clarification. I have two questions - one about map function and the other about return.
> So, in mathematics we might have a mapping between (let's say) counting > numbers 1, 2, 3, 4, ... and the even numbers larger than fifty, 52, 54, > 56, ... and so on. The mapping function is 50 + 2*x: > > x = 1 --> 50 + 2*1 = 52 > x = 2 --> 50 + 2*2 = 54 > x = 3 --> 50 + 2*3 = 56 > x = 4 --> 50 + 2*4 = 58 > > and so on, where we might read the arrow --> as "maps to". > > So the fundamental idea is that we take a series of elements (in the > above case, 1, 2, 3, ...) and a function, apply the function to each > element in turn, and get back a series of transformed elements (52, 54, > 56, ...) as the result. > > So in Python, we can do this with map. First we define a function to do > the transformation, then pass it to map: > > def transform(n): > return 50 + 2*n > > result = map(transform, [1, 2, 3, 4]) #1 Question In which cases should I use a map function instead of a for loop like this for example: def transform(n, m): for i in range (n, m): print (50 + 2*i) transform(1,5) >>> 52 54 56 58 > A thought comes to mind... an very important lesson is to learn the > difference between return and print, and to prefer return. > > You have written a function that calculates the digit sum. But it is not > *reusable* in other functions, since it cannot do anything but *print* > the digit sum. What if you want to store the result in a variable, and > print it later? Or print it twice? Or put it in a list? Or add one to > it? You're screwed, the function is no use to you at all. #2 Question Strangely, I get entirely different results depending on whether I use return or print within a function. Example: def transform(n, m): for i in range (n, m): print (50 + 2*i) transform(1,5) >>> 52 54 56 58 Versus: def transform(n, m): for i in range(n, m): return (50 + 2*i) print(transform(1,5)) >>> 52 Why do I get entirely different results in each case? &: How do I prevent my loop from breaking after the first round when using return instead of print? All the best, Raf _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor