-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathall.atom.xml
More file actions
855 lines (846 loc) · 98.6 KB
/
all.atom.xml
File metadata and controls
855 lines (846 loc) · 98.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Doing Math with Python</title><link href="http://doingmathwithpython.github.io/" rel="alternate"></link><link href="http://doingmathwithpython.github.io/feeds/all.atom.xml" rel="self"></link><id>http://doingmathwithpython.github.io/</id><updated>2023-10-25T13:20:00+10:00</updated><entry><title>Doing Math with Python in Data Science Humble Bundle</title><link href="http://doingmathwithpython.github.io/humble-bundle-data-science.html" rel="alternate"></link><published>2023-10-25T13:20:00+10:00</published><updated>2023-10-25T13:20:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2023-10-25:/humble-bundle-data-science.html</id><summary type="html"><p class="first last">Humble bundle</p>
</summary><content type="html"><p>&quot;Doing Math with Python&quot; 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>
</content><category term="updates"></category></entry><entry><title>Chapter 3 - Google Correlate example update</title><link href="http://doingmathwithpython.github.io/chapter-3-google-correlate-example-update.html" rel="alternate"></link><published>2020-07-11T00:00:00+10:00</published><updated>2020-07-11T00:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2020-07-11:/chapter-3-google-correlate-example-update.html</id><summary type="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></summary><content type="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 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>
</content><category term="updates"></category></entry><entry><title>Coding Starter Kit Humble Bundle</title><link href="http://doingmathwithpython.github.io/humble-bundle-coding-starter.html" rel="alternate"></link><published>2020-03-17T00:00:00+10:00</published><updated>2020-03-17T00:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2020-03-17:/humble-bundle-coding-starter.html</id><summary type="html"><p>I am very excited to share that &quot;Doing Math with Python&quot; 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&amp;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></summary><content type="html"><p>I am very excited to share that &quot;Doing Math with Python&quot; 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&amp;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&amp;mc_eid=a8fa0cb420">here</a>.</p>
</content><category term="updates"></category></entry><entry><title>Number of trailing zeros in the factorial of an integer</title><link href="http://doingmathwithpython.github.io/trailing-zeros-factorial.html" rel="alternate"></link><published>2020-01-02T19:50:00+10:00</published><updated>2020-01-02T19:50:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2020-01-02:/trailing-zeros-factorial.html</id><summary type="html"><p class="first last">Use Python to find the number of trailing zeros in the factorial of an integer</p>
</summary><content type="html"><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 &quot;calculators&quot; in the book, we will
write a &quot;number of trailing zero&quot; 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 &gt; 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(&quot;Factorial of a non-positive non-integer is undefined&quot;)
if __name__ == &quot;__main__&quot;:
fact_num = input(
&quot;Enter the number whose factorial's trailing zeros you want to find: &quot;
)
num_zeros = trailing_zeros(fact_num)
print(&quot;Number of trailing zeros: {0}&quot;.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>
</content><category term="articles"></category></entry><entry><title>Doing Math with Python in Python Humble Bundle</title><link href="http://doingmathwithpython.github.io/humble-bundle-python.html" rel="alternate"></link><published>2019-08-23T00:00:00+10:00</published><updated>2019-08-23T00:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2019-08-23:/humble-bundle-python.html</id><summary type="html"><p class="first last">Humble bundle Python</p>
</summary><content type="html"><p>&quot;Doing Math with Python&quot; 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>
</content><category term="updates"></category></entry><entry><title>Doing Math with Python in Coder's Bookshelf Humble Bundle</title><link href="http://doingmathwithpython.github.io/humble-bundle-coders-bookshelf.html" rel="alternate"></link><published>2019-03-29T00:00:00+10:00</published><updated>2019-03-29T00:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2019-03-29:/humble-bundle-coders-bookshelf.html</id><summary type="html"><p class="first last">Humble bundle</p>
</summary><content type="html"><p>&quot;Doing Math with Python&quot; is part of No Starch Press's &quot;Pay what you want&quot; <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>
</content><category term="updates"></category></entry><entry><title>Doing Math with Python in Linux Geek Humble Bundle</title><link href="http://doingmathwithpython.github.io/humble-bundle-linux-geek.html" rel="alternate"></link><published>2018-07-24T13:20:00+10:00</published><updated>2018-07-24T13:20:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2018-07-24:/humble-bundle-linux-geek.html</id><summary type="html"><p class="first last">Humble bundle</p>
</summary><content type="html"><p>&quot;Doing Math with Python&quot; is part of No Starch Press's &quot;Pay what you want&quot; <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>
</content><category term="updates"></category></entry><entry><title>Anaconda 5.0 release</title><link href="http://doingmathwithpython.github.io/anaconda-5.0.html" rel="alternate"></link><published>2017-10-29T19:50:00+10:00</published><updated>2017-10-29T19:50:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2017-10-29:/anaconda-5.0.html</id><summary type="html"><p class="first last">Anaconda 5.0 release</p>
</summary><content type="html"><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&#64;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>
</content><category term="updates"></category></entry><entry><title>Doing Math with Python Humble Bundle</title><link href="http://doingmathwithpython.github.io/humble-bundle.html" rel="alternate"></link><published>2017-04-06T08:20:00+10:00</published><updated>2017-04-06T08:20:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2017-04-06:/humble-bundle.html</id><summary type="html"><p class="first last">Humble bundle</p>
</summary><content type="html"><p>No Starch Press has launched a &quot;Pay what you want&quot; <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 &quot;Doing Math with Python&quot;! 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>
</content><category term="updates"></category></entry><entry><title>Trying out the code on Ubuntu 16.04</title><link href="http://doingmathwithpython.github.io/install-on-ubuntu16.04.html" rel="alternate"></link><published>2016-10-05T09:00:00+10:00</published><updated>2016-10-05T09:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-10-05:/install-on-ubuntu16.04.html</id><summary type="html"><p class="first last">Code on Ubuntu 15.04</p>
</summary><content type="html"><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 &quot;idle3&quot; 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&#64;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>
</content><category term="tips"></category></entry><entry><title>Video: Doing Math with Python</title><link href="http://doingmathwithpython.github.io/video-pyconau-dmwp.html" rel="alternate"></link><published>2016-08-16T12:00:00+10:00</published><updated>2016-08-16T12:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-08-16:/video-pyconau-dmwp.html</id><summary type="html"><p class="first last">PyConAU2016</p>
</summary><content type="html"><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>
</content><category term="updates"></category></entry><entry><title>PyCon Australia 2016 Education Seminar</title><link href="http://doingmathwithpython.github.io/pycon-au-edu-summit-talk.html" rel="alternate"></link><published>2016-08-12T11:27:00+10:00</published><updated>2016-08-12T11:27:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-08-12:/pycon-au-edu-summit-talk.html</id><summary type="html"><p class="first last">PyCon AU education summit talk</p>
</summary><content type="html"><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&amp;list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&amp;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&amp;list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&amp;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&amp;list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&amp;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&amp;list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&amp;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&amp;list=PLs4CJRBY5F1Jh6fFqT1p5TZRx5q06CcaR&amp;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>
</content><category term="updates"></category></entry><entry><title>PyCon Australia 2016 Education Seminar: Doing Math with Python</title><link href="http://doingmathwithpython.github.io/pyconau-dmwp.html" rel="alternate"></link><published>2016-08-06T18:00:00+10:00</published><updated>2016-08-06T18:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-08-06:/pyconau-dmwp.html</id><summary type="html"><p class="first last">PyConAU2016</p>
</summary><content type="html"><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>
</content><category term="updates"></category></entry><entry><title>O'Reilly Webcast: Doing Math with Python</title><link href="http://doingmathwithpython.github.io/oreilly-webcast-doing-math.html" rel="alternate"></link><published>2016-07-01T14:00:00+10:00</published><updated>2016-07-01T14:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-07-01:/oreilly-webcast-doing-math.html</id><summary type="html"><p class="first last">O'Reilly Webcast</p>
</summary><content type="html"><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(&quot;Hello&quot;)</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 &quot;app&quot;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
&quot;Doing Math with Python&quot;. 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>
</content><category term="updates"></category></entry><entry><title>Python 2016 Education Summit Notes</title><link href="http://doingmathwithpython.github.io/education-summit-pycon-2016.html" rel="alternate"></link><published>2016-05-29T17:00:00+10:00</published><updated>2016-05-29T17:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-05-29:/education-summit-pycon-2016.html</id><summary type="html"><p class="first last">Education Summit Summary</p>
</summary><content type="html"><p>I participated in the education summit today. My talk slides for &quot;Doing Math with Python&quot; 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>
</content><category term="updates"></category></entry><entry><title>PyCon Education Summit Talk</title><link href="http://doingmathwithpython.github.io/pycon-edu-summit-talk.html" rel="alternate"></link><published>2016-05-26T11:27:00+10:00</published><updated>2016-05-26T11:27:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-05-26:/pycon-edu-summit-talk.html</id><summary type="html"><p class="first last">PyCon education summit talk</p>
</summary><content type="html"><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>
</content><category term="updates"></category></entry><entry><title>SymPy 1.0 and Anaconda 4.0 releases</title><link href="http://doingmathwithpython.github.io/sympy-1.0-anaconda-4.0.html" rel="alternate"></link><published>2016-04-11T19:50:00+10:00</published><updated>2016-04-11T19:50:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-04-11:/sympy-1.0-anaconda-4.0.html</id><summary type="html"><p class="first last">sympy 1.0 and Anaconda 4.0</p>
</summary><content type="html"><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&#64;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>
</content><category term="updates"></category></entry><entry><title>What readers are saying</title><link href="http://doingmathwithpython.github.io/what-readers-are-saying.html" rel="alternate"></link><published>2016-02-14T10:00:00+10:00</published><updated>2016-02-14T10:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2016-02-14:/what-readers-are-saying.html</id><summary type="html"><p class="first last">Reviews of Doing Math with Python</p>
</summary><content type="html"><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 &quot;SymPy with Aaron Meurer&quot;. 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&#64;gmail.com">doingmathwithpython&#64;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>
</content><category term="updates"></category></entry><entry><title>Trying out the solutions in IDLE</title><link href="http://doingmathwithpython.github.io/trying-out-solutions.html" rel="alternate"></link><published>2015-11-18T08:20:00+10:00</published><updated>2015-11-18T08:20:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2015-11-18:/trying-out-solutions.html</id><summary type="html"><p class="first last">Trying out the solutions</p>
</summary><content type="html"><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 &gt;
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 &gt; 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&#64;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>
</content><category term="updates"></category></entry><entry><title>Breaking long lines in Python</title><link href="http://doingmathwithpython.github.io/breaking-long-lines-in-python.html" rel="alternate"></link><published>2015-11-04T12:00:00+10:00</published><updated>2015-11-04T12:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2015-11-04:/breaking-long-lines-in-python.html</id><summary type="html"><p class="first last">Breaking long lines in Python</p>
</summary><content type="html"><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">&#64;mathwithpython</a></li>
<li>Email : <a class="reference external" href="mailto:doingmathwithpython&#64;gmail.com">doingmathwithpython&#64;gmail.com</a></li>
</ul>
</div>
</content><category term="articles"></category></entry><entry><title>Chapter code and Errata</title><link href="http://doingmathwithpython.github.io/chapter-code-errata.html" rel="alternate"></link><published>2015-09-11T08:20:00+10:00</published><updated>2015-09-11T08:20:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2015-09-11:/chapter-code-errata.html</id><summary type="html"><p class="first last">Chapter code and errata</p>
</summary><content type="html"><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">&#64;mathwithpython</a></li>
<li>Email : <a class="reference external" href="mailto:doingmathwithpython&#64;gmail.com">doingmathwithpython&#64;gmail.com</a></li>
</ul>
</div>
</content><category term="updates"></category></entry><entry><title>Set operations with Python set compared to SymPy's FiniteSet</title><link href="http://doingmathwithpython.github.io/Sets-in-SymPy-and-built-in-Python-sets.html" rel="alternate"></link><published>2015-09-05T23:00:00+10:00</published><updated>2015-09-05T23:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2015-09-05:/Sets-in-SymPy-and-built-in-Python-sets.html</id><summary type="html"><p class="first last">Sets in SymPy and built-in Python sets</p>
</summary><content type="html"><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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">FiniteSet</span>
<span class="o">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="n">e</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
<span class="o">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">EmptySet</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">e</span> <span class="o">=</span> <span class="n">EmptySet</span><span class="p">()</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">e</span>
<span class="n">EmptySet</span><span class="p">()</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">e</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">()</span>
<span class="o">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">FiniteSet</span>
<span class="o">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="kn">import</span> <span class="nn">itertools</span>
<span class="o">&gt;&gt;&gt;</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">&lt;</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">&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="o">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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>
</content><category term="articles"></category></entry><entry><title>Doing Math with Python Available now!</title><link href="http://doingmathwithpython.github.io/available-now.html" rel="alternate"></link><published>2015-09-02T08:00:00+10:00</published><updated>2015-09-02T08:00:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2015-09-02:/available-now.html</id><summary type="html"><p class="first last">Available now!</p>
</summary><content type="html"><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&#64;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">&#64;mathwithpython</a></li>
<li>Email : <a class="reference external" href="mailto:doingmathwithpython&#64;gmail.com">doingmathwithpython&#64;gmail.com</a></li>
</ul>
</div>
</content><category term="updates"></category></entry><entry><title>All chapters completed, off to the printers</title><link href="http://doingmathwithpython.github.io/off-to-printers.html" rel="alternate"></link><published>2015-08-15T23:27:00+10:00</published><updated>2015-08-15T23:27:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2015-08-15:/off-to-printers.html</id><summary type="html"><p class="first last">off to printers!</p>
</summary><content type="html"><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>
</content><category term="updates"></category></entry><entry><title>Introduction to "Doing Math with Python"</title><link href="http://doingmathwithpython.github.io/hello-world.html" rel="alternate"></link><published>2015-05-24T23:27:00+10:00</published><updated>2015-05-24T23:27:00+10:00</updated><author><name>Amit Saha</name></author><id>tag:doingmathwithpython.github.io,2015-05-24:/hello-world.html</id><summary type="html"><p class="first last">Hello World</p>
</summary><content type="html"><p>Hi all, this is the blog for my book &quot;Doing Math with Python&quot;.</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>
</content><category term="updates"></category></entry></feed>