Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'syntax': 0.04; 'newbie': 0.05; 'output': 0.05; 'nested': 0.07; 'squares': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'python': 0.11; 'invoking': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:python.': 0.16; 'xrange': 0.16; 'trying': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'error': 0.23; 'url:home': 0.24; 'math': 0.24; 'header:X-Complaints-To:1': 0.27; 'is?': 0.30; 'code': 0.31; 'getting': 0.31; "can't": 0.35; 'version': 0.36; 'charset:us-ascii': 0.36; 'error.': 0.37; 'example,': 0.37; 'received:76': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'read': 0.60; 'most': 0.60; 'numbers': 0.61; 'simple': 0.61; 'sum': 0.64; 'more': 0.64; 'here': 0.66; 'subject:. ': 0.67; '165': 0.84; 'subject:Very': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Newbie to python. Very newbie question Date: Sun, 07 Apr 2013 15:09:58 -0400 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-253-106-230.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365361814 news.xs4all.nl 6841 [2001:888:2000:d::a6]:51255 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43013 On Sun, 7 Apr 2013 04:16:27 -0700 (PDT), ReviewBoard User declaimed the following in gmane.comp.python.general: > 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 can't even read that mess... three nested lambda? Not the most efficient version but... >>> sum( x*x for x in range(100/2) if (x*x % 2) and (x*x < 100) ) 165 > The range(100/2) is a simple reduction to avoid invoking a sqrt function... the more economical is >>> import math >>> sum( x*x for x in range(int(math.sqrt(100))) if x*x % 2) 165 >>> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/