December 2018
Beginner to intermediate
796 pages
19h 54m
English
At the beginning of this chapter, we saw slicing applied on strings. Slicing, in general, applies to a sequence: tuples, lists, strings, and so on. With lists, slicing can also be used for assignment. I've almost never seen this used in professional code, but still, you know you can. Could you slice dictionaries or sets? I hear you scream, Of course not!. Excellent; I see we're on the same page here, so let's talk about indexing.
There is one characteristic about Python indexing I haven't mentioned before. I'll show you by way of an example. How do you address the last element of a collection? Let's see:
>>> a = list(range(10)) # `a` has 10 elements. Last one is 9.>>> a[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> len(a) # ...
Read now
Unlock full access