The range
function lets us build a list of numbers.
list(range(10, 20))
Notice anything funny?
Python uses this convention everywhere.
a = list(range(10, 20))
type(a)
Let's talk about indexing.
Indexing in Python starts at 0.
a[0]
And goes from there.
a[1]
a[2]
What do negative numbers do?
a[-1]
a[-2]
You can get a sub-list by slicing.
a[3:7]
Start and end are optional.
a[3:]
a[:3]
Again, notice how the end entry is not included:
print(a[:3])
print(a[3])
Slicing works on any collection type! (list
, tuple
, str
, numpy
array)
a = "CS357"
a[-3:]