Doing Math with Pythonhttp://doingmathwithpython.github.io/2023-10-25T13:20:00+10:00Doing Math with Python in Data Science Humble Bundle2023-10-25T13:20:00+10:002023-10-25T13:20:00+10:00Amit Sahatag:doingmathwithpython.github.io,2023-10-25:/humble-bundle-data-science.html<p class="first last">Humble bundle</p>
<p>"Doing Math with Python" is part of No Starch Press's <a class="reference external" href="https://www.humblebundle.com/books/data-science-no-starch-press-books">Data Science Humble Bundle</a>
running for the next 19 days. Your purchases will help support EFF!</p>
<div class="figure align-center">
<img alt="Humble Bundle" src="http://doingmathwithpython.github.io/images/data-science-humble-bundle.png" />
</div>
<p>Get the bundle <a class="reference external" href="https://www.humblebundle.com/books/data-science-no-starch-press-books">here</a>!</p>
<p>I am still surprised that the book still continues to be relevant, and i am quietly happy that I could produce such a book,
thanks to all the help i got from No Starch folks.</p>
Chapter 3 - Google Correlate example update2020-07-11T00:00:00+10:002020-07-11T00:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2020-07-11:/chapter-3-google-correlate-example-update.html<p>In Chapter 3 on Page 87, the book refers to the Google Correlate service. However, as of December 2019, the service has been shutdown.
Since the chapter requires you to download a CSV formatted data, it is no longer possible. However, you can instead download a version of
the data …</p><p>In Chapter 3 on Page 87, the book refers to the Google Correlate service. However, as of December 2019, the service has been shutdown.
Since the chapter requires you to download a CSV formatted data, it is no longer possible. However, you can instead download a version of
the data that I had used 5 years back when writing the book from
<a class="reference external" href="https://github.com/doingmathwithpython/code/blob/master/chapter3/solutions/correlate-summer.csv">here</a>.</p>
<p>Thanks to a reader for pointing me to this issue.</p>
Coding Starter Kit Humble Bundle2020-03-17T00:00:00+10:002020-03-17T00:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2020-03-17:/humble-bundle-coding-starter.html<p>I am very excited to share that "Doing Math with Python" is part of No Starch Press's <a class="reference external" href="https://www.humblebundle.com/books/coding-starter-kit-no-starch-press-books?mc_cid=01272437d1&mc_eid=a8fa0cb420">Coding Starter Humble Bundle</a>.
Of course, you get No Starch Press's other excellent coding books as part of the bundle.</p>
<p>It's on for the next 20 days!</p>
<div class="figure align-left">
<img alt="Humble Bundle" src="http://doingmathwithpython.github.io/images/coding-starter-humble-bundle.png" />
</div>
<p>Your purchases will help support the …</p><p>I am very excited to share that "Doing Math with Python" is part of No Starch Press's <a class="reference external" href="https://www.humblebundle.com/books/coding-starter-kit-no-starch-press-books?mc_cid=01272437d1&mc_eid=a8fa0cb420">Coding Starter Humble Bundle</a>.
Of course, you get No Starch Press's other excellent coding books as part of the bundle.</p>
<p>It's on for the next 20 days!</p>
<div class="figure align-left">
<img alt="Humble Bundle" src="http://doingmathwithpython.github.io/images/coding-starter-humble-bundle.png" />
</div>
<p>Your purchases will help support the No Starch Foundation and Scratch Foundation.</p>
<p>Get the bundle <a class="reference external" href="https://www.humblebundle.com/books/coding-starter-kit-no-starch-press-books?mc_cid=01272437d1&mc_eid=a8fa0cb420">here</a>.</p>
Number of trailing zeros in the factorial of an integer2020-01-02T19:50:00+10:002020-01-02T19:50:00+10:00Amit Sahatag:doingmathwithpython.github.io,2020-01-02:/trailing-zeros-factorial.html<p class="first last">Use Python to find the number of trailing zeros in the factorial of an integer</p>
<p>I recently learned about a cool formula to calculate the number of
trailing zeros in the factorial of a number. It has been a while since I
wrote a program to do something like this. So, I decided to change that and
write this blog post. Let's jump in.</p>
<p>In the spirit of wring various "calculators" in the book, we will
write a "number of trailing zero" calculator. First up though, let's refresh
some key relevant concepts.</p>
<p><strong>Factorial</strong>: The factorial of a number, <tt class="docutils literal">n</tt> denoted by <tt class="docutils literal">n!</tt> is the product <tt class="docutils literal"><span class="pre">n*(n-1)*(n-2)...*1</span></tt>.
For example, <tt class="docutils literal">5! = 5*4*3*2*1 = 120</tt>.</p>
<p><strong>Trailing zeros</strong>: The trailing zeros of a number is the number of zeros at the end of a number. For example,
the number 567100 has <strong>two</strong> trailing zeros.</p>
<p><strong>Floor</strong>: The floor of a number is the greatest integer less than or equal to x. That is floor of 3.2 is 3
and that of 3.5 is 3 and the floor of 3 is 3 as well.</p>
<p>Now, coming back to the focus of this post, this document at brilliant.org wiki
explains the process in <a class="reference external" href="https://brilliant.org/wiki/trailing-number-of-zeros/">detail</a>.</p>
<p>The key bit there in is this formula:</p>
<div class="figure">
<img alt="" src="http://doingmathwithpython.github.io/images/trailing_zeros_formula.png" />
</div>
<p>where, <tt class="docutils literal">n</tt> is the number for whose factorial we want to find the number of trailing zeros.</p>
<p>The following Python program implements the above formula:</p>
<pre class="code literal-block">
import math
def is_positive_integer(x):
try:
x = float(x)
except ValueError:
return False
else:
if x.is_integer() and x > 0:
return True
else:
return False
def trailing_zeros(num):
if is_positive_integer(num):
# The above function call has done all the sanity checks for us
# so we can just convert this into an integer here
num = int(num)
k = math.floor(math.log(num, 5))
zeros = 0
for i in range(1, k + 1):
zeros = zeros + math.floor(num/math.pow(5, i))
return zeros
else:
print("Factorial of a non-positive non-integer is undefined")
if __name__ == "__main__":
fact_num = input(
"Enter the number whose factorial's trailing zeros you want to find: "
)
num_zeros = trailing_zeros(fact_num)
print("Number of trailing zeros: {0}".format(num_zeros))
</pre>
<p>When we run this program using Python 3, it will ask for the number whose factorial's number of trailing
zeros we want to find and then print it out, like so:</p>
<pre class="code literal-block">
Enter the number whose factorial's trailing zeros you want to find: 5
Number of trailing zeros: 1
</pre>
<p>If you enter a number which is not a positive integer, you will get an error message:</p>
<pre class="code literal-block">
Enter the number whose factorial's trailing zeros you want to find: 5.1
Factorial of a non-positive integer is undefined
Number of trailing zeros: None
</pre>
<p>Some key standard library functions we use in the above program are:</p>
<ul class="simple">
<li><tt class="docutils literal">math.floor</tt>: This function is used to find the floor of a number</li>
<li><tt class="docutils literal">math.log</tt>: This function is used to find the logarithm of a number for a specified base (defaults to 10)</li>
<li><tt class="docutils literal">math.pow</tt>: This function is used to find out the power of a number raised to another</li>
</ul>
<p>The above functions are defined in the <a class="reference external" href="https://docs.python.org/3/library/math.html">math module</a>.</p>
<p>Besides the above, we use the <cite>is_integer()</cite> function defined on a floating point object to check
if the floating point object is actually an integer.</p>
<p>The latest version of the code is available <a class="reference external" href="https://github.com/doingmathwithpython/code/blob/master/explorations/trailing_zeros/trailing_zeros.py">here</a>.</p>
Doing Math with Python in Python Humble Bundle2019-08-23T00:00:00+10:002019-08-23T00:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2019-08-23:/humble-bundle-python.html<p class="first last">Humble bundle Python</p>
<p>"Doing Math with Python" is part of No Starch Press's <a class="reference external" href="https://www.humblebundle.com/books/python-programming-no-starch-books">Python Humble Bundle</a>.
Of course, you get No Starch Press's other excellent Python books as part of the bundle. It's still on for the next 10 days!</p>
<div class="figure align-left">
<img alt="Humble Bundle" src="http://doingmathwithpython.github.io/images/python-humble-bundle.png" />
</div>
<p>Your purchases will help support the No Starch Foundation and Python Software Foundation.</p>
<p>Get the bundle <a class="reference external" href="https://www.humblebundle.com/books/python-programming-no-starch-books">here</a>.</p>
Doing Math with Python in Coder's Bookshelf Humble Bundle2019-03-29T00:00:00+10:002019-03-29T00:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2019-03-29:/humble-bundle-coders-bookshelf.html<p class="first last">Humble bundle</p>
<p>"Doing Math with Python" is part of No Starch Press's "Pay what you want" <a class="reference external" href="https://www.humblebundle.com/books/coders-bookshelf-books">Coder's Bookshelf Bundle</a>.
Your purchases will help support a charity of your choice.</p>
<div class="figure align-center">
<img alt="Humble Bundle" src="http://doingmathwithpython.github.io/images/humble-bundle-3.png" />
</div>
<p>Get the bundle <a class="reference external" href="https://www.humblebundle.com/books/coders-bookshelf-books">here</a>!</p>
Doing Math with Python in Linux Geek Humble Bundle2018-07-24T13:20:00+10:002018-07-24T13:20:00+10:00Amit Sahatag:doingmathwithpython.github.io,2018-07-24:/humble-bundle-linux-geek.html<p class="first last">Humble bundle</p>
<p>"Doing Math with Python" is part of No Starch Press's "Pay what you want" <a class="reference external" href="https://www.humblebundle.com/books/linux-geek-books">Linux Geek Humble Bundle</a>
running for the next 7 days. Your purchases will help support EFF or a charity of your choice.</p>
<div class="figure align-center">
<img alt="Humble Bundle" src="http://doingmathwithpython.github.io/images/humble-bundle-2.png" />
</div>
<p>Get the bundle <a class="reference external" href="https://www.humblebundle.com/books/linux-geek-books">here</a>!</p>
Anaconda 5.0 release2017-10-29T19:50:00+10:002017-10-29T19:50:00+10:00Amit Sahatag:doingmathwithpython.github.io,2017-10-29:/anaconda-5.0.html<p class="first last">Anaconda 5.0 release</p>
<p><a class="reference external" href="https://www.anaconda.com/blog/developer-blog/announcing-the-release-of-anaconda-distribution-5-0/">Anaconda 5.0</a>
was released a few days back. I tried all the <a class="reference external" href="http://doingmathwithpython.github.io/trying-out-solutions.html">sample solutions</a>
and everything works as expected. The <a class="reference external" href="http://doingmathwithpython.github.io/pages/programs.html">chapter programs</a> should
keep working as well.</p>
<p>The versions of the relevant software in this release are:</p>
<ul class="simple">
<li>Python 3.6</li>
<li>sympy 1.1.1</li>
<li>matplotlib 2.1.0</li>
</ul>
<p>You can update your existing installation using:</p>
<pre class="code literal-block">
$ conda update conda
$ conda install anaconda=5.0
</pre>
<p>(Thanks to Dan Wolfe for informing me of the incorrect command to install <cite>anaconda=5.0</cite>)</p>
<p>A fresh installation of Anaconda 5.0 should still be similar to the <a class="reference external" href="https://doingmathwithpython.github.io/pages/software-installation.html">instructions</a>
for earlier versions.</p>
<div class="figure align-center">
<img alt="Anaconda 5" src="http://doingmathwithpython.github.io/images/anaconda-5.png" />
</div>
<div class="figure align-center">
<img alt="Anaconda 5" src="http://doingmathwithpython.github.io/images/anaconda-install-1.png" />
</div>
<div class="figure align-center">
<img alt="Anaconda 5" src="http://doingmathwithpython.github.io/images/anaconda-install-2.png" />
</div>
<div class="figure align-center">
<img alt="Anaconda 5" src="http://doingmathwithpython.github.io/images/anaconda-install-3.png" />
</div>
<p>I have so far verified both on Mac OS X and Linux. If you find any
issues on Windows, please email me at
<tt class="docutils literal">doingmathwithpython@gmail.com</tt> or post your query/tip to any of the
following community forums:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
</ul>
Doing Math with Python Humble Bundle2017-04-06T08:20:00+10:002017-04-06T08:20:00+10:00Amit Sahatag:doingmathwithpython.github.io,2017-04-06:/humble-bundle.html<p class="first last">Humble bundle</p>
<p>No Starch Press has launched a "Pay what you want" <a class="reference external" href="https://www.humblebundle.com/books/python-book-bundle">Python Humble Bundle</a> running from April 5th - April 19th!
Your purchases will help support the Python Software Foundation and I am excited to announce that Doing Math with Python is part of it.</p>
<div class="figure align-center">
<img alt="Humble Bundle" src="http://doingmathwithpython.github.io/images/humble-bundle.png" />
</div>
<p>For 1+ USD you can get three books including "Doing Math with Python"! For 15+ USD you get nine excellent Python books!</p>
<p>Get the bundle <a class="reference external" href="https://www.humblebundle.com/books/python-book-bundle">here</a>!</p>
Trying out the code on Ubuntu 16.042016-10-05T09:00:00+10:002016-10-05T09:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-10-05:/install-on-ubuntu16.04.html<p class="first last">Code on Ubuntu 15.04</p>
<p>If you are using Ubuntu 16.04 and don't want to install the Anaconda
Python distribution for trying out the book's
<a class="reference external" href="http://doingmathwithpython.github.io/pages/programs.html">programs</a> or
the <a class="reference external" href="http://doingmathwithpython.github.io/trying-out-solutions.html">sample solutions</a>, this
post is for you.</p>
<p>Ubuntu 16.04 already comes with Python 3 installed, so we only need to install
the following packages - matplotlib, matplotlib-venn, sympy and idle3.</p>
<p>Open a terminal and do the following:</p>
<pre class="code literal-block">
$ sudo apt-get update
$ sudo apt-get install python3-matplotlib python3-matplotlib-venn python3-sympy idle3
</pre>
<p>It's worth noting that this will install sympy 0.7.6 and matplotlib 1.5.1 which are
both sufficient for the book's programs.</p>
<div class="section" id="starting-idle-editor">
<h2>Starting IDLE editor</h2>
<p>You can now start the IDLE editor by typing in "idle3" from the terminal and then it's ready
for your programs!</p>
</div>
<div class="section" id="contact">
<h2>Contact</h2>
<p>If you find any issues please email me at
<tt class="docutils literal">doingmathwithpython@gmail.com</tt> or post your query/tip to any of the
following community forums:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
</ul>
</div>
Video: Doing Math with Python2016-08-16T12:00:00+10:002016-08-16T12:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-08-16:/video-pyconau-dmwp.html<p class="first last">PyConAU2016</p>
<p>I spoke at the PyCon Australia Education Seminar on August 12, 2016. The video of the talk is now up.
Thanks to Next Day Video!</p>
<div class="youtube youtube-16x9"><iframe src="https://www.youtube.com/embed/XJOt4QQgx0A" allowfullscreen seamless frameBorder="0"></iframe></div><p>The PDF slides and the demos as Jupyter Notebooks (Python 3) are <a class="reference external" href="https://github.com/doingmathwithpython/pycon-au-2016">here</a>. I have some instructions to download them and try them out. If you face any issues, or have a question, please let me know.</p>
<p>Thank you to all the PyCon Australia organizers and the Education seminar organizers for a great mini conference
and the opportunity to be a part of it.</p>
PyCon Australia 2016 Education Seminar2016-08-12T11:27:00+10:002016-08-12T11:27:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-08-12:/pycon-au-edu-summit-talk.html<p class="first last">PyCon AU education summit talk</p>
<p>I attended the PyCon Australia 2016 Education seminar held on 12th August at Melbourne, Australia.
I loved the energy of the seminar and was great to hear about all the ways educators in Australia are
using Python for their teaching. Here are some notes I took, which I also link with the videos. You can
find all the videos <a class="reference external" href="https://www.youtube.com/playlist?list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR">here</a>.</p>
<div class="section" id="python-at-monash">
<h2>Python at Monash</h2>
<ul class="simple">
<li>Faculty of IT</li>
<li>Python close to pseudocode</li>
<li>Jupyter Notebooks</li>
<li>Jupyter Hub installation at Monash</li>
</ul>
</div>
<div class="section" id="keynote-smart-city-from-earth-to-mars-and-back">
<h2>Keynote: Smart City - From earth to mars and back</h2>
<ul class="simple">
<li><a class="reference external" href="https://www.youtube.com/watch?v=BZExUKogvjQ&list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&index=8">Video</a></li>
<li>Find a social mission</li>
</ul>
</div>
<div class="section" id="teaching-python">
<h2>Teaching Python</h2>
<ul class="simple">
<li><a class="reference external" href="https://www.youtube.com/watch?v=7oIwVjEVn0c&list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&index=9">Video</a></li>
<li>Teaching isn't easy</li>
<li>Programming isn't easy, it's new</li>
<li>Grok learning, interactivepython.org, coursera, codecadaemy..</li>
<li>Emphasise the fundamentals, don't assume anything, be explicit</li>
</ul>
</div>
<div class="section" id="python-with-minecraft">
<h2>Python with Minecraft</h2>
<ul class="simple">
<li><a class="reference external" href="https://www.youtube.com/watch?v=WwKkA9YV1K8&list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&index=7">Video</a></li>
<li>Get kids excited</li>
<li>Consumers to creators</li>
</ul>
</div>
<div class="section" id="micropython">
<h2>MicroPython</h2>
<ul class="simple">
<li><a class="reference external" href="https://www.youtube.com/watch?v=oCEZyJqkMrE&list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&index=6">Video</a></li>
<li>Groklearning Microbit</li>
<li><a class="reference external" href="https://groklearning.com/competition/codequest-microbit-2016/">https://groklearning.com/competition/codequest-microbit-2016/</a></li>
</ul>
</div>
<div class="section" id="teaching-geometry-using-logo-python-turtle-module">
<h2>Teaching Geometry using Logo/Python turtle module</h2>
<ul class="simple">
<li><a class="reference external" href="https://www.youtube.com/watch?v=gu3QDizt-_Y&list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&index=5">Video</a></li>
<li>Don't teach subjects in silos</li>
<li>Show students you can do real useful stuff with programming</li>
<li>Turtle powered Geometry</li>
<li>Grok learning lessons (turtle in your browser)</li>
</ul>
</div>
PyCon Australia 2016 Education Seminar: Doing Math with Python2016-08-06T18:00:00+10:002016-08-06T18:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-08-06:/pyconau-dmwp.html<p class="first last">PyConAU2016</p>
<p>Hello everyone, I will be <a class="reference external" href="https://2016.pycon-au.org/schedule/83/view_talk?day=friday">speaking</a> at the PyCon AU education seminar coming August 12, 2016 at Melbourne, Australia (3.00 - 3.40 PM).</p>
<p>I have put up my in progress slides (PDF) and the demos as Jupyter Notebooks (Python 3), I plan to use during the talk on <a class="reference external" href="https://github.com/doingmathwithpython/pycon-au-2016">GitHub</a>. If you are coming along, please let me know if there is anything specific I can share and discuss.</p>
<p><strong>Links</strong></p>
<ul class="simple">
<li><a class="reference external" href="https://2016.pycon-au.org/schedule/83/view_talk?day=friday">Talk abstract</a></li>
<li><a class="reference external" href="https://github.com/doingmathwithpython/pycon-au-2016">Slides and Demos</a></li>
</ul>
O'Reilly Webcast: Doing Math with Python2016-07-01T14:00:00+10:002016-07-01T14:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-07-01:/oreilly-webcast-doing-math.html<p class="first last">O'Reilly Webcast</p>
<p><strong>Updated post after the webcast</strong></p>
<p>A big thank you to all of you who turned up for the webcast across the world. I really had a great time and hope the session was informative to all of you. For those who didn't make it to the webcast, it's now available for <a class="reference external" href="http://www.oreilly.com/pub/e/3712">viewing</a>.</p>
<p>The slides, transcript and the demos are all available at the <a class="reference external" href="https://github.com/doingmathwithpython/oreilly-webcast-2016">GitHub repository</a>. Feel free to use them in any capacity you find useful. If you already have <a class="reference external" href="https://doingmathwithpython.github.io/pages/software-installation.html">Anaconda installed</a>, get the above code, and run <cite>jupyter-notebook</cite> from the same directory to be able to play with the code.</p>
<p><em>Links of Interest</em></p>
<ul class="simple">
<li><a class="reference external" href="http://www.oreilly.com/pub/e/3712">Webcast Recording</a></li>
<li><a class="reference external" href="https://github.com/doingmathwithpython/oreilly-webcast-2016">Slides, Transcript and Demos</a></li>
<li><a class="reference external" href="https://www.nostarch.com/doingmathwithpython">Doing Math with Python book</a></li>
</ul>
<p>Some of you asked a number of questions which I couldn't answer as well as I would have wanted to during the webcast, so I will make a better attempt below:</p>
<p><strong>Q: What is the difference between an interpreter, ide and text editor? And what do you recommend for beginners?</strong></p>
<p>An <tt class="docutils literal">interpreter</tt> is what runs your program. Without going into the details, the Python interpreter is what converts a statement such as <tt class="docutils literal"><span class="pre">print("Hello")</span></tt> to a form that can be understood by the computer to finally print <tt class="docutils literal">Hello</tt> on screen.
An <tt class="docutils literal">IDE</tt> or Integrated Development Environment is a software application where we can write programs and run them usually via Graphical User Interface. IDEs generally feature helpful features such as code completion and can be useful when working with large projects. A <tt class="docutils literal">text editor</tt> is for writing our programs or other text. It usually doesn't support features that an IDE would support but of course, you can configure and enhance text editors to give you IDE-like features.</p>
<p>For beginners, I recommend starting with text editors. I think that doesn't overwhelm someone who is learning with the IDE features. That said, <a class="reference external" href="https://docs.python.org/3/library/idle.html">IDLE</a> is a good in-between choice for beginners and one that I personally use in the book and definitely would be my first choice.</p>
<p><strong>Q: What library do we use for importing metric units that works well with sympy?</strong></p>
<p>I would recommend taking a look at SymPy's <a class="reference external" href="http://docs.sympy.org/dev/modules/physics/units.html#">Units</a> module and see if it has what you need. In addition, and if you already don't know about it, <a class="reference external" href="https://github.com/hgrecco/pint">pint</a> would be another library to look at.</p>
<p><strong>Q: Do you use notebook for exploratory work?</strong></p>
<p>Yes, I use notebook for exploratory work. I think notebooks are great when you want the code and result together in a single document. It's great for sharing too. I recently created <a class="reference external" href="http://echorand.me/presentation-slides-with-jupyter-notebook.html#.V3XhNe0yphE">slides</a> as a Notebook.</p>
<p><strong>Q: Can Sympy be used for the development in a engineering software (i.e. Finite Element Method)? Would the computational speed be (good)? (Not sure about the second part of the question)</strong></p>
<p>You may be interested in taking a look at <a class="reference external" href="http://sfepy.org/doc-devel/index.html">SfePy</a></p>
<p><strong>Q: Thoughts on Cython? Sagemath?</strong></p>
<p>I haven't worked much with Cython. I know about it and what it can useful for. So, I guess if you are looking for speed, definitely look into it. I would also recommend looking at <a class="reference external" href="http://numba.pydata.org/">Numba</a>. Sagemath is more of a system itself than a library. It integrates popular Python libraries and would definitely be something to explore.</p>
<p><strong>Q: Should students use IDLE or a notebook format?</strong></p>
<p>I would recommend using IDLE to start with. It gives the opportunity for the student to at least get an idea of the cycle of editing code and running it. I would only introduce notebook later and in addition to IDLE. Both have their merits, but Notebook just introduces one more thing to grasp in the beginning.</p>
<p><strong>Q: Any recommendations for introducing 3D graphics e.g. polyhedrons on screen?</strong></p>
<p>I haven't explored them, you may want to look at <a class="reference external" href="https://pi3d.github.io/html/">Pi3D</a> or <a class="reference external" href="http://vpython.org/">VPython</a>.</p>
<p><strong>Q: How well do SymPy and Numpy work together?</strong></p>
<p>No personal experience, but searching a bit, it looks like you may want to look at SymPy's <a class="reference external" href="http://docs.sympy.org/dev/modules/utilities/lambdify.html">Lambdify</a> feature. The <a class="reference external" href="https://groups.google.com/forum/#!forum/sympy">SymPy google group</a> may give you a better answer.</p>
<p><strong>Q: You are working in Jupyter - can the "app"s you should be embedded in a regular HTML web page?</strong></p>
<p>I haven't personally tried this. I think this <a class="reference external" href="https://jakevdp.github.io/blog/2013/12/05/static-interactive-widgets/">post</a> may give you clues to do it. O'Reilly Media's project <a class="reference external" href="https://github.com/oreillymedia/thebe">thebe</a> may be another option to look at.</p>
<p><strong>Announcement post</strong></p>
<p>I am very excited to share that I am doing a webcast this coming week with O'Reilly titled
"Doing Math with Python". You can register for it on the <a class="reference external" href="http://www.oreilly.com/pub/e/3712">event page</a>.</p>
<p>Here are the date and time of the webcast:</p>
<ul class="simple">
<li>Wed, June 29th at 7 PM, San Francisco</li>
<li>Wed, June 29th at 10pm, New York</li>
<li>Thu, Jun 30th at 3am - London</li>
<li>Thu, Jun 30th at 7:30am - Mumbai</li>
<li>Thu, Jun 30th at 10am - Beijing</li>
<li>Thu, Jun 30th at 11am - Tokyo</li>
<li>Thu, Jun 30th at 12pm - Sydney</li>
</ul>
<p>I have created a <a class="reference external" href="https://github.com/doingmathwithpython/oreilly-webcast-2016">GitHub repository</a> which
will have the rough transcript, final slides and the code examples as Jupyter Notebooks.</p>
Python 2016 Education Summit Notes2016-05-29T17:00:00+10:002016-05-29T17:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-05-29:/education-summit-pycon-2016.html<p class="first last">Education Summit Summary</p>
<p>I participated in the education summit today. My talk slides for "Doing Math with Python" is available <a class="reference external" href="https://doingmathwithpython.github.io/pycon-us-2016/#/">here</a>.</p>
<p>Here are some notes on the other talks I attended.</p>
<div class="section" id="keynote">
<h2>Keynote</h2>
<ul class="simple">
<li>Education WG</li>
<li>PythonZero</li>
<li>GPIO Zero</li>
<li>Network zero: <a class="reference external" href="https://github.com/tjguk/networkzero/">https://github.com/tjguk/networkzero/</a></li>
<li>Zero: based on an established package, emphasis is on up-and-running use in a classroom, Relevant error messages</li>
</ul>
<p>Micro:bit</p>
<ul class="simple">
<li>Mu, demos</li>
</ul>
</div>
<div class="section" id="lessons-learned-from-teaching-python">
<h2>Lessons learned from teaching Python</h2>
<ul class="simple">
<li>Put your work out there on the internet</li>
<li>Think about internationalization</li>
<li>Self publish Amazon's create space, Kindle</li>
<li>Quizzes, collect data, data mining</li>
<li>Instructor section</li>
<li>Online markup of code</li>
<li>EpicPen</li>
<li>YouTube channel</li>
<li>Libraries: Pygame, Arcade</li>
</ul>
</div>
<div class="section" id="pyzero">
<h2>Pyzero</h2>
<ul class="simple">
<li>Demos</li>
<li>pzrun</li>
</ul>
</div>
<div class="section" id="minecraft-with-python">
<h2>Minecraft with Python</h2>
<ul class="simple">
<li>Use Python to interact with Minecraft</li>
<li>CoderDojo Minecraft bundle</li>
<li>Using Jupyter Notebooks</li>
</ul>
</div>
<div class="section" id="pycharm-edu">
<h2>PyCharm Edu</h2>
<ul class="simple">
<li>Courses</li>
<li>Checkout PyCharm EDU for creating courses</li>
</ul>
</div>
<div class="section" id="teaching-data-structures-with-python">
<h2>Teaching data structures with Python</h2>
<ul class="simple">
<li>Python makes the teacher happy</li>
<li>Lab only</li>
<li>Algorithms in Python + C (Side by side)</li>
<li>Two languages worked well for them.</li>
<li>Low level language: easy to find the complexity of the algorithm</li>
<li>High level language: hard to find the complexity of the algorithm</li>
</ul>
</div>
<div class="section" id="merlin-for-data-science-education">
<h2>Merlin for Data Science Education</h2>
<ul class="simple">
<li>Where to even start?</li>
<li>Effort justification</li>
<li>Spending 2hr out of a 8hr session in fixing something is not worth it</li>
<li>Shouldn't be proud of battling with tool set rather than not doing the real work</li>
<li>Merlin - <a class="reference external" href="http://www.darklabsdatascience.com/project_merlin/">http://www.darklabsdatascience.com/project_merlin/</a></li>
</ul>
</div>
PyCon Education Summit Talk2016-05-26T11:27:00+10:002016-05-26T11:27:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-05-26:/pycon-edu-summit-talk.html<p class="first last">PyCon education summit talk</p>
<p>Hi everyone, I have uploaded the <a class="reference external" href="https://doingmathwithpython.github.io/pycon-us-2016/">slides</a> for my upcoming talk at the PyCon Education Summit. If you are coming to the talk, feel free to have a look at the slides and have any questions/comments ready for me.</p>
<p>The slides are made using Jupyter Notebook + nbconvert magic. Thank you to everyone who makes these things happen. You can see the slides notebook <a class="reference external" href="https://github.com/doingmathwithpython/pycon-us-2016">here</a>.</p>
<p>As a PyCon special, No Starch Press has setup a discount code <tt class="docutils literal">PYCONMATH</tt> code which will give you 30 % off my book, <a class="reference external" href="https://www.nostarch.com/doingmathwithpython">Doing Math with Python</a> and is valid from May 26th - June 8th.</p>
SymPy 1.0 and Anaconda 4.0 releases2016-04-11T19:50:00+10:002016-04-11T19:50:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-04-11:/sympy-1.0-anaconda-4.0.html<p class="first last">sympy 1.0 and Anaconda 4.0</p>
<p><a class="reference external" href="http://sympy.org">SymPy 1.0</a> was released recently and <a class="reference external" href="https://www.continuum.io/blog/developer-blog/anaconda-4-release">Anaconda 4.0</a>
was just released. I tried all the <a class="reference external" href="http://doingmathwithpython.github.io/trying-out-solutions.html">sample solutions</a>
and everything works as expected. The <a class="reference external" href="http://doingmathwithpython.github.io/pages/programs.html">chapter programs</a> should
keep working as well.</p>
<p>You can get both the updates when you install Anaconda 4.0 or updated
your existing Anaconda installation:</p>
<pre class="code literal-block">
$ conda update conda
$ conda update anaconda
</pre>
<p>I have so far verified both on Mac OS X and Linux. If you find any
issues on Windows, please email me at
<tt class="docutils literal">doingmathwithpython@gmail.com</tt> or post your query/tip to any of the
following community forums:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
</ul>
What readers are saying2016-02-14T10:00:00+10:002016-02-14T10:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2016-02-14:/what-readers-are-saying.html<p class="first last">Reviews of Doing Math with Python</p>
<p>Readers have shared how they are finding <em>Doing Math with Python</em> by
posting reviews on Amazon and their own blog. You can view all of them
on the <a class="reference external" href="http://doingmathwithpython.github.io/pages/reviews.html">Reviews</a> page.</p>
<p>Some readers have also been kind enough to let me know personally how the book
has helped them to restart their programming, or looking at something
they have been putting off. As the author, I think this is the highest
level of appreciation that I could have hoped for.</p>
<p>Recently, Aaron Meurer (the lead developer of SymPy) mentioned the
book in an <a class="reference external" href="http://pythonpodcast.com/aaron-meurer-sympy.html">episode</a> of
Podcast.__init__ titled "SymPy with Aaron Meurer". If you are curious
to learn more about SymPy, I would recommend listening to it.</p>
<p>I am curious to hear more. If you want to get in touch personally,
please do so via any of the following channels:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
<li><a class="reference external" href="https://twitter.com/mathwithpython">Twitter</a></li>
</ul>
<p>You can email me at <a class="reference external" href="mailto:doingmathwithpython@gmail.com">doingmathwithpython@gmail.com</a>.</p>
<p>Alternatively, if you just plan to write a review, please do so on
Amazon, O'Reilly or your personal blog.</p>
Trying out the solutions in IDLE2015-11-18T08:20:00+10:002015-11-18T08:20:00+10:00Amit Sahatag:doingmathwithpython.github.io,2015-11-18:/trying-out-solutions.html<p class="first last">Trying out the solutions</p>
<p>Once you <a class="reference external" href="https://www.nostarch.com/download/doingmath_code.zip">download</a> the solutions ZIP file, and extract it you will
find the solutions for each chapter in the corresponding sub-directory.</p>
<div class="figure align-center">
<img alt="Extracted ZIP archive" src="http://doingmathwithpython.github.io/images/zip-extracted.png" />
</div>
<p>The <strong>PDF</strong> file contains explanations for each of the solutions
similar to the explanations for the programs in the book.</p>
<p>Before you can try the programs out, you will have to open them first in IDLE.
Let's consider the solution to a challenge posed in Chapter 6 to draw
the Mandelbrot set - <tt class="docutils literal">mandelbrot.py</tt>. Start <tt class="docutils literal">IDLE</tt> and click on the menu item <tt class="docutils literal">File >
Open</tt> and navigate to the location where you extracted the directory
above and open the file <tt class="docutils literal">mandelbrot.py</tt>.</p>
<div class="figure align-center">
<img alt="IDLE window" src="http://doingmathwithpython.github.io/images/idle-1.png" />
<p class="caption">Snapshot of the source code</p>
</div>
<div class="section" id="running-the-program">
<h2>Running the program</h2>
<p>To run the program, click on <tt class="docutils literal">Run > Run Module</tt> and you should see
the Mandelbrot set in the matplotlib window.</p>
<div class="figure align-center">
<img alt="Mandelbrot Set" src="http://doingmathwithpython.github.io/images/idle-2.png" />
<p class="caption">Mandelbrot set</p>
</div>
<p>All the solutions should be ready to run, try them out, make changes
to experiment and let me know what you come up with!</p>
<p>Email me at <tt class="docutils literal">doingmathwithpython@gmail.com</tt> or post your query/tip to any of the
following community forums:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
</ul>
</div>
Breaking long lines in Python2015-11-04T12:00:00+10:002015-11-04T12:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2015-11-04:/breaking-long-lines-in-python.html<p class="first last">Breaking long lines in Python</p>
<p>In some of the programs discussed in the book including the sample solutions, you will see statements like:</p>
<pre class="code literal-block">
print('Area: {0}, Estimated ({1}): {2}'.
format(area_of_circle, points, estimate(radius, points)))
</pre>
<p>This is really the following single statement:</p>
<pre class="code literal-block">
print('Area: {0}, Estimated ({1}): {2}'.format(area_of_circle, points, estimate(radius, points)))
</pre>
<p>The first code snippet above is an example of breaking a long line into two (or more) lines so that we don't end up with really long lines in our code. How long should a line be when you should think about breaking it? If your statement's length is more than 80 characters, you should think about breaking it up.</p>
<p>In the book, we often had to do so because of layout reasons even though the statement may not have exceeded 80 characters, and in your projects you will want to do it so that your statements are easier to read and on the average all lines have a similar length. This is formalized (among other things) in <a class="reference external" href="https://www.python.org/dev/peps/pep-0008/">PEP 8</a>.</p>
<p>Note that the examples below will for illustrative purposes break lines waaaaay less than 80 characters.</p>
<div class="section" id="how-do-you-break">
<h2>How do you break?</h2>
<div class="section" id="when-not-calling-function">
<h3>When not calling function</h3>
<p>When you are not calling a function, you essentially have two choices:</p>
<p><strong>Use paranthesis</strong></p>
<p>This is exactly how we break the long statement in the example we started this article with. For the moment ignore the call to <tt class="docutils literal">print()</tt> and assume that the statement is:</p>
<pre class="code literal-block">
s = 'Area: {0}, Estimated ({1}): {2}'.format(area_of_circle, points, estimate(radius, points))
</pre>
<p>This essentially just creates the string <tt class="docutils literal">s</tt>. If we were to split this statement over multiple lines, we would do the following:</p>
<pre class="code literal-block">
s = ('Area: {0}, Estimated ({1}): {2}'
.format(area_of_circle, points, estimate(radius, points)))
</pre>
<p>Note the extra beginning and the ending parenthesis.</p>
<p>Here is another example:</p>
<pre class="code literal-block">
s1 = x + x**2/2 + x**3/3 + x**4/4 + x**5/5 + x**6/6 + x**7/7 + x**8/8
</pre>
<p>Here is how we can use split the above statment into multiple lines using parentheses:</p>
<pre class="code literal-block">
s3 = (x + x**2/2 + x**3/3
+ x**4/4 + x**5/5
+ x**6/6 + x**7/7
+ x**8/8)
</pre>
<p><strong>Use the line continuation operator</strong></p>
<p>The line continuation operator, <tt class="docutils literal">\</tt> can be used to split long statements over multiple lines. Here is how we could split the above statement using <tt class="docutils literal">\</tt> instead:</p>
<pre class="code literal-block">
s3 = x + x**2/2 + x**3/3 \
+ x**4/4 + x**5/5 \
+ x**6/6 + x**7/7 \
+ x**8/8
</pre>
<p>At the end of every line (except the last), we just add a <tt class="docutils literal">\</tt> indicating that the next line is also a part of the same statement.</p>
<p><strong>Breaking up those long if statements</strong></p>
<p>Often I have to break long <tt class="docutils literal">if</tt> statements and is in fact one of the most common cases I face at work where I have to break the statement into multiple lines. Here is an example using both the approaches above:</p>
<pre class="code literal-block">
# Using parenthesis
if (cond1 and cond2 and cond3
and cond4):
# True block
else:
# False block
# Using line continuation operator
if cond1 and cond2 and cond3 \
and cond4:
# True block
else:
# False block
</pre>
</div>
<div class="section" id="when-calling-functions">
<h3>When calling functions</h3>
<p>By default, when calling functions you can just press enter and without doing anything more keep writing your statement over multiple lines. For example:</p>
<pre class="code literal-block">
x = 1
print(x,
x)
</pre>
<p>Hence, we <cite>could</cite> have broken the first example we saw as:</p>
<pre class="code literal-block">
print('Area: {0}, Estimated ({1}): {2}'.format(area_of_circle,
points,
estimate(radius, points)))
</pre>
<p>When calling <tt class="docutils literal">format()</tt> we put the arguments over separate lines.</p>
</div>
</div>
<div class="section" id="learning-more-about-python-coding-style">
<h2>Learning more about Python coding style</h2>
<p>If you liked reading this article, you may also find it worth your time going over the <a class="reference external" href="https://www.python.org/dev/peps/pep-0008/">Python style guide</a>. You may even find instances where I have not followed a guideline when writing the programs in the book. If you find one, let me know.</p>
</div>
<div class="section" id="getting-in-touch">
<h2>Getting in touch</h2>
<p>Stay updated or get in touch:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
<li><a class="reference external" href="https://twitter.com/mathwithpython">Twitter</a></li>
</ul>
<p>You can contact me directly via:</p>
<ul class="simple">
<li>Twitter: <a class="reference external" href="https://twitter.com/mathwithpython">@mathwithpython</a></li>
<li>Email : <a class="reference external" href="mailto:doingmathwithpython@gmail.com">doingmathwithpython@gmail.com</a></li>
</ul>
</div>
Chapter code and Errata2015-09-11T08:20:00+10:002015-09-11T08:20:00+10:00Amit Sahatag:doingmathwithpython.github.io,2015-09-11:/chapter-code-errata.html<p class="first last">Chapter code and errata</p>
<p>You can find the chapter programs and snippets linked from the <a class="reference external" href="http://doingmathwithpython.github.io/pages/programs.html">programs</a> page. They should be free
from any error mentioned on the <a class="reference external" href="http://doingmathwithpython.github.io/pages/errata.html">errata</a> page.</p>
<div class="section" id="stay-in-touch">
<h2>Stay in touch</h2>
<p>You can stay connected with the book, its readers and me via the
following channels:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
<li><a class="reference external" href="https://twitter.com/mathwithpython">Twitter</a></li>
</ul>
<p>You can contact me directly via:</p>
<ul class="simple">
<li>Twitter: <a class="reference external" href="https://twitter.com/mathwithpython">@mathwithpython</a></li>
<li>Email : <a class="reference external" href="mailto:doingmathwithpython@gmail.com">doingmathwithpython@gmail.com</a></li>
</ul>
</div>
Set operations with Python set compared to SymPy's FiniteSet2015-09-05T23:00:00+10:002015-09-05T23:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2015-09-05:/Sets-in-SymPy-and-built-in-Python-sets.html<p class="first last">Sets in SymPy and built-in Python sets</p>
<p><cite>Chapter 5</cite> (<a class="reference external" href="http://doingmathwithpython.github.io/pages/about.html">About</a>) of the book discusses working with mathematical sets in
Python. While writing the chapter, I had a choice of whether to
use Python 3's built-in <a class="reference external" href="https://docs.python.org/3.3/library/stdtypes.html?highlight=union#set-types-set-frozenset">set</a> data
structure or use SymPy's (0.7.6 +) <tt class="docutils literal">FiniteSet</tt> class. I decided to go ahead
with the latter. My choice is briefly explained towards the end of
this post, but hopefully it will be clear before that.</p>
<p>Next, I describe how you can use Python 3's built-in set data
structure to create sets and perform set operations such as finding
the union, intersection or cartesian product of sets. For comparison,
I also show how you can do the same using SymPy's <tt class="docutils literal">FiniteSet</tt> class.</p>
<div class="section" id="creating-a-set">
<h2>Creating a set</h2>
<p>We can create a set consisting of the elements <cite>{1, 2, 3}</cite> in Python 3
as follows:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="o">>>></span> <span class="n">s1</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
</pre></div>
<p>To create a set when the elements are already in a list (for
example), we would use the following syntax:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">items</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">s2</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">items</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s2</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
</pre></div>
<p>The comparative operations using SymPy's <tt class="docutils literal">FiniteSet</tt> class are:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">FiniteSet</span>
<span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s1</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="o">>>></span> <span class="n">items</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">s2</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="o">*</span><span class="n">items</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s2</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
</pre></div>
<p>To create an <a class="reference external" href="https://en.wikipedia.org/wiki/Empty_set">empty set</a>,
in Python 3 you would use create an empty <tt class="docutils literal">set</tt> object:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">e</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">e</span>
<span class="nb">set</span><span class="p">()</span>
</pre></div>
<p>In SymPy, an empty set is represented by an <tt class="docutils literal">EmptySet</tt> object. Thus,
you can either create an empty set by directly creating an
<tt class="docutils literal">EmptySet</tt> object or by creating a <tt class="docutils literal">FiniteSet</tt> object without
specifying any set members, like so:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">EmptySet</span>
<span class="o">>>></span> <span class="n">e</span> <span class="o">=</span> <span class="n">EmptySet</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">e</span>
<span class="n">EmptySet</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">e</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">e</span>
<span class="n">EmptySet</span><span class="p">()</span>
</pre></div>
</div>
<div class="section" id="cardinality-and-membership">
<h2>Cardinality and Membership</h2>
<p>The <tt class="docutils literal">len()</tt> function returns the number of set members for sets
created using either of the above approaches.</p>
<p>Similarly, to check if an item <tt class="docutils literal">x</tt> is present in a set, <tt class="docutils literal">s</tt>
created using any of the above approaches, we can use the statement,
<tt class="docutils literal">x in s</tt>.</p>
</div>
<div class="section" id="union-and-intersection">
<h2>Union and intersection</h2>
<p>The <tt class="docutils literal">union()</tt> method can be used in both cases to find the union of
two or more sets:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">s2</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">s3</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">s1</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">s3</span><span class="p">)</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">}</span>
</pre></div>
<p>Similary in the case of SymPy:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">FiniteSet</span>
<span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s2</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s3</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s1</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">s3</span><span class="p">)</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">}</span>
</pre></div>
<p>The <tt class="docutils literal">intersection()</tt> method can be used to find the intersection of
two or more sets created using either of the above approaches. Continuing
with the above three sets:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">s2</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">s3</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">s1</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">s3</span><span class="p">)</span>
<span class="p">{</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
</pre></div>
<p>Similary, in SymPy:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">s3</span><span class="p">)</span>
<span class="p">{</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
</pre></div>
</div>
<div class="section" id="cartesian-product">
<h2>Cartesian product</h2>
<p>To find the cartesian product of sets created via the built-in <tt class="docutils literal">set</tt>
data structure, we have to use the <tt class="docutils literal">product()</tt> function in the
<a class="reference external" href="https://docs.python.org/3/library/itertools.html#itertools.product">itertools</a>
module:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="o">>>></span> <span class="n">s2</span> <span class="o">=</span> <span class="p">{</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">}</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">itertools</span>
<span class="o">>>></span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">)</span>
<span class="o"><</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span> <span class="nb">object</span> <span class="n">at</span> <span class="mh">0x10418c990</span><span class="o">></span>
</pre></div>
<p>However considering that the <cite>cartesian product</cite> of two sets <a class="reference external" href="http://mathinsight.org/definition/cartesian_product">should</a> be another set,
the <tt class="docutils literal">product()</tt> function doesn't really then return the
cartesian product itself, but (an iterator to) the elements in it. Hence, if we
try to apply the result returned by the function directly to a method or
function which is expected to be applicable to a set, it will fail. For
example, <tt class="docutils literal">itertools.product(s1, <span class="pre">s2).union(s3)</span></tt> will result in an error, but
<tt class="docutils literal">set(itertools.product(s1, <span class="pre">s2)).union(s3)</span></tt> will work.</p>
<p>Using SymPy's <tt class="docutils literal">FiniteSet</tt>, you can use the <tt class="docutils literal">*</tt>
(multiplication or product) operator to find the cartesian product
and the result is a set itself. Thus, it is closer to what
a cartesian product is mathematically. An example follows:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s2</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="o">>>></span> <span class="o">>>></span> <span class="n">s3</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span>
<span class="o">>>></span> <span class="p">(</span><span class="n">s1</span><span class="o">*</span><span class="n">s2</span><span class="p">)</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">s3</span><span class="p">)</span>
<span class="p">{</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">}</span> <span class="n">U</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span> <span class="n">x</span> <span class="p">{</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">}</span>
</pre></div>
<p><strong>Cartesian product of a set with itself</strong></p>
<p>To find the cartesian product of a set with itself, i.e. <cite>s1*s1</cite> for
example, we pass in a keyword argument, <tt class="docutils literal">repeat</tt> while calling the
<tt class="docutils literal">itertools.product()</tt> function. The value of <tt class="docutils literal">repeat</tt> is the
<cite>power</cite> we want to raise the set to. Thus, <tt class="docutils literal">itertools.product(s1,
repeat=2)</tt> will calculate the cartesian product, <cite>s1*s1</cite>:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="o">>>></span> <span class="nb">set</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
<span class="p">{(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)}</span>
</pre></div>
<p>In SymPy, the <tt class="docutils literal">**</tt> operator can be used for finding the cartesian
product of a set with itself:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s1</span><span class="o">**</span><span class="mi">2</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span> <span class="n">x</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
</pre></div>
</div>
<div class="section" id="subset-super-set-proper-subset-checking">
<h2>Subset/super set/proper subset checking</h2>
<p>The <tt class="docutils literal">issubset()</tt> and <tt class="docutils literal">issuperset()</tt> methods are available for sets
created via either approaches to check if a set is a subset and super
set of another, respectively. Thus, <tt class="docutils literal">s1.issubset(s2)</tt> will check if
<cite>s1</cite> is a subset of <cite>s2</cite>.</p>
<p><strong>Checking for proper subset and superset</strong></p>
<p>To check if a set, <cite>s1</cite> is a <a class="reference external" href="http://mathworld.wolfram.com/ProperSubset.html">proper subset</a> of another set,
<cite>s2</cite> when using built-in set, we can do the following:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
<span class="o">>>></span> <span class="n">s2</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">}</span>
<span class="o">>>></span> <span class="n">s1</span><span class="o">.</span><span class="n">issubset</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span> <span class="ow">and</span> <span class="n">s1</span> <span class="o">!=</span> <span class="n">s2</span>
<span class="kc">True</span>
</pre></div>
<p>We can do something similar for <a class="reference external" href="http://mathinsight.org/definition/proper_superset">proper superset</a>.</p>
<p>In SymPy, we have <tt class="docutils literal">is_proper_subset()</tt> and <tt class="docutils literal">is_proper_superset()</tt>
methods which can be used to check if a set is a proper subset or
superset of another, respectively. Thus, the above would be written as
<tt class="docutils literal">s1.is_proper_subset(s2)</tt>.</p>
</div>
<div class="section" id="calculating-the-powerset">
<h2>Calculating the powerset</h2>
<p>For sets created via built-in <tt class="docutils literal">set</tt> data structure, there is no
direct method available to create the <a class="reference external" href="https://www.mathsisfun.com/sets/power-set.html">power set</a>. However, you can use the
<tt class="docutils literal">powerset</tt> recipe described in the <a class="reference external" href="https://docs.python.org/3/library/itertools.html#recipes">itertools documentation</a>.</p>
<p>On the other hand, in SymPy, there is a <tt class="docutils literal">powerset()</tt> method
available which returns the power set:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">s1</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">s1</span><span class="o">.</span><span class="n">powerset</span><span class="p">()</span>
<span class="p">{</span><span class="n">EmptySet</span><span class="p">(),</span> <span class="p">{</span><span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="mi">2</span><span class="p">},</span> <span class="p">{</span><span class="mi">3</span><span class="p">},</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">},</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">},</span> <span class="p">{</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">},</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}}</span>
</pre></div>
<p>You can see that the <tt class="docutils literal">powerset()</tt> method returns the power <cite>set</cite> and not the
elements in it.</p>
</div>
<div class="section" id="choice-of-sympy-s-finiteset-over-set">
<h2>Choice of SymPy's <tt class="docutils literal">FiniteSet</tt> over <tt class="docutils literal">set</tt></h2>
<p>From the above comparison, we can see that SymPy's <tt class="docutils literal">FiniteSet</tt>
provides us with nice features such as being able to use the <tt class="docutils literal">*</tt>
operator to find the cartesian product, <tt class="docutils literal">**</tt> operator to calculate
the cartesian product with itself and <tt class="docutils literal">powerset()</tt> method for calculating the
power set. These are not present when using the built-in <tt class="docutils literal">set</tt> data
structure. This was certainly a big driving factor in my choice,
since SymPy was also being used in other chapters of the book.</p>
<p>However, a <em>key</em> reason for my choice was that I wanted to show how we
can create sets which did not allow addition or removal once created -
like mathematical sets. This need was fulfilled by SymPy's
<tt class="docutils literal">FiniteSet</tt> since it used Python's <tt class="docutils literal">frozenset</tt> data structure and
not the <tt class="docutils literal">set</tt> data sturcture.</p>
<p>The alternative to that would have
been to use <tt class="docutils literal">frozenset</tt> directly, but I just did not like the idea
of it and I would have also missed out on the nice features
<tt class="docutils literal">FiniteSet</tt> would provide (eventually). I should note here that once
I had made the decision to go with <tt class="docutils literal">FiniteSet</tt>, I <a class="reference external" href="https://github.com/amitsaha/sympy/commits?author=amitsaha">contributed</a> patches
to SymPy to make the methods of <tt class="docutils literal">FiniteSet</tt> more compatible with Python's built in set
and also implement minor features I discussed above.</p>
</div>
Doing Math with Python Available now!2015-09-02T08:00:00+10:002015-09-02T08:00:00+10:00Amit Sahatag:doingmathwithpython.github.io,2015-09-02:/available-now.html<p class="first last">Available now!</p>
<p>Hi all, I am very excited to announce that the book is now available for
purchase in print and electronic formats from various online stores
including <a class="reference external" href="http://www.amazon.com/Doing-Math-Python-Programming-Statistics/dp/1593276400">Amazon</a>
and <a class="reference external" href="https://www.nostarch.com/doingmathwithpython">No Starch Press</a>.
Please see the <a class="reference external" href="http://doingmathwithpython.github.io/pages/buy.html">Buy</a> page for others.</p>
<a class="reference external image-reference" href="http://www.nostarch.com/doingmathwithpython"><img alt="Book cover" class="align-center" src="http://doingmathwithpython.github.io/images/cover.png" /></a>
<p>If you are keen to take a look at the contents and read a sample
chapter, please head over to <a class="reference external" href="https://www.nostarch.com/doingmathwithpython">No Starch's book page</a>.</p>
<p>Alternatively, if you are keen to recieve a review copy, please email
<cite>doingmathwithpython@gmail.com</cite> and I will try to request one from the
publishers.</p>
<div class="section" id="stay-in-touch">
<h2>Stay in touch</h2>
<p>You can stay connected with the book, its readers and me via the
following channels:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
<li><a class="reference external" href="https://twitter.com/mathwithpython">Twitter</a></li>
</ul>
<p>You can contact me directly via:</p>
<ul class="simple">
<li>Twitter: <a class="reference external" href="https://twitter.com/mathwithpython">@mathwithpython</a></li>
<li>Email : <a class="reference external" href="mailto:doingmathwithpython@gmail.com">doingmathwithpython@gmail.com</a></li>
</ul>
</div>
All chapters completed, off to the printers2015-08-15T23:27:00+10:002015-08-15T23:27:00+10:00Amit Sahatag:doingmathwithpython.github.io,2015-08-15:/off-to-printers.html<p class="first last">off to printers!</p>
<p>I am very excited to write that all the chapters has been completed
and the book is currently with the printers! You can find out more
about the contents (including a detailed table of contents) from the
<a class="reference external" href="http://doingmathwithpython.github.io/pages/about.html">About</a> page.</p>
<p>You can stay connected with the book, its readers and me via the
following channels:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
<li><a class="reference external" href="https://twitter.com/mathwithpython">Twitter</a></li>
</ul>
Introduction to "Doing Math with Python"2015-05-24T23:27:00+10:002015-05-24T23:27:00+10:00Amit Sahatag:doingmathwithpython.github.io,2015-05-24:/hello-world.html<p class="first last">Hello World</p>
<p>Hi all, this is the blog for my book "Doing Math with Python".</p>
<a class="reference external image-reference" href="http://www.nostarch.com/doingmathwithpython"><img alt="Book cover" class="align-center" src="http://doingmathwithpython.github.io/images/cover.png" /></a>
<p>The first six chapters of the book are already available via the
publisher's early access program. You can learn briefly about each
chapter on the <a class="reference external" href="http://doingmathwithpython.github.io/pages/about.html">About</a> page.
Going forward, I will be sharing updates regarding the book and posting
original content related to that discussed in the book.</p>
<p>You can stay connected with the book, its readers and me via the
following channels:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.facebook.com/doingmathwithpython">Facebook page</a></li>
<li><a class="reference external" href="https://plus.google.com/u/0/communities/113121562865298236232">G+ Community</a></li>
<li><a class="reference external" href="https://twitter.com/mathwithpython">Twitter</a></li>
</ul>