Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42992 > unrolled thread

Newbie to python. Very newbie question

Started byReviewBoard User <lalitha.viswanath@gmail.com>
First post2013-04-07 04:16 -0700
Last post2013-04-08 03:16 +0000
Articles 10 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  Newbie to python. Very newbie question ReviewBoard User <lalitha.viswanath@gmail.com> - 2013-04-07 04:16 -0700
    Re: Newbie to python. Very newbie question Kruno Saho <kruno.saho@gmail.com> - 2013-04-07 04:19 -0700
    Re: Newbie to python. Very newbie question Dave Angel <davea@davea.name> - 2013-04-07 07:50 -0400
    Re: Newbie to python. Very newbie question rusi <rustompmody@gmail.com> - 2013-04-07 10:02 -0700
    Re: Newbie to python. Very newbie question Miki Tebeka <miki.tebeka@gmail.com> - 2013-04-07 10:04 -0700
    Re: Newbie to python. Very newbie question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-04-07 15:09 -0400
      Re: Newbie to python. Very newbie question Miki Tebeka <miki.tebeka@gmail.com> - 2013-04-07 16:57 -0700
    Re: Newbie to python. Very newbie question Ian Foote <ian@feete.org> - 2013-04-07 20:23 +0100
    Re: Newbie to python. Very newbie question Arnaud Delobelle <arnodel@gmail.com> - 2013-04-07 21:16 +0100
    Re: Newbie to python. Very newbie question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-08 03:16 +0000

#42992 — Newbie to python. Very newbie question

FromReviewBoard User <lalitha.viswanath@gmail.com>
Date2013-04-07 04:16 -0700
SubjectNewbie to python. Very newbie question
Message-ID<f0759987-4ff8-49a1-a826-4ae3f6508030@5g2000yqz.googlegroups.com>
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

[toc] | [next] | [standalone]


#42993

FromKruno Saho <kruno.saho@gmail.com>
Date2013-04-07 04:19 -0700
Message-ID<bb778099-2f57-41d1-9f11-e8c04b6c7be3@googlegroups.com>
In reply to#42992
On Sunday, April 7, 2013 9:16:27 PM UTC+10, ReviewBoard User 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

Are you sure you do not mean '==' instead of '='?

[toc] | [prev] | [next] | [standalone]


#42996

FromDave Angel <davea@davea.name>
Date2013-04-07 07:50 -0400
Message-ID<mailman.239.1365335449.3114.python-list@python.org>
In reply to#42992
On 04/07/2013 07:16 AM, ReviewBoard User wrote:
> Hi
> I am a newbie to python

Then why are you trying to do 7 or 8 things on one line?

> 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)
>

No it doesn't.  A small piece of it does, and I'd recommend making that 
piece a separate line or three, probably making a function out of it.

Then if you want to write other code to exercise that function, go right 
ahead.

If you're new to Python, concentrate on the algorithm needed, and keep 
the program straightforward.  After you've got something simple working, 
and you're comfortable with the algorithm, then you can play code-golf 
to your heart's content.

Perhaps you hadn't realized that any odd number when squared will yield 
an odd number, and likewise for even.  So the stated problem is much 
simpler than what you're trying to do.

3 lambda's in one line of code?  Silly.



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#43005

Fromrusi <rustompmody@gmail.com>
Date2013-04-07 10:02 -0700
Message-ID<eaf04e6d-615a-4169-9a6e-bd94d04d539e@p5g2000yqj.googlegroups.com>
In reply to#42992
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

[toc] | [prev] | [next] | [standalone]


#43006

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2013-04-07 10:04 -0700
Message-ID<8cf004b8-fa76-4c94-b366-fccbf448c914@googlegroups.com>
In reply to#42992
> I am a newbie to python
Welcome! I hope you'll do great things with Python.

> and am trying to write a program that does a
> sum of squares of numbers whose squares are odd.
OK.

