passing on Python lore

kirby urner <[email protected]> Wed, 24 Jul 2024 21:01:42 -0700
Newsgroups gmane.comp.python.education
Message-ID <CAPJgG3T+Dv96ax+exJ7pMwVeOOGx59FOw4tKJVynRubPbXt85Q@mail.gmail.com>
--===============7189288332872074529==
Content-Type: multipart/alternative; boundary="0000000000009728d9061e0a74d5"

--0000000000009728d9061e0a74d5
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

I'm herewith archiving an email from earlier today, to the guy in charge of
the Python curriculum at this company I work for (see LinkedIn profile). It
refers back to this listserv, in the context of recycling what I consider a
choice piece of Python lore: that generator for deriving digits of pi.  --
Kirby



---------- Forwarded message ---------
From: Kirby Urner <[email protected]>
Date: Wed, Jul 24, 2024 at 4:05=E2=80=AFPM
Subject: Python generators (a fun example, not by me)
To:




On the topic of Python generators, I just wanted to share what I consider a
mysterious one, meaning I didn't write it and don't understand why it works
(eyeballing the code has not yielded answers).

It may have actually come to me from Guido himself if I remember how it
went on edu-sig (one of the archived Python.org discussion groups on which
I've been active). He and I have a long term shared interest around
integrating Python into everyday math learning (we've come a long way since
then).

For context, I'm copying this Python generator from my Jupyter Notebook
called Pi Day, which is about using Python to generate the number pi in
various ways:

https://nbviewer.org/github/4dsolutions/Python5/blob/master/Pi%20Day%20Fun.=
ipynb

Although most of the Python is by me, I'm simply implementing algorithms
discovered by others (it's about their genius, not mine), such as that
famous one by Ramanujan in the middle of the page.

Here's the generator I'm talking about (at the very end of the above
notebook, before the flashing GIF pizza pi):

"""
Another generator example:  converging to Pi

https://mail.python.org/pipermail/edu-sig/2015-September/date.html

"""

def pi():

    k, a, b, a1, b1 =3D 2, 4, 1, 12, 4
    while True:
        p, q, k =3D k*k, 2*k+1, k+1
        a, b, a1, b1 =3D a1, b1, p*a+q*a1, p*b+q*b1
        d, d1 =3D a/b, a1/b1
        while d =3D=3D d1:
            yield int(d)
            a, a1 =3D 10*(a%b), 10*(a1%b1)
            d, d1 =3D a/b, a1/b1

if __name__ =3D=3D "__main__":
    the_gen =3D pi()
    for _ in range(100):
        print(next(the_gen),end=3D"")
    print()

If I run that I get (I also have 3.9 as well as 3.11):

Python 3.9.12 (main, Apr  5 2022, 01:53:17)
Type "copyright", "credits" or "license" for more information.

IPython 7.31.1 -- An enhanced Interactive Python.

runfile('/Users/kirbyurner/Documents/clarusway_data_analysis/python_warm_up=
/pi_generator.py',
wdir=3D'/Users/kirbyurner/Documents/clarusway_data_analysis/python_warm_up'=
)
314159265358979323846264338327950288419716939937510582097494459230781640628=
6208998628034825342117067

Those are the digits of pi <https://www.piday.org/million/>!  Why?  Pretty
interesting, no?

Kirby

--0000000000009728d9061e0a74d5
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:verdana,=
sans-serif;font-size:small"><br></div><div class=3D"gmail_default" style=3D=
"font-family:verdana,sans-serif;font-size:small"><br></div><div class=3D"gm=
ail_default" style=3D"font-family:verdana,sans-serif;font-size:small">I&#39=
;m herewith archiving an email from earlier today, to the guy in charge of =
the Python curriculum at this company I work for (see LinkedIn profile). It=
 refers back to this listserv, in the context of recycling what I consider =
a choice piece of Python lore: that generator for deriving digits of pi.=C2=
=A0 -- Kirby</div><div class=3D"gmail_default" style=3D"font-family:verdana=
,sans-serif;font-size:small"><br></div><div class=3D"gmail_default" style=
=3D"font-family:verdana,sans-serif;font-size:small"><br></div><br><div clas=
s=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">---------- Forwarde=
d message ---------<br>From: <strong class=3D"gmail_sendername" dir=3D"auto=
">Kirby Urner</strong> <span dir=3D"auto">&lt;<a href=3D"mailto:kirby@claru=
sway.com">[email protected]</a>&gt;</span><br>Date: Wed, Jul 24, 2024 at =
4:05=E2=80=AFPM<br>Subject: Python generators (a fun example, not by me)<br=
>To:=C2=A0<br></div><br><br><div dir=3D"ltr"><br><div><br></div><div>On the=
 topic of Python generators, I just wanted to share what I consider a myste=
rious one, meaning I didn&#39;t write it and don&#39;t understand why it wo=
rks (eyeballing the code has not yielded answers).</div><div><br></div><div=
>It may have actually come to me from Guido himself if I remember how it we=
nt on edu-sig (one of the archived Python.org discussion groups on which I&=
#39;ve been active). He and I have a long term shared interest around integ=
rating Python into everyday math learning (we&#39;ve come a long way since =
then).</div><div><br></div><div>For context, I&#39;m copying this Python ge=
nerator from my Jupyter Notebook called Pi Day, which is about using Python=
 to generate the number pi in various ways:=C2=A0</div><div><br></div><div>=
<a href=3D"https://nbviewer.org/github/4dsolutions/Python5/blob/master/Pi%2=
0Day%20Fun.ipynb" target=3D"_blank">https://nbviewer.org/github/4dsolutions=
/Python5/blob/master/Pi%20Day%20Fun.ipynb</a><br></div><div><br></div><div>=
Although most of the Python is by me, I&#39;m simply implementing algorithm=
s discovered by others (it&#39;s about their genius, not mine), such as tha=
t famous one by Ramanujan in the middle of the page.</div><div><br></div><d=
iv>Here&#39;s the generator I&#39;m talking about (at the very end of the a=
bove notebook, before the flashing GIF pizza pi):</div><div><font face=3D"m=
onospace"><br></font></div><div><font face=3D"monospace">&quot;&quot;&quot;=
<br>Another generator example: =C2=A0converging to Pi<br><br><a href=3D"htt=
ps://mail.python.org/pipermail/edu-sig/2015-September/date.html" target=3D"=
_blank">https://mail.python.org/pipermail/edu-sig/2015-September/date.html<=
/a><br><br>&quot;&quot;&quot;<br><br>def pi():<br><br>=C2=A0 =C2=A0 k, a, b=
, a1, b1 =3D 2, 4, 1, 12, 4<br>=C2=A0 =C2=A0 while True:<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 p, q, k =3D k*k, 2*k+1, k+1<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 a,=
 b, a1, b1 =3D a1, b1, p*a+q*a1, p*b+q*b1<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 d,=
 d1 =3D a/b, a1/b1<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 while d =3D=3D d1:<br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 yield int(d)<br>=C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 a, a1 =3D 10*(a%b), 10*(a1%b1)<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 d, d1 =3D a/b, a1/b1<br><br>if __name__ =3D=3D &qu=
ot;__main__&quot;: =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0<br>=C2=A0 =C2=A0 the_gen =3D pi()<br>=C2=A0 =C2=A0 for _ in r=
ange(100):<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 print(next(the_gen),end=3D&quot;&=
quot;)<br>=C2=A0 =C2=A0 print()</font><br></div><div><br></div><div>If I ru=
n that I get (I also have 3.9 as well as 3.11):<br></div><div><br></div><di=
v><font face=3D"monospace">Python 3.9.12 (main, Apr =C2=A05 2022, 01:53:17)=
 <br>Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot;=
 for more information.<br><br>IPython 7.31.1 -- An enhanced Interactive Pyt=
hon.<br><br>runfile(&#39;/Users/kirbyurner/Documents/clarusway_data_analysi=
s/python_warm_up/pi_generator.py&#39;, wdir=3D&#39;/Users/kirbyurner/Docume=
nts/clarusway_data_analysis/python_warm_up&#39;)<br>31415926535897932384626=
433832795028841971693993751058209749445923078164062862089986280348253421170=
67<br></font></div><div><br></div><div>Those are <a href=3D"https://www.pid=
ay.org/million/" target=3D"_blank">the digits of pi</a>!=C2=A0 Why?=C2=A0 P=
retty interesting, no?<br></div><div><br></div><div>Kirby</div><div><br></d=
iv><div><br></div><div><br></div></div>
</div></div>

--0000000000009728d9061e0a74d5--

--===============7189288332872074529==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Edu-sig mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/edu-sig.python.org/
Member address: [email protected]

--===============7189288332872074529==--