[Tutor] Geometric sequence
Peter O'Doherty
abjadmailinglist at gmail.com
Fri Nov 8 20:19:28 CET 2013
To the people who kindly replied to my question: many thanks!
On 10/31/2013 06:29 PM, Danny Yoo wrote:
> As an aside: It shouldn't be too bad to write a "generator" for the
> geometric series, so that we can pick out the terms on-demand.
>
> #################################
> >>> def geometric(base):
> ... x = 1
> ... while True:
> ... yield x
> ... x *= base
> ...
> >>> twos = geometric(2)
> #################################
>
>
> 'twos' is a representation of the geometric series; we can then pick
> out the elements one at a time:
>
>
> #################################
> >>> twos.next()
> 1
> >>> twos.next()
> 2
> >>> twos.next()
> 4
> >>> twos.next()
> 8
> >>> twos.next()
> 16
> >>> twos.next()
> 32
> >>> twos.next()
> 64
> #################################
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131108/dfbe4cba/attachment-0001.html>
More information about the Tutor
mailing list