Re: Using Python as a Calculator
Wes Turner via Edu-sig <[email protected]> Sun, 22 Jun 2025 15:06:15 -0400
| Newsgroups | gmane.comp.python.education |
|---|---|
| Message-ID | <CACfEFw9g52s+dwQjD1SFOYT=U86keGS7JMrZnSJai8d5TY2aJg@mail.gmail.com> |
--===============0118171760488926101==
Content-Type: multipart/alternative; boundary="000000000000afed8506382dca52"
--000000000000afed8506382dca52
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Add a few more of these percent-style notebook delimiters and comments and
a heading and that'd be a notebook:
# %%
```python
# %%
# To run tests in a notebook with Pytest, there's ipytest:
# https://github.com/chmp/ipytest
!pip install ipytest
# %%
import ipytest
ipytest.autoconfig()
# %%
%%ipytest -qq
# This assertion will be run with `pytest`,
# so there will be a useful AssertionError:
assert "str a" =3D=3D "str b"
# %%
import pytest
with pytest.raises(AssertionError):
assert True =3D=3D False
# %%
%%ipytest -qq
import unittest
class TestAssertions(unittest.TestCase):
def test_assertRaises():
with self.assertRaises(AssertionError):
assert True =3D=3D False
# %%
%%ipytest -qq
import unittest
test =3D unittest.TestCase()
with test.assertRaises(AssertionError):
assert True =3D=3D False
```
Are notebook development skills favored over coverage-tested modules, with
*tests* in a separate directory or in the same directory; test_*.py?
On Sun, Jun 22, 2025, 2:13=E2=80=AFPM Jorge Garcia <[email protected]> wro=
te:
> Share code on github is all well and good, but sharing in a Jupyter
> Notebook such as colab.research.google.com makes it executable in your
> browser right away!
>
> On Sun, Jun 22, 2025, 2:01=E2=80=AFPM Wes Turner via Edu-sig <edu-sig@pyt=
hon.org>
> wrote:
>
>> Here's the link to that PR, which makes things testable and then verifie=
s
>> with test assertions:
>>
>> https://github.com/4dsolutions/python_camp/pull/4
>>
>> ... @staticmethod is for when you want to add a method on a class that
>> doesn't take self as the first parameter.
>>
>> On Sun, Jun 22, 2025, 1:52=E2=80=AFPM Wes Turner <[email protected]> =
wrote:
>>
>>> How to best phase in Python?
>>>
>>> It can check your answers every time something is changed:
>>>
>>> ```py
>>> 1+1
>>>
>>> print(1+1)
>>>
>>> x =3D 1 +1
>>> print(x)
>>>
>>> 1+1 =3D=3D True
>>>
>>> print(1+1 =3D=3D True)
>>>
>>> assert 1+1 =3D=3D True
>>> print("It did not raise AssertionError")
>>>
>>> assert 1+1 =3D=3D False
>>> print("This won't run, because AssertionError is an Exception, and
>>> control flow is interrupted on Exception")
>>>
>>>
>>> # %%
>>> import unittest
>>> test =3D unittest.TestCase()
>>>
>>> test.assertEqual(1+1, 0)
>>> ```
>>>
>>> We verify our assumptions about types and values of parameters and
>>> return values at runtime; we check preconditions and post conditions.
>>>
>>> I am reminded of camper_program.py from 2020:
>>>
>>> https://mail.python.org/archives/list/[email protected]/message/OYZ2QP=
DYDFRV2PPJXEOHUTFWFPZVOCJO/
>>>
>>> Maybe a good prompt to lock in that automated testing is much more
>>> efficient than attempting to manually test everything after every chang=
e:
>>>
>>> "Write your own test assertion library; starting with `def
>>> assertEqual(a, b, msg)`"
>>>
>>> On Sun, Jun 22, 2025, 1:19=E2=80=AFPM kirby urner via Edu-sig <
>>> [email protected]> wrote:
>>>
>>>>
>>>> From an educator's perspective, how to best phase-in Python is often a
>>>> core question.
>>>>
>>>> One answer is: use it the same way you might use a calculator in the
>>>> classroom, but show off why it's better.
>>>>
>>>> An example (new today) of what I share with my student and peer
>>>> faculty, perhaps new to Python:
>>>>
>>>>
>>>> https://github.com/4dsolutions/School_of_Tomorrow/blob/master/smod_pla=
y.py
>>>>
>>>> A mix of Python + geometric content already familiar to those using my
>>>> curriculum.
>>>>
>>>> The docstring itself contains a link to the famous Using Python as a
>>>> Calculator tutorial at the Python dot org site.
>>>>
>>>> Kirby
>>>>
>>>> _______________________________________________
>>>> 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]
>>>>
>>> _______________________________________________
>> 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]
>>
>
--000000000000afed8506382dca52
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"auto">Add a few more of these percent-style notebook delimiters=
and comments and a heading and that'd be a notebook:=C2=A0<div dir=3D"=
auto"><br></div><div dir=3D"auto"># %%</div><div dir=3D"auto"><br></div><di=
v dir=3D"auto"><br></div><div dir=3D"auto">```python</div><div dir=3D"auto"=
><br></div><div dir=3D"auto"># %%</div><div dir=3D"auto"># To run tests in =
a notebook with Pytest, there's ipytest:</div><div dir=3D"auto"># <a hr=
ef=3D"https://github.com/chmp/ipytest">https://github.com/chmp/ipytest</a><=
/div><div dir=3D"auto">!pip install ipytest</div><div dir=3D"auto"><br></di=
v><div dir=3D"auto"># %%</div><div dir=3D"auto">import ipytest</div><div di=
r=3D"auto">ipytest.autoconfig()</div><div dir=3D"auto"><br></div><div dir=
=3D"auto"># %%</div><div dir=3D"auto">%%ipytest -qq</div><div dir=3D"auto">=
# This assertion will be run with `pytest`,=C2=A0</div><div dir=3D"auto"># =
so there will be a useful AssertionError:</div><div dir=3D"auto">assert &qu=
ot;str a" =3D=3D "str b"</div><div dir=3D"auto"></div><div d=
ir=3D"auto"><br></div><div dir=3D"auto"># %%</div><div dir=3D"auto">import =
pytest</div><div dir=3D"auto">with pytest.raises(AssertionError):</div><div=
dir=3D"auto">=C2=A0 =C2=A0 assert True =3D=3D False</div><div dir=3D"auto"=
><br></div><div dir=3D"auto"># %%</div><div dir=3D"auto">%%ipytest -qq</div=
><div dir=3D"auto">import unittest</div><div dir=3D"auto"><br></div><div di=
r=3D"auto">class TestAssertions(unittest.TestCase):</div><div dir=3D"auto">=
=C2=A0 =C2=A0 def test_assertRaises():</div><div dir=3D"auto">=C2=A0 =C2=A0=
=C2=A0 =C2=A0 with self.assertRaises(AssertionError):</div><div dir=3D"aut=
o">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 assert True =3D=3D False</div>=
<div dir=3D"auto"><br></div><div dir=3D"auto"># %%</div><div dir=3D"auto">%=
%ipytest -qq</div><div dir=3D"auto">import unittest</div><div dir=3D"auto">=
test =3D unittest.TestCase()</div><div dir=3D"auto"><br></div><div dir=3D"a=
uto"><div dir=3D"auto">with test.assertRaises(AssertionError):</div><div di=
r=3D"auto">=C2=A0 =C2=A0 =C2=A0 =C2=A0 assert True =3D=3D False</div><div d=
ir=3D"auto">```</div><div dir=3D"auto"><br></div><div dir=3D"auto">Are note=
book development skills favored over=C2=A0 coverage-tested modules, with *t=
ests* in a separate directory or in the same directory;=C2=A0 test_*.py?</d=
iv></div><div dir=3D"auto"><br></div><div dir=3D"auto"><br></div></div><br>=
<div class=3D"gmail_quote gmail_quote_container"><div dir=3D"ltr" class=3D"=
gmail_attr">On Sun, Jun 22, 2025, 2:13=E2=80=AFPM Jorge Garcia <<a href=
=3D"mailto:[email protected]">[email protected]</a>> wrote:<br></div><=
blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-l=
eft:1px solid rgb(204,204,204);padding-left:1ex"><p dir=3D"ltr">Share code =
on github is all well and good, but sharing in a Jupyter Notebook such as <=
a href=3D"http://colab.research.google.com" target=3D"_blank" rel=3D"norefe=
rrer">colab.research.google.com</a> makes it executable in your browser rig=
ht away!</p>
<br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sun=
, Jun 22, 2025, 2:01=E2=80=AFPM Wes Turner via Edu-sig <<a href=3D"mailt=
o:[email protected]" target=3D"_blank" rel=3D"noreferrer">[email protected]=
rg</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margi=
n:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex=
"><div dir=3D"auto">Here's the link to that PR, which makes things test=
able and then verifies with test assertions:<div dir=3D"auto"><br></div><di=
v dir=3D"auto"><a href=3D"https://github.com/4dsolutions/python_camp/pull/4=
" rel=3D"noreferrer noreferrer noreferrer" target=3D"_blank">https://github=
.com/4dsolutions/python_camp/pull/4</a></div><div dir=3D"auto"><br></div><d=
iv dir=3D"auto">... @staticmethod is for when you want to add a method on a=
class that doesn't take self as the first parameter.=C2=A0</div></div>=
<br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sun=
, Jun 22, 2025, 1:52=E2=80=AFPM Wes Turner <<a href=3D"mailto:wes.turner=
@gmail.com" rel=3D"noreferrer noreferrer noreferrer" target=3D"_blank">wes.=
[email protected]</a>> wrote:<br></div><blockquote class=3D"gmail_quote" =
style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pa=
dding-left:1ex"><div dir=3D"auto"><div dir=3D"auto">How to best phase in Py=
thon?</div><div dir=3D"auto"><br></div><div dir=3D"auto">It can check your =
answers every time something is changed:</div><div dir=3D"auto"><br></div><=
div dir=3D"auto">```py</div><div dir=3D"auto">1+1</div><div dir=3D"auto"><b=
r></div><div dir=3D"auto">print(1+1)</div><div dir=3D"auto"><br></div><div =
dir=3D"auto">x =3D 1 +1</div><div dir=3D"auto">print(x)</div><div dir=3D"au=
to"><br></div><div dir=3D"auto">1+1 =3D=3D True</div><div dir=3D"auto"><br>=
</div><div dir=3D"auto">print(1+1 =3D=3D True)</div><div dir=3D"auto"><br><=
/div><div dir=3D"auto">assert 1+1 =3D=3D True</div><div dir=3D"auto">print(=
"It did not raise AssertionError")</div><div dir=3D"auto"><br></d=
iv><div dir=3D"auto">assert 1+1 =3D=3D False</div><div dir=3D"auto">print(&=
quot;This won't run, because AssertionError is an Exception, and contro=
l flow is interrupted on Exception")</div><div dir=3D"auto"><br></div>=
<div dir=3D"auto"><br></div><div dir=3D"auto"># %%</div><div dir=3D"auto">i=
mport unittest</div><div dir=3D"auto">test =3D unittest.TestCase()</div><di=
v dir=3D"auto"><br></div><div dir=3D"auto">test.assertEqual(1+1, 0)</div><d=
iv dir=3D"auto">```</div><div dir=3D"auto"><br></div><div dir=3D"auto">We v=
erify our assumptions about types and values of parameters and return value=
s at runtime; we check preconditions and post conditions.</div><div dir=3D"=
auto"><br></div><div dir=3D"auto">I am reminded of camper_program.py from 2=
020:=C2=A0</div><a href=3D"https://mail.python.org/archives/list/edu-sig@py=
thon.org/message/OYZ2QPDYDFRV2PPJXEOHUTFWFPZVOCJO/" rel=3D"noreferrer noref=
errer noreferrer noreferrer" target=3D"_blank">https://mail.python.org/arch=
ives/list/[email protected]/message/OYZ2QPDYDFRV2PPJXEOHUTFWFPZVOCJO/</a><=
div dir=3D"auto"><br></div><div dir=3D"auto">Maybe a good prompt to lock in=
that automated testing is much more efficient than attempting to manually =
test everything after every change:</div><div dir=3D"auto"><br></div><div d=
ir=3D"auto">"Write your own test assertion library; starting with `def=
assertEqual(a, b, msg)`"<br><br><div class=3D"gmail_quote" dir=3D"aut=
o"><div dir=3D"ltr" class=3D"gmail_attr">On Sun, Jun 22, 2025, 1:19=E2=80=
=AFPM kirby urner via Edu-sig <<a href=3D"mailto:[email protected]" rel=
=3D"noreferrer noreferrer noreferrer noreferrer" target=3D"_blank">edu-sig@=
python.org</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=
=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding=
-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-fami=
ly:verdana,sans-serif;font-size:small"><br></div><div class=3D"gmail_defaul=
t" style=3D"font-family:verdana,sans-serif;font-size:small">From an educato=
r's perspective, how to best phase-in Python is often a core question.=
=C2=A0</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">One answer is: use it the same =
way you might use a calculator in the classroom, but show off why it's =
better.</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"fon=
t-family:verdana,sans-serif;font-size:small">An example (new today) of what=
I share with my student and peer faculty, perhaps new to Python:</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"><a href=3D"https://github.com/4dsolutions/Schoo=
l_of_Tomorrow/blob/master/smod_play.py" rel=3D"noreferrer noreferrer norefe=
rrer noreferrer noreferrer" target=3D"_blank">https://github.com/4dsolution=
s/School_of_Tomorrow/blob/master/smod_play.py</a></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">A mix of Python=C2=A0+ geometric content already familiar to th=
ose using my curriculum.</div><div class=3D"gmail_default" style=3D"font-fa=
mily:verdana,sans-serif;font-size:small"><br></div><div class=3D"gmail_defa=
ult" style=3D"font-family:verdana,sans-serif;font-size:small">The docstring=
itself contains a link to the famous Using Python as a Calculator tutorial=
at the Python dot org site.</div><div class=3D"gmail_default" style=3D"fon=
t-family:verdana,sans-serif;font-size:small"><br></div><div class=3D"gmail_=
default" style=3D"font-family:verdana,sans-serif;font-size:small">Kirby</di=
v><div class=3D"gmail_default" style=3D"font-family:verdana,sans-serif;font=
-size:small"><br></div></div>
_______________________________________________<br>
Edu-sig mailing list -- <a href=3D"mailto:[email protected]" rel=3D"norefe=
rrer noreferrer noreferrer noreferrer noreferrer" target=3D"_blank">edu-sig=
@python.org</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]"=
rel=3D"noreferrer noreferrer noreferrer noreferrer noreferrer" target=3D"_=
blank">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman3//lists/edu-sig.python.org" rel=
=3D"noreferrer noreferrer noreferrer noreferrer noreferrer noreferrer" targ=
et=3D"_blank">https://mail.python.org/mailman3//lists/edu-sig.python.org</a=
><br>
Member address: <a href=3D"mailto:[email protected]" rel=3D"noreferrer n=
oreferrer noreferrer noreferrer noreferrer" target=3D"_blank">wes.turner@gm=
ail.com</a><br>
</blockquote></div></div></div>
</blockquote></div>
_______________________________________________<br>
Edu-sig mailing list -- <a href=3D"mailto:[email protected]" rel=3D"norefe=
rrer noreferrer" target=3D"_blank">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]"=
rel=3D"noreferrer noreferrer" target=3D"_blank">[email protected]</=
a><br>
<a href=3D"https://mail.python.org/mailman3//lists/edu-sig.python.org" rel=
=3D"noreferrer noreferrer noreferrer" target=3D"_blank">https://mail.python=
.org/mailman3//lists/edu-sig.python.org</a><br>
Member address: <a href=3D"mailto:[email protected]" rel=3D"noreferrer nor=
eferrer" target=3D"_blank">[email protected]</a><br>
</blockquote></div>
</blockquote></div>
--000000000000afed8506382dca52--
--===============0118171760488926101==
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]
--===============0118171760488926101==--