avatarCoucou Camille

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2505

Abstract

ll using <code>y = 10</code> , any update of <code>y</code> after the function definition has no effect on the argument it uses.</p><h2 id="a6d1">Random Selection with/without Replacement</h2><ul><li>Selection with replacement: <code>random.<b>choices</b>(seq, k=1)</code> , <code>k</code> being the size for sample, 1 by default.</li></ul><div id="a265"><pre><span class="hljs-keyword">import</span> random</pre></div><div id="5f4f"><pre>random<span class="hljs-selector-class">.choices</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>)), k=<span class="hljs-number">8</span>) >>> <span class="hljs-selector-attr">[6, 3, 5, 5, 9, 4, 4, 2]</span></pre></div><ul><li>Selection without replacement: <code>random.<b>sample</b>(seq, k)</code></li></ul><div id="34ac"><pre>random<span class="hljs-selector-class">.sample</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>)), k=<span class="hljs-number">8</span>) >>> <span class="hljs-selector-attr">[1, 5, 3, 7, 2, 0, 8, 4]</span></pre></div><h2 id="e613">Copy: Shallow vs Deep</h2><ul><li>Shallow copy: <code>copy()</code> copies <b>reference </b>of the objects in the original. Changes made to the original object are reflected in the shallow copy as well</li></ul><div id="85ef"><pre><span class="hljs-keyword">import</span> copy</pre></div><div id="cb37"><pre>a = <span class="hljs-string">[[1, 2], [3, 4]]</span> b = copy.copy(a); b >>> <span class="hljs-string">[[1, 2], [3, 4]]</span></pre></div><div id="f3f8"><pre><span class="hljs-selector-tag">a</span><span class="hljs-selector-attr">[1]</span><span class="hljs-selector-class">.append</span>(<span class="hljs-number">5</span>) <span class="hljs-selector-tag">b</span> >>> <span class="hljs-selector-attr">[[1, 2]</span>, <span class="hljs-selector-attr">[3, 4, 5]</span>]</pre></div><ul><li>Deep copy: <code>deepcopy()</code> inserts copies of the objects found in the original. Changes made to the original object do not affect the deep copy.</li></ul><div id="cfd5"><pre>a = tp_exit_ids_items = list() c = copy.deepcopy(a); c >>> <span class="hljs-string">[[1, 2], [3, 4]]</span></pre></div><div id="462a"><pre>a[<span class="hljs-number">1</span>].append(<span class="hljs-number">5</span>) c >>> <span class="hljs-string">[[1, 2], [3, 4]]</span></pre></div><h2 id="4fa9">Equivalence vs Equa

Options

lity</h2><ul><li>Whether both have the same value: <code>==</code></li></ul><div id="eb14"><pre>a = <span class="hljs-string">[[1, 2], [3, 4]]</span> b = <span class="hljs-string">[[1, 2], [3, 4]]</span> c = a</pre></div><div id="aace"><pre># equivalent check a == b <span class="hljs-meta prompt_">>>></span> <span class="language-python"><span class="hljs-literal">True</span></span> a == c <span class="hljs-meta prompt_">>>></span> <span class="language-python"><span class="hljs-literal">True</span></span></pre></div><ul><li>Whether both points to the same object: <code>is</code></li></ul><div id="2fe8"><pre><span class="hljs-comment"># equality check</span> a <span class="hljs-keyword">is</span> b <span class="hljs-meta">>>> </span><span class="hljs-literal">False</span> a <span class="hljs-keyword">is</span> c <span class="hljs-meta">>>> </span><span class="hljs-literal">True</span></pre></div><h2 id="6a1a">Searching for Substring</h2><p id="40c3">There are four ways to search for a substring within a string:</p><ul><li><code>str.find(sub, start=None, end=None)</code> and <code>str.rfind(...)</code></li><li><code>str.index(sub, start=None, end=None)</code> and <code>str.rindex(...)</code></li></ul><p id="2518">The following operations all return a result of 4 as “on” is found at index 4:</p><div id="31a5"><pre><span class="hljs-string">"python"</span><span class="hljs-selector-class">.find</span>(<span class="hljs-string">"on"</span>) <span class="hljs-string">"python"</span><span class="hljs-selector-class">.rfind</span>(<span class="hljs-string">"on"</span>) <span class="hljs-string">"python"</span><span class="hljs-selector-class">.index</span>(<span class="hljs-string">"on"</span>) <span class="hljs-string">"python"</span><span class="hljs-selector-class">.rindex</span>(<span class="hljs-string">"on"</span>)</pre></div><p id="50e7">The most importance difference is that <code>find()</code> and <code>rfind()</code> returns -1 if <code>sub</code> is not founded; <code>index()</code> and <code>rindex()</code> throws a <b>ValueError </b>instead:</p><div id="db51"><pre><span class="hljs-string">"python"</span>.<span class="hljs-built_in">find</span>(<span class="hljs-string">"a"</span>) >>> -1</pre></div><div id="45a8"><pre>"python".<span class="hljs-keyword">index</span>("a") >>> ValueError: substring <span class="hljs-keyword">not</span> <span class="hljs-built_in">found</span></pre></div></article></body>

