|
5 | 5 | <link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/> |
6 | 6 | <link href="https://learnbyexample.github.io"/> |
7 | 7 | <generator uri="https://www.getzola.org/">Zola</generator> |
8 | | - <updated>2021-12-08T00:00:00+00:00</updated> |
| 8 | + <updated>2021-12-14T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id> |
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>Python tip: 3</title> |
| 12 | + <published>2021-12-14T00:00:00+00:00</published> |
| 13 | + <updated>2021-12-14T00:00:00+00:00</updated> |
| 14 | + <link href="https://learnbyexample.github.io/tips/python-tip-3/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/python-tip-3/</id> |
| 16 | + <content type="html"><p>In case you haven't yet discovered this awesome <strong>f-string</strong> feature, you can add <code>=</code> after an expression to get both the expression and the result in the output.</p> |
| 17 | +<pre style="background-color:#f5f5f5;"> |
| 18 | +<code><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#1f1f1f;">num1 </span><span style="color:#72ab00;">= </span><span style="color:#b3933a;">42 |
| 19 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#1f1f1f;">num2 </span><span style="color:#72ab00;">= </span><span style="color:#b3933a;">7 |
| 20 | + |
| 21 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#668f14;">f</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">{num1 </span><span style="color:#72ab00;">+ </span><span style="color:#1f1f1f;">num2 = }</span><span style="color:#d07711;">&#39; |
| 22 | +&#39;num1 + num2 = 49&#39; |
| 23 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#668f14;">f</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">{num1 </span><span style="color:#72ab00;">+ </span><span style="color:#1f1f1f;">(num2 </span><span style="color:#72ab00;">* </span><span style="color:#b3933a;">10</span><span style="color:#1f1f1f;">) = }</span><span style="color:#d07711;">&#39; |
| 24 | +&#39;num1 + (num2 * 10) = 112&#39; |
| 25 | +</span></code></pre> |
| 26 | +<p>I use it often to quickly test a function:</p> |
| 27 | +<pre style="background-color:#f5f5f5;"> |
| 28 | +<code><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="background-color:#562d56bf;color:#f8f8f8;">def</span><span style="color:#1f1f1f;"> </span><span style="color:#5597d6;">isodd</span><span style="color:#1f1f1f;">(n): |
| 29 | +</span><span style="color:#b3933a;">... </span><span style="color:#72ab00;">return </span><span style="color:#a2a001;">bool</span><span style="color:#1f1f1f;">(n </span><span style="color:#72ab00;">% </span><span style="color:#b3933a;">2</span><span style="color:#1f1f1f;">) |
| 30 | +... |
| 31 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#b39f04;">print</span><span style="color:#1f1f1f;">(</span><span style="color:#668f14;">f</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">{</span><span style="color:#5597d6;">isodd</span><span style="color:#1f1f1f;">(</span><span style="color:#b3933a;">42</span><span style="color:#1f1f1f;">) = }</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">) |
| 32 | +</span><span style="color:#5597d6;">isodd</span><span style="color:#1f1f1f;">(</span><span style="color:#b3933a;">42</span><span style="color:#1f1f1f;">) </span><span style="color:#72ab00;">= </span><span style="color:#b3933a;">False |
| 33 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#b39f04;">print</span><span style="color:#1f1f1f;">(</span><span style="color:#668f14;">f</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">{</span><span style="color:#5597d6;">isodd</span><span style="color:#1f1f1f;">(</span><span style="color:#b3933a;">123</span><span style="color:#1f1f1f;">) = }</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">) |
| 34 | +</span><span style="color:#5597d6;">isodd</span><span style="color:#1f1f1f;">(</span><span style="color:#b3933a;">123</span><span style="color:#1f1f1f;">) </span><span style="color:#72ab00;">= </span><span style="color:#b3933a;">True |
| 35 | +</span></code></pre> |
| 36 | +<p><img src="/images/info.svg" alt="info" /> See <a href="https://docs.python.org/3/library/string.html#formatstrings">docs.python: Format String Syntax</a> and <a href="https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals">docs.python: Formatted string literals</a> for documentation and examples.</p> |
| 37 | +<p><img src="/images/info.svg" alt="info" /> See my <a href="https://github.com/learnbyexample/100_page_python_intro">100 Page Python Intro</a> ebook for a short, introductory guide for the Python programming language.</p> |
| 38 | +</content> |
| 39 | + </entry> |
10 | 40 | <entry xml:lang="en"> |
11 | 41 | <title>Vim tip: 1</title> |
12 | 42 | <published>2021-12-08T00:00:00+00:00</published> |
@@ -2574,7 +2604,7 @@ no VAL is specified, the key will be given the value true.</p> |
2574 | 2604 | <entry xml:lang="en"> |
2575 | 2605 | <title>Python regular expression cheatsheet and examples</title> |
2576 | 2606 | <published>2020-07-03T00:00:00+00:00</published> |
2577 | | - <updated>2020-11-12T00:00:00+00:00</updated> |
| 2607 | + <updated>2021-12-14T00:00:00+00:00</updated> |
2578 | 2608 | <link href="https://learnbyexample.github.io/python-regex-cheatsheet/" type="text/html"/> |
2579 | 2609 | <id>https://learnbyexample.github.io/python-regex-cheatsheet/</id> |
2580 | 2610 | <content type="html"><p align="center"><img src="/images/books/pyregex_example.png" alt="pyregex example" /></p> |
@@ -2613,9 +2643,9 @@ no VAL is specified, the key will be given the value true.</p> |
2613 | 2643 | <tr><td><code>+</code></td><td>Match one or more times</td></tr> |
2614 | 2644 | <tr><td><code>?</code></td><td>Match zero or one times</td></tr> |
2615 | 2645 | <tr><td><code>{m,n}</code></td><td>Match <code>m</code> to <code>n</code> times (inclusive)</td></tr> |
2616 | | -<tr><td><code>{m,}</code></td><td>Match at least m times</td></tr> |
| 2646 | +<tr><td><code>{m,}</code></td><td>Match at least <code>m</code> times</td></tr> |
2617 | 2647 | <tr><td><code>{,n}</code></td><td>Match up to <code>n</code> times (including <code>0</code> times)</td></tr> |
2618 | | -<tr><td><code>{n}</code></td><td>Match exactly n times</td></tr> |
| 2648 | +<tr><td><code>{n}</code></td><td>Match exactly <code>n</code> times</td></tr> |
2619 | 2649 | <tr><td><code>pat1.*pat2</code></td><td>any number of characters between <code>pat1</code> and <code>pat2</code></td></tr> |
2620 | 2650 | <tr><td><code>pat1.*pat2|pat2.*pat1</code></td><td>match both <code>pat1</code> and <code>pat2</code> in any order</td></tr> |
2621 | 2651 | </tbody></table> |
@@ -2891,6 +2921,10 @@ mission success |
2891 | 2921 | </span><span style="color:#7f8989;"># swap words that are separated by a comma |
2892 | 2922 | </span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#1f1f1f;">re.</span><span style="color:#5597d6;">sub</span><span style="color:#1f1f1f;">(</span><span style="color:#668f14;">r</span><span style="color:#d07711;">&#39;</span><span style="color:#7c8f4c;">(</span><span style="color:#aeb52b;">\w</span><span style="color:#72ab00;">+</span><span style="color:#7c8f4c;">),(</span><span style="color:#aeb52b;">\w</span><span style="color:#72ab00;">+</span><span style="color:#7c8f4c;">)</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">, </span><span style="color:#668f14;">r</span><span style="color:#d07711;">&#39;</span><span style="color:#72ab00;">\2</span><span style="color:#7c8f4c;">,</span><span style="color:#72ab00;">\1</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">, </span><span style="color:#d07711;">&#39;good,bad 42,24&#39;</span><span style="color:#1f1f1f;">) |
2893 | 2923 | </span><span style="color:#d07711;">&#39;bad,good 24,42&#39; |
| 2924 | + |
| 2925 | +</span><span style="color:#7f8989;"># example with both capturing and non-capturing groups |
| 2926 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#1f1f1f;">re.</span><span style="color:#5597d6;">sub</span><span style="color:#1f1f1f;">(</span><span style="color:#668f14;">r</span><span style="color:#d07711;">&#39;</span><span style="color:#7c8f4c;">(</span><span style="color:#aeb52b;">\d</span><span style="color:#72ab00;">+</span><span style="color:#7c8f4c;">)(?:abc)</span><span style="color:#72ab00;">+</span><span style="color:#7c8f4c;">(</span><span style="color:#aeb52b;">\d</span><span style="color:#72ab00;">+</span><span style="color:#7c8f4c;">)</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">, </span><span style="color:#668f14;">r</span><span style="color:#d07711;">&#39;</span><span style="color:#72ab00;">\2</span><span style="color:#7c8f4c;">:</span><span style="color:#72ab00;">\1</span><span style="color:#d07711;">&#39;</span><span style="color:#1f1f1f;">, </span><span style="color:#d07711;">&#39;2abcabc42 12abcd21&#39;</span><span style="color:#1f1f1f;">) |
| 2927 | +</span><span style="color:#d07711;">&#39;42:2 12abcd21&#39; |
2894 | 2928 | </span></code></pre> |
2895 | 2929 | <ul> |
2896 | 2930 | <li>using functions in replacement section of <code>re.sub</code></li> |
@@ -2958,7 +2992,7 @@ mission success |
2958 | 2992 | </span><span style="color:#d07711;">&#39;Hi there. Nice day&#39; |
2959 | 2993 | </span></code></pre><br> |
2960 | 2994 | <h2 id="python-re-gex-book">Python re(gex)? book<a class="zola-anchor" href="#python-re-gex-book" aria-label="Anchor link for: python-re-gex-book">🔗</a></h2> |
2961 | | -<p>Visit my repo <a href="https://github.com/learnbyexample/py_regular_expressions">Python re(gex)?</a> for details about the book I wrote on Python regular expressions. The ebook uses plenty of examples to explain the concepts from the very beginning and step by step introduces more advanced concepts. The book also covers the <a href="https://pypi.org/project/regex/">third party module regex</a>. The cheatsheet and examples presented in this post are based on contents of this book.</p> |
| 2995 | +<p>Visit my GitHub repo <a href="https://github.com/learnbyexample/py_regular_expressions">Python re(gex)?</a> for details about the book I wrote on Python regular expressions. The ebook uses plenty of examples to explain the concepts from the very beginning and step by step introduces more advanced concepts. The book also covers the <a href="https://pypi.org/project/regex/">third party module regex</a>. The cheatsheet and examples presented in this post are based on contents of this book.</p> |
2962 | 2996 | <p>Use <a href="https://leanpub.com/py_regex/c/P7erPYAm1386">this leanpub link</a> for a discounted price.</p> |
2963 | 2997 | <p align="center"><img src="/images/books/pyregex.png" alt="Python regex book cover image" /></p> |
2964 | 2998 | </content> |
|
0 commit comments