Skip to content

Commit 88b740b

Browse files
added fifth python tip
1 parent f039cdf commit 88b740b

File tree

8 files changed

+254
-5
lines changed

8 files changed

+254
-5
lines changed

atom.xml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,36 @@
55
<link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
66
<link href="https://learnbyexample.github.io"/>
77
<generator uri="https://www.getzola.org/">Zola</generator>
8-
<updated>2022-01-21T00:00:00+00:00</updated>
8+
<updated>2022-01-25T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>Python tip 5: random choice and sample</title>
12+
<published>2022-01-25T00:00:00+00:00</published>
13+
<updated>2022-01-25T00:00:00+00:00</updated>
14+
<link href="https://learnbyexample.github.io/tips/python-tip-5/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/python-tip-5/</id>
16+
<content type="html">&lt;p&gt;Here are a couple of commonly used methods for the built-in &lt;code&gt;random&lt;&#x2F;code&gt; module:&lt;&#x2F;p&gt;
17+
&lt;ul&gt;
18+
&lt;li&gt;&lt;code&gt;choice()&lt;&#x2F;code&gt; method helps you get a random element&lt;&#x2F;li&gt;
19+
&lt;li&gt;&lt;code&gt;sample()&lt;&#x2F;code&gt; method helps you get a &lt;code&gt;list&lt;&#x2F;code&gt; of a specific count of random elements&lt;&#x2F;li&gt;
20+
&lt;&#x2F;ul&gt;
21+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
22+
&lt;code&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;random
23+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;nums &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;51&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;]
24+
25+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;random.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;choice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;(nums)
26+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3
27+
28+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;random.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;sample&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;(nums, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;k&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;)
29+
[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;51&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;]
30+
31+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;random.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;sample&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b39f04;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;k&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;)
32+
[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;490&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;26&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;745&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;919&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;]
33+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
34+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; Both these methods will work on any sequence object. The &lt;code&gt;sample()&lt;&#x2F;code&gt; method also accepts a &lt;code&gt;set&lt;&#x2F;code&gt; object, but that will be deprecated.&lt;&#x2F;p&gt;
35+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See my &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;learnbyexample&#x2F;100_page_python_intro&quot;&gt;100 Page Python Intro&lt;&#x2F;a&gt; ebook for a short, introductory guide for the Python programming language.&lt;&#x2F;p&gt;
36+
</content>
37+
</entry>
1038
<entry xml:lang="en">
1139
<title>Brag post: Hacker News Front Page entries</title>
1240
<published>2022-01-21T00:00:00+00:00</published>
@@ -2592,7 +2620,7 @@ $ diff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F
25922620
<entry xml:lang="en">
25932621
<title>Customizing pandoc to generate beautiful pdf and epub from markdown</title>
25942622
<published>2020-07-21T00:00:00+00:00</published>
2595-
<updated>2021-08-27T00:00:00+00:00</updated>
2623+
<updated>2022-01-25T00:00:00+00:00</updated>
25962624
<link href="https://learnbyexample.github.io/customizing-pandoc/" type="text/html"/>
25972625
<id>https://learnbyexample.github.io/customizing-pandoc/</id>
25982626
<content type="html">&lt;p&gt;Either you&#x27;ve already heard of &lt;code&gt;pandoc&lt;&#x2F;code&gt; or if you have searched online for &lt;code&gt;markdown&lt;&#x2F;code&gt; to &lt;code&gt;pdf&lt;&#x2F;code&gt; or similar, you are sure to come across &lt;code&gt;pandoc&lt;&#x2F;code&gt;. This tutorial will help you use &lt;code&gt;pandoc&lt;&#x2F;code&gt; to generate &lt;code&gt;pdf&lt;&#x2F;code&gt; and &lt;code&gt;epub&lt;&#x2F;code&gt; from a &lt;a href=&quot;https:&#x2F;&#x2F;github.github.com&#x2F;gfm&#x2F;&quot;&gt;GitHub style markdown&lt;&#x2F;a&gt; file. The main motivation for this blog post is to highlight what customizations I did to generate &lt;code&gt;pdf&lt;&#x2F;code&gt; and &lt;code&gt;epub&lt;&#x2F;code&gt; versions for &lt;a href=&quot;https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;books&#x2F;&quot;&gt;self-publishing my ebooks&lt;&#x2F;a&gt;. It wasn&#x27;t easy to arrive at the set-up I ended up with, so I hope this will be useful for those looking to use &lt;code&gt;pandoc&lt;&#x2F;code&gt; to generate &lt;code&gt;pdf&lt;&#x2F;code&gt; and &lt;code&gt;epub&lt;&#x2F;code&gt; formats. This guide is specifically aimed at technical books that has code snippets.&lt;&#x2F;p&gt;
@@ -2826,6 +2854,7 @@ no VAL is specified, the key will be given the value true.&lt;&#x2F;p&gt;
28262854
&lt;li&gt;See also &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;fastai&#x2F;fastdoc&quot;&gt;fastdoc&lt;&#x2F;a&gt; — the output of fastdoc is an asciidoc file for each input notebook. You can then use asciidoctor to convert that to HTML, DocBook, epub, mobi, and so forth&lt;&#x2F;li&gt;
28272855
&lt;&#x2F;ul&gt;
28282856
&lt;&#x2F;li&gt;
2857+
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.thedigitalcatbooks.com&#x2F;maubook-introduction&#x2F;&quot;&gt;Mau&lt;&#x2F;a&gt; — template-based markup language, heavily inspired by AsciiDoc&lt;&#x2F;li&gt;
28292858
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;asciidoctor.org&#x2F;docs&#x2F;what-is-asciidoc&#x2F;&quot;&gt;Asciidoctor&lt;&#x2F;a&gt;
28302859
&lt;ul&gt;
28312860
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;shape-of-code.coding-guidelines.com&#x2F;2019&#x2F;08&#x2F;11&#x2F;my-books-pdf-generation-workflow&#x2F;&quot;&gt;pdf generation workflow with Asciidoc&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;

