Skip to content

Commit 7db0c8f

Browse files
committed
updating rendered materials
1 parent cb2e1db commit 7db0c8f

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎_sources/exercises/slicing.rst.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ Your functions should look like:
3131
def exchange_first_last(seq):
3232
return a_new_sequence
3333
34+
35+
**Hint:**
36+
37+
Your functions should work with ALL sequences. That means that you cannot use list methods, like ``.append``, because that won't work with strings and tuples. But all sequences support concatenation with the ``+`` operator.
38+
39+
Item or Sequence?
40+
.................
41+
42+
A key difference between using a single index: ``seq[i]`` and using a slice: ``seq[i:j], seq[:i], seq[i:]`` is that using an index returns a single item, whereas a slice always returns a sequence -- even if that sequence is of length one or even empty. And concatenation requires a sequence, so make sure you use slicing if you want to concatenate the results.
43+
44+
.. note:: Python "gotcha" with strings. Python does not have a character type. Instead of a character, you get a length-one string. This can cause confusion sometimes, as other sequences return a single item when you index, so when you index into a list of numbers, you get number -- which is not a list (or any type of sequence). But with strings, when you index into (or loop through) a string, you get a length-one string, which is, itself a string, and therefor a valid sequence. So: ``a_string[i] + another_string`` works, but ``a_list[i] + another_list`` does not work.
45+
46+
47+
3448
Tests:
3549
------
3650

‎exercises/slicing.html‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ <h3>Tasks<a class="headerlink" href="#tasks" title="Permalink to this headline">
211211
<span class="k">return</span> <span class="n">a_new_sequence</span>
212212
</pre></div>
213213
</div>
214+
<p><strong>Hint:</strong></p>
215+
<p>Your functions should work with ALL sequences. That means that you cannot use list methods, like <code class="docutils literal notranslate"><span class="pre">.append</span></code>, because that won’t work with strings and tuples. But all sequences support concatenation with the <code class="docutils literal notranslate"><span class="pre">+</span></code> operator.</p>
216+
<div class="section" id="item-or-sequence">
217+
<h4>Item or Sequence?<a class="headerlink" href="#item-or-sequence" title="Permalink to this headline"></a></h4>
218+
<p>A key difference between using a single index: <code class="docutils literal notranslate"><span class="pre">seq[i]</span></code> and using a slice: <code class="docutils literal notranslate"><span class="pre">seq[i:j],</span> <span class="pre">seq[:i],</span> <span class="pre">seq[i:]</span></code> is that using an index returns a single item, whereas a slice always returns a sequence – even if that sequence is of length one or even empty. And concatenation requires a sequence, so make sure you use slicing if you want to concatenate the results.</p>
219+
<div class="admonition note">
220+
<p class="admonition-title">Note</p>
221+
<p>Python “gotcha” with strings. Python does not have a character type. Instead of a character, you get a length-one string. This can cause confusion sometimes, as other sequences return a single item when you index, so when you index into a list of numbers, you get number – which is not a list (or any type of sequence). But with strings, when you index into (or loop through) a string, you get a length-one string, which is, itself a string, and therefor a valid sequence. So: <code class="docutils literal notranslate"><span class="pre">a_string[i]</span> <span class="pre">+</span> <span class="pre">another_string</span></code> works, but <code class="docutils literal notranslate"><span class="pre">a_list[i]</span> <span class="pre">+</span> <span class="pre">another_list</span></code> does not work.</p>
222+
</div>
223+
</div>
214224
</div>
215225
<div class="section" id="tests">
216226
<h3>Tests:<a class="headerlink" href="#tests" title="Permalink to this headline"></a></h3>

‎searchindex.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)