# [Tutor] Starting over with Python

James Cunningham jameshcunningham at uky.edu
Thu Dec 14 20:49:23 CET 2006

```Sum works just fine, as others have said. A more generic way to do this:

reduce(lambda x, y: x + y, (i for i in range(100) if i % 2))

Reduce iterates through the list, calling x + y on the next element in the
list and the previous sum, in effect summing the whole thing. I might do the
following:

def reduce_list(list, operator):
return reduce(lambda x, y: operator(x, y), list)

reduce_list((i for i in range(100) if i % 2), int.__add__)
reduce_list((i for i in range(100) if i % 2), int.__mul__)

etc.

best,
james

On 12/13/06, John Carmona <jeannot18 at hotmail.com> wrote:
>
> After quite a while away from Python, I have decided to re-study Python. I
> am interested to learn Python to support my love for Cryptography. I have
> a
> first very easy question (did some search on Google but could not find
> anything helpful). I realise that this is very basic so be gentle with me.
>
> If i insert the following script:
>
>
> -------------------------------------------------------------------------------------------------
> odd =1
> >>>while odd <=100:
>         if (odd%2)==1:
>                 print odd
>         odd = odd + 1
>
> -------------------------------------------------------------------------------------------------
> I get a list of the odd numbers from 1 to 99. But now if I wanted to add
> those number together (i.e. 1 + 3 +5 + 7 etc.), what line of coding should
> I
> include? I have tried to add "odd + odd" but it did not work. In advance
> thanks.
>
> If anyone could direct me to some site where python is associated with
> Cryptography I would be very grateful. Many thanks
> JC
>
> _________________________________________________________________
> Be the first to hear what's new at MSN - sign up to our free newsletters!