From: Maximilian Friedersdorff Date: Fri, 24 May 2019 13:04:06 +0000 (+0100) Subject: Some notes and excercises on generators and comprehensions X-Git-Url: https://git.friedersdorff.com/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=max%2Fcomprehensions_and_generators.git Some notes and excercises on generators and comprehensions --- 04222fa0c35f377653abe2f4f7f50f04f35de768 diff --git a/comprehensions_and_generators.pdf b/comprehensions_and_generators.pdf new file mode 100644 index 0000000..abfeada --- /dev/null +++ b/comprehensions_and_generators.pdf @@ -0,0 +1,982 @@ +%PDF-1.4 +%“Œ‹ž ReportLab Generated PDF document http://www.reportlab.com +1 0 obj +<< +/F1 2 0 R /F2 3 0 R /F3 4 0 R /F4 5 0 R /F5 7 0 R +>> +endobj +2 0 obj +<< +/BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font +>> +endobj +3 0 obj +<< +/BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding /Name /F2 /Subtype /Type1 /Type /Font +>> +endobj +4 0 obj +<< +/BaseFont /Courier /Encoding /WinAnsiEncoding /Name /F3 /Subtype /Type1 /Type /Font +>> +endobj +5 0 obj +<< +/BaseFont /Courier-Bold /Encoding /WinAnsiEncoding /Name /F4 /Subtype /Type1 /Type /Font +>> +endobj +6 0 obj +<< +/Contents 16 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 15 0 R /Resources << +/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] +>> /Rotate 0 /Trans << + +>> + /Type /Page +>> +endobj +7 0 obj +<< +/BaseFont /Helvetica-Oblique /Encoding /WinAnsiEncoding /Name /F5 /Subtype /Type1 /Type /Font +>> +endobj +8 0 obj +<< +/Contents 17 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 15 0 R /Resources << +/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] +>> /Rotate 0 /Trans << + +>> + /Type /Page +>> +endobj +9 0 obj +<< +/Outlines 11 0 R /PageLabels 18 0 R /PageMode /UseNone /Pages 15 0 R /Type /Catalog +>> +endobj +10 0 obj +<< +/Author () /CreationDate (D:20190524140358+00'00') /Creator (\(unspecified\)) /Keywords () /ModDate (D:20190524140358+00'00') /Producer (ReportLab PDF Library - www.reportlab.com) + /Subject (\(unspecified\)) /Title () /Trapped /False +>> +endobj +11 0 obj +<< +/Count 3 /First 12 0 R /Last 14 0 R /Type /Outlines +>> +endobj +12 0 obj +<< +/Dest [ 6 0 R /XYZ 62.69291 765.0236 0 ] /Next 13 0 R /Parent 11 0 R /Title (List Comprehensions) +>> +endobj +13 0 obj +<< +/Dest [ 6 0 R /XYZ 62.69291 369.2236 0 ] /Next 14 0 R /Parent 11 0 R /Prev 12 0 R /Title (Dictionary Comprehensions) +>> +endobj +14 0 obj +<< +/Dest [ 6 0 R /XYZ 62.69291 193.0236 0 ] /Parent 11 0 R /Prev 13 0 R /Title (Generators) +>> +endobj +15 0 obj +<< +/Count 2 /Kids [ 6 0 R 8 0 R ] /Type /Pages +>> +endobj +16 0 obj +<< +/Length 19408 +>> +stream +1 0 0 1 0 0 cm BT /F1 12 Tf 14.4 TL ET +q +1 0 0 1 62.69291 744.0236 cm +q +BT 1 0 0 1 0 3.5 Tm 21 TL /F2 17.5 Tf 0 0 0 rg (List Comprehensions) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 714.0236 cm +q +0 0 0 rg +BT 1 0 0 1 0 14 Tm /F1 10 Tf 12 TL .641235 Tw (List comprehensions are declarative ways of defining lists in python. The following ways of building a list) Tj T* 0 Tw (are equivalent:) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 632.8236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 72 re B* +Q +q +.960784 .960784 .862745 rg +n 0 48 6 12 re f* +.960784 .960784 .862745 rg +n 12 48 6 12 re f* +.960784 .960784 .862745 rg +n 24 48 6 12 re f* +.960784 .960784 .862745 rg +n 30 48 6 12 re f* +.960784 .960784 .862745 rg +n 36 48 12 12 re f* +.960784 .960784 .862745 rg +n 48 48 6 12 re f* +.960784 .960784 .862745 rg +n 60 48 18 12 re f* +.960784 .960784 .862745 rg +n 84 48 6 12 re f* +.960784 .960784 .862745 rg +n 96 48 12 12 re f* +.960784 .960784 .862745 rg +n 114 48 30 12 re f* +.960784 .960784 .862745 rg +n 144 48 6 12 re f* +.960784 .960784 .862745 rg +n 150 48 6 12 re f* +.960784 .960784 .862745 rg +n 156 48 6 12 re f* +.960784 .960784 .862745 rg +n 168 48 12 12 re f* +.960784 .960784 .862745 rg +n 180 48 12 12 re f* +.960784 .960784 .862745 rg +n 0 24 6 12 re f* +.960784 .960784 .862745 rg +n 12 24 6 12 re f* +.960784 .960784 .862745 rg +n 24 24 12 12 re f* +.960784 .960784 .862745 rg +n 0 12 18 12 re f* +.960784 .960784 .862745 rg +n 24 12 6 12 re f* +.960784 .960784 .862745 rg +n 36 12 12 12 re f* +.960784 .960784 .862745 rg +n 54 12 30 12 re f* +.960784 .960784 .862745 rg +n 84 12 6 12 re f* +.960784 .960784 .862745 rg +n 90 12 6 12 re f* +.960784 .960784 .862745 rg +n 96 12 6 12 re f* +.960784 .960784 .862745 rg +n 108 12 12 12 re f* +.960784 .960784 .862745 rg +n 120 12 12 12 re f* +.960784 .960784 .862745 rg +n 24 0 6 12 re f* +.960784 .960784 .862745 rg +n 30 0 6 12 re f* +.960784 .960784 .862745 rg +n 36 0 36 12 re f* +.960784 .960784 .862745 rg +n 72 0 6 12 re f* +.960784 .960784 .862745 rg +n 78 0 6 12 re f* +.960784 .960784 .862745 rg +n 84 0 12 12 re f* +.960784 .960784 .862745 rg +n 96 0 6 12 re f* +.960784 .960784 .862745 rg +n 102 0 6 12 re f* +BT 1 0 0 1 0 50 Tm 12 TL /F3 10 Tf 0 0 0 rg (l) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg ([) Tj 0 0 0 rg (x) Tj .4 .4 .4 rg (**) Tj .4 .4 .4 rg (2) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10) Tj 0 0 0 rg (\)]) Tj 0 0 0 rg T* T* 0 0 0 rg (l) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg ([]) Tj 0 0 0 rg T* /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10) Tj 0 0 0 rg (\):) Tj 0 0 0 rg T* ( ) Tj 0 0 0 rg (l) Tj .4 .4 .4 rg (.) Tj 0 0 0 rg (append) Tj 0 0 0 rg (\() Tj 0 0 0 rg (x) Tj .4 .4 .4 rg (**) Tj .4 .4 .4 rg (2) Tj 0 0 0 rg (\)) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 539.6236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 84 re B* +Q +q +.960784 .960784 .862745 rg +n 0 60 6 12 re f* +.960784 .960784 .862745 rg +n 12 60 6 12 re f* +.960784 .960784 .862745 rg +n 24 60 6 12 re f* +.960784 .960784 .862745 rg +n 30 60 6 12 re f* +.960784 .960784 .862745 rg +n 42 60 18 12 re f* +.960784 .960784 .862745 rg +n 66 60 6 12 re f* +.960784 .960784 .862745 rg +n 78 60 12 12 re f* +.960784 .960784 .862745 rg +n 96 60 30 12 re f* +.960784 .960784 .862745 rg +n 126 60 6 12 re f* +.960784 .960784 .862745 rg +n 132 60 6 12 re f* +.960784 .960784 .862745 rg +n 138 60 6 12 re f* +.960784 .960784 .862745 rg +n 150 60 12 12 re f* +.960784 .960784 .862745 rg +n 162 60 6 12 re f* +.960784 .960784 .862745 rg +n 174 60 12 12 re f* +.960784 .960784 .862745 rg +n 192 60 6 12 re f* +.960784 .960784 .862745 rg +n 204 60 6 12 re f* +.960784 .960784 .862745 rg +n 216 60 6 12 re f* +.960784 .960784 .862745 rg +n 228 60 12 12 re f* +.960784 .960784 .862745 rg +n 246 60 6 12 re f* +.960784 .960784 .862745 rg +n 252 60 6 12 re f* +.960784 .960784 .862745 rg +n 0 36 6 12 re f* +.960784 .960784 .862745 rg +n 12 36 6 12 re f* +.960784 .960784 .862745 rg +n 24 36 12 12 re f* +.960784 .960784 .862745 rg +n 0 24 18 12 re f* +.960784 .960784 .862745 rg +n 24 24 6 12 re f* +.960784 .960784 .862745 rg +n 36 24 12 12 re f* +.960784 .960784 .862745 rg +n 54 24 30 12 re f* +.960784 .960784 .862745 rg +n 84 24 6 12 re f* +.960784 .960784 .862745 rg +n 90 24 6 12 re f* +.960784 .960784 .862745 rg +n 96 24 6 12 re f* +.960784 .960784 .862745 rg +n 108 24 12 12 re f* +.960784 .960784 .862745 rg +n 120 24 12 12 re f* +.960784 .960784 .862745 rg +n 24 12 12 12 re f* +.960784 .960784 .862745 rg +n 42 12 6 12 re f* +.960784 .960784 .862745 rg +n 54 12 6 12 re f* +.960784 .960784 .862745 rg +n 66 12 6 12 re f* +.960784 .960784 .862745 rg +n 78 12 12 12 re f* +.960784 .960784 .862745 rg +n 96 12 6 12 re f* +.960784 .960784 .862745 rg +n 102 12 6 12 re f* +.960784 .960784 .862745 rg +n 48 0 6 12 re f* +.960784 .960784 .862745 rg +n 54 0 6 12 re f* +.960784 .960784 .862745 rg +n 60 0 36 12 re f* +.960784 .960784 .862745 rg +n 96 0 6 12 re f* +.960784 .960784 .862745 rg +n 102 0 6 12 re f* +.960784 .960784 .862745 rg +n 108 0 6 12 re f* +BT 1 0 0 1 0 62 Tm 12 TL /F3 10 Tf 0 0 0 rg (m) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg ([) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10) Tj 0 0 0 rg (\)) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (if) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (%) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (2) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (==) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (]) Tj 0 0 0 rg T* T* 0 0 0 rg (m) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg ([]) Tj 0 0 0 rg T* /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10) Tj 0 0 0 rg (\):) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (if) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (%) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (2) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (==) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (:) Tj 0 0 0 rg T* ( ) Tj 0 0 0 rg (m) Tj .4 .4 .4 rg (.) Tj 0 0 0 rg (append) Tj 0 0 0 rg (\() Tj 0 0 0 rg (x) Tj 0 0 0 rg (\)) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 434.4236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 96 re B* +Q +q +.960784 .960784 .862745 rg +n 0 72 6 12 re f* +.960784 .960784 .862745 rg +n 12 72 6 12 re f* +.960784 .960784 .862745 rg +n 24 72 12 12 re f* +.960784 .960784 .862745 rg +n 36 72 6 12 re f* +.960784 .960784 .862745 rg +n 42 72 6 12 re f* +.960784 .960784 .862745 rg +n 54 72 6 12 re f* +.960784 .960784 .862745 rg +n 60 72 6 12 re f* +.960784 .960784 .862745 rg +n 72 72 18 12 re f* +.960784 .960784 .862745 rg +n 96 72 6 12 re f* +.960784 .960784 .862745 rg +n 108 72 12 12 re f* +.960784 .960784 .862745 rg +n 126 72 30 12 re f* +.960784 .960784 .862745 rg +n 156 72 6 12 re f* +.960784 .960784 .862745 rg +n 162 72 6 12 re f* +.960784 .960784 .862745 rg +n 168 72 6 12 re f* +.960784 .960784 .862745 rg +n 180 72 12 12 re f* +.960784 .960784 .862745 rg +n 192 72 6 12 re f* +.960784 .960784 .862745 rg +n 204 72 18 12 re f* +.960784 .960784 .862745 rg +n 228 72 6 12 re f* +.960784 .960784 .862745 rg +n 240 72 12 12 re f* +.960784 .960784 .862745 rg +n 258 72 30 12 re f* +.960784 .960784 .862745 rg +n 288 72 6 12 re f* +.960784 .960784 .862745 rg +n 294 72 6 12 re f* +.960784 .960784 .862745 rg +n 300 72 6 12 re f* +.960784 .960784 .862745 rg +n 312 72 12 12 re f* +.960784 .960784 .862745 rg +n 324 72 6 12 re f* +.960784 .960784 .862745 rg +n 336 72 12 12 re f* +.960784 .960784 .862745 rg +n 354 72 6 12 re f* +.960784 .960784 .862745 rg +n 366 72 6 12 re f* +.960784 .960784 .862745 rg +n 378 72 6 12 re f* +.960784 .960784 .862745 rg +n 384 72 6 12 re f* +.960784 .960784 .862745 rg +n 0 48 6 12 re f* +.960784 .960784 .862745 rg +n 12 48 6 12 re f* +.960784 .960784 .862745 rg +n 24 48 12 12 re f* +.960784 .960784 .862745 rg +n 0 36 18 12 re f* +.960784 .960784 .862745 rg +n 24 36 6 12 re f* +.960784 .960784 .862745 rg +n 36 36 12 12 re f* +.960784 .960784 .862745 rg +n 54 36 30 12 re f* +.960784 .960784 .862745 rg +n 84 36 6 12 re f* +.960784 .960784 .862745 rg +n 90 36 6 12 re f* +.960784 .960784 .862745 rg +n 96 36 6 12 re f* +.960784 .960784 .862745 rg +n 108 36 12 12 re f* +.960784 .960784 .862745 rg +n 120 36 12 12 re f* +.960784 .960784 .862745 rg +n 24 24 18 12 re f* +.960784 .960784 .862745 rg +n 48 24 6 12 re f* +.960784 .960784 .862745 rg +n 60 24 12 12 re f* +.960784 .960784 .862745 rg +n 78 24 30 12 re f* +.960784 .960784 .862745 rg +n 108 24 6 12 re f* +.960784 .960784 .862745 rg +n 114 24 6 12 re f* +.960784 .960784 .862745 rg +n 120 24 6 12 re f* +.960784 .960784 .862745 rg +n 132 24 12 12 re f* +.960784 .960784 .862745 rg +n 144 24 12 12 re f* +.960784 .960784 .862745 rg +n 48 12 12 12 re f* +.960784 .960784 .862745 rg +n 66 12 6 12 re f* +.960784 .960784 .862745 rg +n 78 12 6 12 re f* +.960784 .960784 .862745 rg +n 90 12 6 12 re f* +.960784 .960784 .862745 rg +n 96 12 6 12 re f* +.960784 .960784 .862745 rg +n 72 0 6 12 re f* +.960784 .960784 .862745 rg +n 78 0 6 12 re f* +.960784 .960784 .862745 rg +n 84 0 36 12 re f* +.960784 .960784 .862745 rg +n 120 0 12 12 re f* +.960784 .960784 .862745 rg +n 132 0 6 12 re f* +.960784 .960784 .862745 rg +n 138 0 6 12 re f* +.960784 .960784 .862745 rg +n 150 0 6 12 re f* +.960784 .960784 .862745 rg +n 156 0 12 12 re f* +BT 1 0 0 1 0 74 Tm 12 TL /F3 10 Tf 0 0 0 rg (n) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg ([\() Tj 0 0 0 rg (x) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj 0 0 0 rg (y) Tj 0 0 0 rg (\)) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10) Tj 0 0 0 rg (\)) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (y) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10) Tj 0 0 0 rg (\)) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (if) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (>) Tj 0 0 0 rg ( ) Tj 0 0 0 rg (y) Tj 0 0 0 rg (]) Tj 0 0 0 rg T* T* 0 0 0 rg (n) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg ([]) Tj 0 0 0 rg T* /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10) Tj 0 0 0 rg (\):) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (y) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10) Tj 0 0 0 rg (\):) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (if) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (>) Tj 0 0 0 rg ( ) Tj 0 0 0 rg (y) Tj 0 0 0 rg (:) Tj 0 0 0 rg T* ( ) Tj 0 0 0 rg (n) Tj .4 .4 .4 rg (.) Tj 0 0 0 rg (append) Tj 0 0 0 rg (\(\() Tj 0 0 0 rg (x) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj 0 0 0 rg (y) Tj 0 0 0 rg (\)\)) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 414.4236 cm +q +0 0 0 rg +BT 1 0 0 1 0 2 Tm /F1 10 Tf 12 TL (The general form is:) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 381.2236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 24 re B* +Q +q +.960784 .960784 .862745 rg +n 0 0 6 12 re f* +.960784 .960784 .862745 rg +n 6 0 60 12 re f* +.960784 .960784 .862745 rg +n 72 0 18 12 re f* +.960784 .960784 .862745 rg +n 96 0 6 12 re f* +.960784 .960784 .862745 rg +n 108 0 12 12 re f* +.960784 .960784 .862745 rg +n 126 0 6 12 re f* +.960784 .960784 .862745 rg +n 138 0 54 12 re f* +.960784 .960784 .862745 rg +n 192 0 6 12 re f* +BT 1 0 0 1 0 2 Tm 12 TL /F3 10 Tf 0 0 0 rg ([) Tj 0 0 0 rg (expression) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (y) Tj 0 0 0 rg ( ) Tj 0 0 0 rg (condition) Tj 0 0 0 rg (]) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 348.2236 cm +q +BT 1 0 0 1 0 3.5 Tm 21 TL /F2 17.5 Tf 0 0 0 rg (Dictionary Comprehensions) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 330.2236 cm +q +0 0 0 rg +BT 1 0 0 1 0 2 Tm /F1 10 Tf 12 TL (Dictionary comprehensions are the analogous construct for dictionaries:) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 237.0236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 84 re B* +Q +q +.960784 .960784 .862745 rg +n 0 60 6 12 re f* +.960784 .960784 .862745 rg +n 12 60 6 12 re f* +.960784 .960784 .862745 rg +n 24 60 6 12 re f* +.960784 .960784 .862745 rg +n 30 60 6 12 re f* +.960784 .960784 .862745 rg +n 36 60 6 12 re f* +.960784 .960784 .862745 rg +n 48 60 6 12 re f* +.960784 .960784 .862745 rg +n 54 60 12 12 re f* +.960784 .960784 .862745 rg +n 66 60 6 12 re f* +.960784 .960784 .862745 rg +n 78 60 18 12 re f* +.960784 .960784 .862745 rg +n 102 60 6 12 re f* +.960784 .960784 .862745 rg +n 114 60 12 12 re f* +.960784 .960784 .862745 rg +n 132 60 30 12 re f* +.960784 .960784 .862745 rg +n 162 60 6 12 re f* +.960784 .960784 .862745 rg +n 168 60 6 12 re f* +.960784 .960784 .862745 rg +n 174 60 6 12 re f* +.960784 .960784 .862745 rg +n 186 60 18 12 re f* +.960784 .960784 .862745 rg +n 204 60 6 12 re f* +.960784 .960784 .862745 rg +n 216 60 12 12 re f* +.960784 .960784 .862745 rg +n 234 60 24 12 re f* +.960784 .960784 .862745 rg +n 258 60 6 12 re f* +.960784 .960784 .862745 rg +n 264 60 24 12 re f* +.960784 .960784 .862745 rg +n 288 60 6 12 re f* +.960784 .960784 .862745 rg +n 294 60 6 12 re f* +.960784 .960784 .862745 rg +n 300 60 6 12 re f* +.960784 .960784 .862745 rg +n 306 60 6 12 re f* +.960784 .960784 .862745 rg +n 312 60 60 12 re f* +.960784 .960784 .862745 rg +n 372 60 18 12 re f* +.960784 .960784 .862745 rg +n 0 36 6 12 re f* +.960784 .960784 .862745 rg +n 12 36 6 12 re f* +.960784 .960784 .862745 rg +n 24 36 12 12 re f* +.960784 .960784 .862745 rg +n 0 24 18 12 re f* +.960784 .960784 .862745 rg +n 24 24 6 12 re f* +.960784 .960784 .862745 rg +n 36 24 12 12 re f* +.960784 .960784 .862745 rg +n 54 24 30 12 re f* +.960784 .960784 .862745 rg +n 84 24 6 12 re f* +.960784 .960784 .862745 rg +n 90 24 6 12 re f* +.960784 .960784 .862745 rg +n 96 24 6 12 re f* +.960784 .960784 .862745 rg +n 108 24 18 12 re f* +.960784 .960784 .862745 rg +n 126 24 12 12 re f* +.960784 .960784 .862745 rg +n 24 12 12 12 re f* +.960784 .960784 .862745 rg +n 42 12 24 12 re f* +.960784 .960784 .862745 rg +n 66 12 6 12 re f* +.960784 .960784 .862745 rg +n 72 12 24 12 re f* +.960784 .960784 .862745 rg +n 96 12 6 12 re f* +.960784 .960784 .862745 rg +n 102 12 6 12 re f* +.960784 .960784 .862745 rg +n 108 12 6 12 re f* +.960784 .960784 .862745 rg +n 114 12 6 12 re f* +.960784 .960784 .862745 rg +n 120 12 60 12 re f* +.960784 .960784 .862745 rg +n 180 12 18 12 re f* +.960784 .960784 .862745 rg +n 48 0 6 12 re f* +.960784 .960784 .862745 rg +n 54 0 6 12 re f* +.960784 .960784 .862745 rg +n 60 0 6 12 re f* +.960784 .960784 .862745 rg +n 66 0 6 12 re f* +.960784 .960784 .862745 rg +n 78 0 6 12 re f* +.960784 .960784 .862745 rg +n 90 0 6 12 re f* +.960784 .960784 .862745 rg +n 96 0 12 12 re f* +.960784 .960784 .862745 rg +n 108 0 6 12 re f* +BT 1 0 0 1 0 62 Tm 12 TL /F3 10 Tf 0 0 0 rg (o) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg ({) Tj 0 0 0 rg (x) Tj 0 0 0 rg (:) Tj 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj .4 .4 .4 rg (**) Tj .4 .4 .4 rg (3) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (100) Tj 0 0 0 rg (\)) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (if) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (math) Tj .4 .4 .4 rg (.) Tj 0 0 0 rg (sqrt) Tj 0 0 0 rg (\() Tj 0 0 0 rg (x) Tj 0 0 0 rg (\)) Tj .4 .4 .4 rg (.) Tj 0 0 0 rg (is_integer) Tj 0 0 0 rg (\(\)}) Tj 0 0 0 rg T* T* 0 0 0 rg (o) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg ({}) Tj 0 0 0 rg T* /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (1) Tj 0 0 0 rg (,) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (100) Tj 0 0 0 rg (\):) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (if) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (math) Tj .4 .4 .4 rg (.) Tj 0 0 0 rg (sqrt) Tj 0 0 0 rg (\() Tj 0 0 0 rg (x) Tj 0 0 0 rg (\)) Tj .4 .4 .4 rg (.) Tj 0 0 0 rg (is_integer) Tj 0 0 0 rg (\(\):) Tj 0 0 0 rg T* ( ) Tj 0 0 0 rg (o) Tj 0 0 0 rg ([) Tj 0 0 0 rg (x) Tj 0 0 0 rg (]) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg (x) Tj .4 .4 .4 rg (**) Tj .4 .4 .4 rg (3) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 205.0236 cm +q +0 0 0 rg +BT 1 0 0 1 0 14 Tm /F1 10 Tf 12 TL .227045 Tw (I have never used a dictionary comprehension outside of exercises. I don't know how useful they are, and) Tj T* 0 Tw (I'm struggling to think of good examples.) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 172.0236 cm +q +BT 1 0 0 1 0 3.5 Tm 21 TL /F2 17.5 Tf 0 0 0 rg (Generators) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 154.0236 cm +q +0 0 0 rg +BT 1 0 0 1 0 2 Tm /F1 10 Tf 12 TL (A generator is a function that yields values rather than returning them. For example:) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 96.82362 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 48 re B* +Q +q +.960784 .960784 .862745 rg +n 0 24 18 12 re f* +.960784 .960784 .862745 rg +n 24 24 42 12 re f* +.960784 .960784 .862745 rg +n 66 24 18 12 re f* +.960784 .960784 .862745 rg +n 24 12 6 12 re f* +.960784 .960784 .862745 rg +n 36 12 6 12 re f* +.960784 .960784 .862745 rg +n 48 12 6 12 re f* +.960784 .960784 .862745 rg +n 24 0 30 12 re f* +.960784 .960784 .862745 rg +n 60 0 24 12 re f* +BT 1 0 0 1 0 26 Tm 12 TL /F4 10 Tf 0 .501961 0 rg (def) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 1 rg (squares) Tj 0 0 0 rg (\(\):) Tj 0 0 0 rg T* ( ) Tj 0 0 0 rg (i) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (0) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (while) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (True) Tj 0 0 0 rg T* ET +Q +Q +Q +Q +Q + +endstream +endobj +17 0 obj +<< +/Length 5988 +>> +stream +1 0 0 1 0 0 cm BT /F1 12 Tf 14.4 TL ET +q +1 0 0 1 62.69291 727.8236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 36 re B* +Q +q +.960784 .960784 .862745 rg +n 48 12 30 12 re f* +.960784 .960784 .862745 rg +n 84 12 6 12 re f* +.960784 .960784 .862745 rg +n 90 12 12 12 re f* +.960784 .960784 .862745 rg +n 102 12 6 12 re f* +.960784 .960784 .862745 rg +n 48 0 6 12 re f* +.960784 .960784 .862745 rg +n 60 0 12 12 re f* +.960784 .960784 .862745 rg +n 78 0 6 12 re f* +BT 1 0 0 1 0 14 Tm 12 TL /F3 10 Tf 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (yield) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (i) Tj .4 .4 .4 rg (**) Tj .4 .4 .4 rg (2) Tj 0 0 0 rg T* ( ) Tj 0 0 0 rg (i) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (+=) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (1) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 707.8236 cm +q +BT 1 0 0 1 0 2 Tm 12 TL /F1 10 Tf 0 0 0 rg (The built in ) Tj /F5 10 Tf 0 0 0 rg (range) Tj /F1 10 Tf 0 0 0 rg ( function is similar to a generator. Generators are iterable:) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 638.6236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 60 re B* +Q +q +.960784 .960784 .862745 rg +n 0 36 18 12 re f* +.960784 .960784 .862745 rg +n 24 36 36 12 re f* +.960784 .960784 .862745 rg +n 66 36 12 12 re f* +.960784 .960784 .862745 rg +n 84 36 42 12 re f* +.960784 .960784 .862745 rg +n 126 36 18 12 re f* +.960784 .960784 .862745 rg +n 24 24 30 12 re f* +.960784 .960784 .862745 rg +n 54 24 6 12 re f* +.960784 .960784 .862745 rg +n 60 24 36 12 re f* +.960784 .960784 .862745 rg +n 96 24 6 12 re f* +.960784 .960784 .862745 rg +n 24 12 12 12 re f* +.960784 .960784 .862745 rg +n 42 12 36 12 re f* +.960784 .960784 .862745 rg +n 84 12 6 12 re f* +.960784 .960784 .862745 rg +n 96 12 30 12 re f* +.960784 .960784 .862745 rg +n 126 12 6 12 re f* +.960784 .960784 .862745 rg +n 48 0 30 12 re f* +BT 1 0 0 1 0 38 Tm 12 TL /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (square) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (squares) Tj 0 0 0 rg (\(\):) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (print) Tj /F3 10 Tf 0 0 0 rg (\() Tj 0 0 0 rg (square) Tj 0 0 0 rg (\)) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (if) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (square) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (>) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (10000) Tj 0 0 0 rg (:) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (break) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 618.6236 cm +q +0 0 0 rg +BT 1 0 0 1 0 2 Tm /F1 10 Tf 12 TL (But only once!) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 600.6236 cm +q +0 0 0 rg +BT 1 0 0 1 0 2 Tm /F1 10 Tf 12 TL (Generators can take arguments:) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 519.4236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 72 re B* +Q +q +.960784 .960784 .862745 rg +n 0 48 18 12 re f* +.960784 .960784 .862745 rg +n 24 48 54 12 re f* +.960784 .960784 .862745 rg +n 78 48 6 12 re f* +.960784 .960784 .862745 rg +n 84 48 18 12 re f* +.960784 .960784 .862745 rg +n 102 48 12 12 re f* +.960784 .960784 .862745 rg +n 24 36 6 12 re f* +.960784 .960784 .862745 rg +n 36 36 6 12 re f* +.960784 .960784 .862745 rg +n 48 36 6 12 re f* +.960784 .960784 .862745 rg +n 24 24 30 12 re f* +.960784 .960784 .862745 rg +n 60 24 24 12 re f* +.960784 .960784 .862745 rg +n 48 12 30 12 re f* +.960784 .960784 .862745 rg +n 84 12 6 12 re f* +.960784 .960784 .862745 rg +n 90 12 12 12 re f* +.960784 .960784 .862745 rg +n 102 12 18 12 re f* +.960784 .960784 .862745 rg +n 48 0 6 12 re f* +.960784 .960784 .862745 rg +n 60 0 12 12 re f* +.960784 .960784 .862745 rg +n 78 0 6 12 re f* +BT 1 0 0 1 0 50 Tm 12 TL /F4 10 Tf 0 .501961 0 rg (def) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 1 rg (exponents) Tj 0 0 0 rg (\() Tj 0 0 0 rg (exp) Tj 0 0 0 rg (\):) Tj 0 0 0 rg T* ( ) Tj 0 0 0 rg (i) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (0) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (while) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (True) Tj 0 0 0 rg T* ( ) Tj /F4 10 Tf 0 .501961 0 rg (yield) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (i) Tj .4 .4 .4 rg (**) Tj 0 0 0 rg (exp) Tj 0 0 0 rg T* ( ) Tj 0 0 0 rg (i) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (+=) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (1) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 499.4236 cm +q +0 0 0 rg +BT 1 0 0 1 0 2 Tm /F1 10 Tf 12 TL (Generator expressions are similar to list comprehensions:) Tj T* ET +Q +Q +q +1 0 0 1 62.69291 466.2236 cm +q +q +1 0 0 1 0 0 cm +q +1 0 0 1 6.6 6.6 cm +q +.662745 .662745 .662745 RG +.5 w +.960784 .960784 .862745 rg +n -6 -6 468.6898 24 re B* +Q +q +.960784 .960784 .862745 rg +n 0 0 42 12 re f* +.960784 .960784 .862745 rg +n 48 0 6 12 re f* +.960784 .960784 .862745 rg +n 60 0 6 12 re f* +.960784 .960784 .862745 rg +n 66 0 6 12 re f* +.960784 .960784 .862745 rg +n 72 0 12 12 re f* +.960784 .960784 .862745 rg +n 84 0 6 12 re f* +.960784 .960784 .862745 rg +n 96 0 18 12 re f* +.960784 .960784 .862745 rg +n 120 0 6 12 re f* +.960784 .960784 .862745 rg +n 132 0 12 12 re f* +.960784 .960784 .862745 rg +n 150 0 30 12 re f* +.960784 .960784 .862745 rg +n 180 0 6 12 re f* +.960784 .960784 .862745 rg +n 186 0 30 12 re f* +.960784 .960784 .862745 rg +n 216 0 12 12 re f* +BT 1 0 0 1 0 2 Tm 12 TL /F3 10 Tf 0 0 0 rg (squares) Tj 0 0 0 rg ( ) Tj .4 .4 .4 rg (=) Tj 0 0 0 rg ( ) Tj 0 0 0 rg (\() Tj 0 0 0 rg (i) Tj .4 .4 .4 rg (**) Tj .4 .4 .4 rg (2) Tj 0 0 0 rg ( ) Tj /F4 10 Tf 0 .501961 0 rg (for) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 0 0 rg (i) Tj 0 0 0 rg ( ) Tj /F4 10 Tf .666667 .133333 1 rg (in) Tj /F3 10 Tf 0 0 0 rg ( ) Tj 0 .501961 0 rg (range) Tj 0 0 0 rg (\() Tj .4 .4 .4 rg (10000) Tj 0 0 0 rg (\)\)) Tj T* ET +Q +Q +Q +Q +Q +q +1 0 0 1 62.69291 434.2236 cm +q +0 0 0 rg +BT 1 0 0 1 0 14 Tm /F1 10 Tf 12 TL .57229 Tw (Generators are lazy, values are calculated when needed, meaning they can represent infinite sequences) Tj T* 0 Tw (without requiring infinite memory.) Tj T* ET +Q +Q + +endstream +endobj +18 0 obj +<< +/Nums [ 0 19 0 R 1 20 0 R ] +>> +endobj +19 0 obj +<< +/S /D /St 1 +>> +endobj +20 0 obj +<< +/S /D /St 2 +>> +endobj +xref +0 21 +0000000000 65535 f +0000000073 00000 n +0000000144 00000 n +0000000251 00000 n +0000000363 00000 n +0000000468 00000 n +0000000578 00000 n +0000000783 00000 n +0000000898 00000 n +0000001103 00000 n +0000001208 00000 n +0000001466 00000 n +0000001540 00000 n +0000001660 00000 n +0000001799 00000 n +0000001910 00000 n +0000001976 00000 n +0000021437 00000 n +0000027477 00000 n +0000027527 00000 n +0000027561 00000 n +trailer +<< +/ID +[<7b776b85e1f11c424682a79edf2f54eb><7b776b85e1f11c424682a79edf2f54eb>] +% ReportLab generated PDF document -- digest (http://www.reportlab.com) + +/Info 10 0 R +/Root 9 0 R +/Size 21 +>> +startxref +27595 +%%EOF diff --git a/comprehensions_and_generators.rst b/comprehensions_and_generators.rst new file mode 100644 index 0000000..5ccf70a --- /dev/null +++ b/comprehensions_and_generators.rst @@ -0,0 +1,102 @@ +List Comprehensions +=================== + +List comprehensions are declarative ways of defining lists in python. The +following ways of building a list are equivalent: + +.. code-block:: python + + l = [x**2 for x in range(1, 10)] + + l = [] + for x in range(1, 10): + l.append(x**2) + +.. code-block:: python + + m = [x for x in range(1, 10) if x % 2 == 1] + + m = [] + for x in range(1, 10): + if x % 2 == 1: + m.append(x) + +.. code-block:: python + + n = [(x, y) for x in range(1, 10) for y in range(1, 10) if x > y] + + n = [] + for x in range(1, 10): + for y in range(1, 10): + if x > y: + n.append((x, y)) + +The general form is: + +.. code-block:: python + + [expression for x in y condition] + + +Dictionary Comprehensions +========================= + +Dictionary comprehensions are the analogous construct for dictionaries: + +.. code-block:: python + + o = {x: x**3 for x in range(1, 100) if math.sqrt(x).is_integer()} + + o = {} + for x in range(1, 100): + if math.sqrt(x).is_integer(): + o[x] = x**3 + +I have never used a dictionary comprehension outside of exercises. I don't +know how useful they are, and I'm struggling to think of good examples. + +Generators +========== + +A generator is a function that yields values rather than returning them. For +example: + +.. code-block:: python + + def squares(): + i = 0 + while True + yield i**2 + i += 1 + +The built in `range` function is similar to a generator. Generators are iterable: + +.. code-block:: python + + for square in squares(): + print(square) + if square > 10000: + break + +But only once! + +Generators can take arguments: + +.. code-block:: python + + def exponents(exp): + i = 0 + while True + yield i**exp + i += 1 + + + +Generator expressions are similar to list comprehensions: + +.. code-block:: python + + squares = (i**2 for i in range(10000)) + +Generators are lazy, values are calculated when needed, meaning they can represent +infinite sequences without requiring infinite memory. diff --git a/excercises.py b/excercises.py new file mode 100644 index 0000000..48a435b --- /dev/null +++ b/excercises.py @@ -0,0 +1,41 @@ +# Write list comprehensions, dictionary comprehensions or generators as +# necessary to produce the following outputs: + +# A list of all odd integers between 0 and 100 + + +# Some examples from reddit: https://www.reddit.com/r/learnpython/comments/4d2yl7/i_need_list_comprehension_exercises_to_drill/ +# Find all integers between 1-1000 that contain a 3. What about the first 1000 +# integers with that property that are greater than 35? + + +# Replace all occurences of 'a' with '!' in the following string (but don't use +# a library function. Yes I know str.replace exists. +"Conditional expressions can be used in all kinds of situations where you want to choose between two expression values based on some condition. This does the same as the ternary operator ?: that exists in other languages." + + +# List all the words in the obove string that contain more than 5 letters + + +# Count the number of occurences of each letter in the above string. The output +# should be a dictionary with the letter as the key, and the number of occurences +# as the value. I am actually not sure this is a good example. + + +# The square root of 2 is 1.41, in base 10 the second decimal place as a value +# of 1. What are the first 10 such integers? Hint: itertool.count() is a +# generator that will yield all non negative integers. + + +# The first 10 lines of a file that do not start with a '#' character. (After +# you implement some of the things in this file, you could use this file). + + +# Create an iterable that contains all members of the fibonaci sequence. + + +# Implement the 'range' generator. + + +# Create an iterable that contains the moving average over the fibonacci +# sequence.