On Apr 7, 4:16 pm, ReviewBoard User <lalitha.viswan...@gmail.com> wrote: > Hi > I am a newbie to python and am trying to write a program that does a > sum of squares of numbers whose squares are odd. > For example, for x from 1 to 100, it generates 165 as an output (sum > of 1,9,25,49,81) > > Here is the code I have > print reduce(lambda x, y: x+y, filter(lambda x: x%2, map(lambda x: > x*x, xrange > (10**6)))) = sum(x*x for x in xrange(1, 10**6, 2)) > > I am getting a syntax error. > Can you let me know what the error is? > > I am new to Python and am also looking for good documentation on > python functions.http://www.python.org/doc/does not provide examples > of usage of each function
In problems like this it is usually preferable to use list comprehensions over map/filter. Your problem is literally solvable like this: >>> [sq for sq in [x*x for x in range(100)] if sq%2 == 1 and sq <= 100] [1, 9, 25, 49, 81] >>> sum([sq for sq in [x*x for x in range(100)] if sq%2 == 1 and sq <= 100]) 165 Using Dave's observation that odd(x) == odd(x*x) it simplifies to >>> sum([x*x for x in range(100) if x%2==1 and x*x <=100]) 165 Note: Python comprehensions unlike Haskell does not allow local lets so the x*x has to be repeated -- http://mail.python.org/mailman/listinfo/python-list