customizing-pandoc/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ <h2 id="resource-links">Resource links<a class="zola-anchor" href="#resource-lin
439439
<li>See also <a href="https://github.com/fastai/fastdoc">fastdoc</a> — the output of fastdoc is an asciidoc file for each input notebook. You can then use asciidoctor to convert that to HTML, DocBook, epub, mobi, and so forth</li>
440440
</ul>
441441
</li>
442+
<li><a href="https://www.thedigitalcatbooks.com/maubook-introduction/">Mau</a> — template-based markup language, heavily inspired by AsciiDoc</li>
442443
<li><a href="https://asciidoctor.org/docs/what-is-asciidoc/">Asciidoctor</a>
443444
<ul>
444445
<li><a href="https://shape-of-code.coding-guidelines.com/2019/08/11/my-books-pdf-generation-workflow/">pdf generation workflow with Asciidoc</a></li>

sitemap.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</url>
3333
<url>
3434
<loc>https://learnbyexample.github.io/customizing-pandoc/</loc>
35-
<lastmod>2021-08-27</lastmod>
35+
<lastmod>2022-01-25</lastmod>
3636
</url>
3737
<url>
3838
<loc>https://learnbyexample.github.io/duplicates-irrespective-field-order/</loc>
@@ -366,6 +366,10 @@
366366
<loc>https://learnbyexample.github.io/tips/python-tip-4/</loc>
367367
<lastmod>2022-01-04</lastmod>
368368
</url>
369+
<url>
370+
<loc>https://learnbyexample.github.io/tips/python-tip-5/</loc>
371+
<lastmod>2022-01-25</lastmod>
372+
</url>
369373
<url>
370374
<loc>https://learnbyexample.github.io/tips/vim-tip-1/</loc>
371375
<lastmod>2021-12-08</lastmod>

tags/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@
329329

330330
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;python&#x2F;">
331331
python
332-
<span class="count">21</span>
332+
<span class="count">22</span>
333333
</a>
334334

335335
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;re-module&#x2F;">
@@ -379,7 +379,7 @@
379379

380380
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;tip&#x2F;">
381381
tip
382-
<span class="count">11</span>
382+
<span class="count">12</span>
383383
</a>
384384

385385
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;tutorial&#x2F;">

tags/python/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
<div class="taxonomy">
125125
<h2>python</h2>
126126

127+
<div class="taxonomy__item">
128+
<span class="taxonomy__item__time">2022-01-25</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;python-tip-5&#x2F;">Python tip 5: random choice and sample</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-01-19</span>
129136
<span class="taxonomy__item__title">

tags/tip/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
<div class="taxonomy">
125125
<h2>tip</h2>
126126

127+
<div class="taxonomy__item">
128+
<span class="taxonomy__item__time">2022-01-25</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;python-tip-5&#x2F;">Python tip 5: random choice and sample</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-01-18</span>
129136
<span class="taxonomy__item__title">

tips/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
<div class="about">
125125
<h1 id="list-of-all-tips"><p style="color: #c05b4d">List of all tips</h1>
126126
<ul>
127+
<li><a href="https://learnbyexample.github.io/tips/python-tip-5/">Python tip 5: random choice and sample</a></li>
127128
<li><a href="https://learnbyexample.github.io/tips/vim-tip-3/">Vim tip 3: auto complete words and lines in Insert mode</a></li>
128129
<li><a href="https://learnbyexample.github.io/tips/cli-tip-4/">CLI tip 4: serialize file contents to a single line</a></li>
129130
<li><a href="https://learnbyexample.github.io/tips/python-tip-4/">Python tip 4: comparison chaining</a></li>

0 commit comments

Comments
 (0)