Skip to content

Commit 5226456

Browse files
committed
added a bit more explaination to the slicing lab
1 parent fee198b commit 5226456

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

source/exercises/slicing.rst

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

0 commit comments

Comments
 (0)