Skip to content

Commit 85f1972

Browse files
added 13th python tip
1 parent b14ae67 commit 85f1972

File tree

8 files changed

+310
-4
lines changed

8 files changed

+310
-4
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ I'm addicted to reading fantasy/sci-fi books, so I have a [blog](https://learnby
5656

5757
## Tips
5858

59+
* [Python tip 13: formatting numbers with underscore separation](https://learnbyexample.github.io/tips/python-tip-13/)
5960
* [Vim tip 11: replace characters in Normal mode](https://learnbyexample.github.io/tips/vim-tip-11/)
6061
* [CLI tip 12: squeeze empty lines](https://learnbyexample.github.io/tips/cli-tip-12/)
6162
* [Python tip 12: negate a regex grouping](https://learnbyexample.github.io/tips/python-tip-12/)

Diff for: atom.xml

+58-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,65 @@
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-07-06T00:00:00+00:00</updated>
8+
<updated>2022-07-13T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>Python tip 13: formatting numbers with underscore separation</title>
12+
<published>2022-07-13T00:00:00+00:00</published>
13+
<updated>2022-07-13T00:00:00+00:00</updated>
14+
<link href="https://learnbyexample.github.io/tips/python-tip-13/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/python-tip-13/</id>
16+
<content type="html">&lt;p&gt;For readability purposes, you can use underscores while declaring large numbers. For example:&lt;&#x2F;p&gt;
17+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
18+
&lt;code&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;1_000_000_000
19+
1000000000
20+
21+
&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:#b3933a;&quot;&gt;0b1000_1111
22+
143
23+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
24+
&lt;p&gt;Did you know that you can also format numbers with underscore separation?&lt;&#x2F;p&gt;
25+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
26+
&lt;code&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;n &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;14310023
27+
28+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# underscore separation
29+
&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:#668f14;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;{n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;:_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;
30+
&amp;#39;14_310_023&amp;#39;
31+
32+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# you can also use comma separation for integers
33+
&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:#668f14;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;{n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;:,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;
34+
&amp;#39;14,310,023&amp;#39;
35+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
36+
&lt;p&gt;Here are some examples for displaying numbers in binary, octal and hexadecimal formats:&lt;&#x2F;p&gt;
37+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
38+
&lt;code&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;n &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;14310023
39+
40+
&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:#668f14;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;{n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;:_b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;
41+
&amp;#39;1101_1010_0101_1010_1000_0111&amp;#39;
42+
&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:#668f14;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;{n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;:#_b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;
43+
&amp;#39;0b1101_1010_0101_1010_1000_0111&amp;#39;
44+
45+
&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:#668f14;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;{n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;:#_x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;
46+
&amp;#39;0xda_5a87&amp;#39;
47+
48+
&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:#668f14;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;{n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;:#_o&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;
49+
&amp;#39;0o6645_5207&amp;#39;
50+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
51+
&lt;p&gt;And here&#x27;s an example with zero filling:&lt;&#x2F;p&gt;
52+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
53+
&lt;code&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;n &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;in &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;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;28&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;):
54+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;... &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b39f04;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#668f14;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;{n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;:09_b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;)
55+
...
56+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;0000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;_0011
57+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;0001&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;_0100
58+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;0001&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;_1100
59+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
60+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;reference&#x2F;lexical_analysis.html#f-strings&quot;&gt;docs.python: Formatted string literals&lt;&#x2F;a&gt; for documentation and other examples.&lt;&#x2F;p&gt;
61+
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
62+
&lt;p align=&quot;center&quot;&gt;&lt;iframe width=&quot;560&quot; height=&quot;315&quot; loading=&quot;lazy&quot; src=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;wFg0wS8nY4o&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;&#x2F;iframe&gt;&lt;&#x2F;p&gt;
63+
&lt;br&gt;
64+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See also 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.&lt;&#x2F;p&gt;
65+
</content>
66+
</entry>
1067
<entry xml:lang="en">
1168
<title>Vim tip 11: replace characters in Normal mode</title>
1269
<published>2022-07-06T00:00:00+00:00</published>

Diff for: sitemap.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@
368368
</url>
369369
<url>
370370
<loc>https://learnbyexample.github.io/tips/</loc>
371-
<lastmod>2022-07-06</lastmod>
371+
<lastmod>2022-07-13</lastmod>
372372
</url>
373373
<url>
374374
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -434,6 +434,10 @@
434434
<loc>https://learnbyexample.github.io/tips/python-tip-12/</loc>
435435
<lastmod>2022-06-22</lastmod>
436436
</url>
437+
<url>
438+
<loc>https://learnbyexample.github.io/tips/python-tip-13/</loc>
439+
<lastmod>2022-07-13</lastmod>
440+
</url>
437441
<url>
438442
<loc>https://learnbyexample.github.io/tips/python-tip-2/</loc>
439443
<lastmod>2022-05-16</lastmod>

Diff for: tags/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@
349349

350350
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;python&#x2F;">
351351
python
352-
<span class="count">31</span>
352+
<span class="count">32</span>
353353
</a>
354354

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

405405
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;tip&#x2F;">
406406
tip
407-
<span class="count">35</span>
407+
<span class="count">36</span>
408408
</a>
409409

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

Diff for: tags/python/index.html

+7
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-07-13</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;python-tip-13&#x2F;">Python tip 13: formatting numbers with underscore separation</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-06-22</span>
129136
<span class="taxonomy__item__title">

Diff for: tags/tip/index.html

+7
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-07-13</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;python-tip-13&#x2F;">Python tip 13: formatting numbers with underscore separation</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-07-06</span>
129136
<span class="taxonomy__item__title">

Diff for: tips/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ <h1 id="list-of-all-tips-bulb"><p style="color: #c05b4d">List of all tips 💡</
126126
<p>This page links to tips about <u><a href="https://learnbyexample.github.io/tips/#python">Python</a></u>, <u><a href="https://learnbyexample.github.io/tips/#command-line-tools">Command line tools</a></u> and <u><a href="https://learnbyexample.github.io/tips/#vim">Vim</a></u></p>
127127
<h2 id="python-snake"><p style="color: #ff6600"><a name="python"></a>Python 🐍</h2>
128128
<ul>
129+
<li><a href="https://learnbyexample.github.io/tips/python-tip-13/">Python tip 13: formatting numbers with underscore separation</a></li>
129130
<li><a href="https://learnbyexample.github.io/tips/python-tip-12/">Python tip 12: negate a regex grouping</a></li>
130131
<li><a href="https://learnbyexample.github.io/tips/python-tip-11/">Python tip 11: capture external command output</a></li>
131132
<li><a href="https://learnbyexample.github.io/tips/python-tip-10/">Python tip 10: removeprefix and removesuffix string methods</a></li>

0 commit comments

Comments
 (0)