[BangPypers] which one is the best pythonic way .

Anand Chitipothu anandology at gmail.com
Tue Feb 9 06:07:55 CET 2010


On Tue, Feb 9, 2010 at 10:22 AM, Srinivas Reddy Thatiparthy
<srinivas_thatiparthy at akebonosoft.com> wrote:
> I have written  four solutions to a problem(self explanatory) ,out of
> them ,which one is the  pythonic way of doing  and
> is there any other ways of solving it?
>
>
> 1.sum([i for i in range(1000) if i%3==0 or i%5==0])
>
> 2.gen=(i for i in range(1000))
>   sum([i for i in gen if i%3==0 or i%5==0])
>
> 3.sum(filter(lambda a:a%3==0 or a%5==0,range(1000)))
>
> 4.def generator(m):
>        count=0
>        while count<m:
>                 if count%3==0 or count%5==0:
>                     yield count
>                 count+=1
> sum([i for i in generator(1000)])

#1.

You can even remove the square brackets.

sum(i for i in range(1000) if i%3==0 or i%5==0)


More information about the BangPypers mailing list