> For example, for x from 1 to 100, it generates 165 as an output (sum
> of 1,9,25,49,81)
I don't follow, you seem to be missing a lot of numbers. For example 3^2 = 9 which is odd as well.

> 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))

print X = Y is a syntax error. Why do you need the 2'nd part.
In general, we're moving to list/generator comperhension over map/filter.
Something like:
    print(sum(x*x for x in xrange(10**6) if (x*x)%2))

HTH,
Miki

[toc] | [prev] | [next] | [standalone]


#43013

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-04-07 15:09 -0400
Message-ID<mailman.251.1365361814.3114.python-list@python.org>
In reply to#42992
On Sun, 7 Apr 2013 04:16:27 -0700 (PDT), ReviewBoard User
<lalitha.viswanath@gmail.com> 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/

[toc] | [prev] | [next] | [standalone]


#43031

FromMiki Tebeka <miki.tebeka@gmail.com>
Date2013-04-07 16:57 -0700
Message-ID<8fafbd2c-1fe5-4615-b85b-633a7a2a5756@googlegroups.com>
In reply to#43013
> 	I can't even read that mess... three nested lambda?
I have to say this and other answers in this thread seem not that friendly to me.
The OP said it's a newbie question, we should be more welcoming to newcomers.

[toc] | [prev] | [next] | [standalone]


#43014

FromIan Foote <ian@feete.org>
Date2013-04-07 20:23 +0100
Message-ID<mailman.252.1365362646.3114.python-list@python.org>
In reply to#42992
On 07/04/13 20:09, Dennis Lee Bieber wrote:
> On Sun, 7 Apr 2013 04:16:27 -0700 (PDT), ReviewBoard User
> <lalitha.viswanath@gmail.com> 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
>>>>

I'm surprised no one has suggested:

 >>> import math
 >>> sum( x*x for x in range(1, int(math.sqrt(100)), 2))

Regards,
Ian F

[toc] | [prev] | [next] | [standalone]


#43016

FromArnaud Delobelle <arnodel@gmail.com>
Date2013-04-07 21:16 +0100
Message-ID<mailman.253.1365365803.3114.python-list@python.org>
In reply to#42992
On 7 April 2013 20:23, Ian Foote <ian@feete.org> wrote:
> I'm surprised no one has suggested:
>
>>>> import math
>>>> sum( x*x for x in range(1, int(math.sqrt(100)), 2))

Yeah! And I'm surprised no one came up with:

>>> from itertools import count, takewhile
>>> sum(takewhile((100).__gt__, filter((2).__rmod__, map((2).__rpow__, count(1)))))
165

:)

-- 
Arnaud

[toc] | [prev] | [next] | [standalone]


#43035

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-04-08 03:16 +0000
Message-ID<51623687$0$29868$c3e8da3$5496439d@news.astraweb.com>
In reply to#42992
On Sun, 07 Apr 2013 04:16:27 -0700, ReviewBoard User 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?

Python already has told you what the error is. You should read the error 
message. If you don't understand it, you should copy and paste the full 
traceback, starting with the line "Traceback", and ask for help. But 
please do not expect us to *guess* what error you are seeing.

I'm now going to guess. I think you are seeing this error:


py> len(x) = len(y)
  File "<stdin>", line 1
SyntaxError: can't assign to function call


In this example, I have a function call, len(x), on the left hand side of 
an assignment. That is illegal syntax.

In your code, you also have a function call reduce(...) on the left hand 
side of an assignment. If the error message is not clear enough, can you 
suggest something that would be more understandable?

Perhaps you meant to use an equality test == instead of = assignment.


> 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


No, the reference material does not generally provide examples. Some 
people like that style, and some don't.

However, many pages do have extensive examples:

http://docs.python.org/2/library/string.html


You should also work through a tutorial or two. Also the "Module of the 
week" website is very good:

http://pymotw.com



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web