5 Python Operations that Confuse Beginners

This article summarizes some highly similar Python operations that are easy to get mixed up with, especially for beginners.

Image by Author

Lambda Argument: Bounded during Definition vs Runtime Random Selection with/without Replacement Copy: Shallow vs Deep Equivalence vs Equality Searching for Substring

Lambda Argument: Bounded during Definition vs Runtime

  • y bounded during definition
func = lambda x, y=y: x + y
  • y bounded during runtime
func = lambda x: x + y

Example: Assign y with value 10 and define the functions:

y = 10
func1 = lambda x: x + y
func2 = lambda x, y=y: x + y
list(map(func1, lst))
>>> [11, 12, 13, 14, 15]
list(map(func2, lst))
>>> [11, 12, 13, 14, 15]

Let’s then update the value of y , run the functions and see what happens.

y = 100
list(map(func1, lst))
>>> [101, 102, 103, 104, 105]
list(map(func2, lst))
>>> [11, 12, 13, 14, 15]

Clearly y in func1 now takes the new value of 100 while func2 is still using y = 10 , any update of y after the function definition has no effect on the argument it uses.

Random Selection with/without Replacement

  • Selection with replacement: random.choices(seq, k=1) , k being the size for sample, 1 by default.
import random
random.choices(list(range(10)), k=8)
>>> [6, 3, 5, 5, 9, 4, 4, 2]
  • Selection without replacement: random.sample(seq, k)
random.sample(list(range(10)), k=8)
>>> [1, 5, 3, 7, 2, 0, 8, 4]

Copy: Shallow vs Deep

  • Shallow copy: copy() copies reference of the objects in the original. Changes made to the original object are reflected in the shallow copy as well
import copy
a = [[1, 2], [3, 4]]
b = copy.copy(a); b
>>> [[1, 2], [3, 4]]
a[1].append(5)
b
>>> [[1, 2], [3, 4, 5]]
  • Deep copy: deepcopy() inserts copies of the objects found in the original. Changes made to the original object do not affect the deep copy.
a = tp_exit_ids_items = list()
c = copy.deepcopy(a); c
>>> [[1, 2], [3, 4]]
a[1].append(5)
c
>>> [[1, 2], [3, 4]]

Equivalence vs Equality

  • Whether both have the same value: ==
a = [[1, 2], [3, 4]]
b = [[1, 2], [3, 4]]
c = a
# equivalent check
a == b
>>> True
a == c
>>> True
  • Whether both points to the same object: is
# equality check
a is b
>>> False
a is c
>>> True

Searching for Substring

There are four ways to search for a substring within a string:

  • str.find(sub, start=None, end=None) and str.rfind(...)
  • str.index(sub, start=None, end=None) and str.rindex(...)

The following operations all return a result of 4 as “on” is found at index 4:

"python".find("on")
"python".rfind("on")
"python".index("on")
"python".rindex("on")

The most importance difference is that find() and rfind() returns -1 if sub is not founded; index() and rindex() throws a ValueError instead:

"python".find("a")
>>> -1
"python".index("a")
>>> ValueError: substring not found
Python
Programming
Recommended from ReadMedium