In this lecture, I'm going to talk about the three types of sequences that are available in Python
sequences, done types that allow you to store multiple items under the same variable name and Python,
both see Python and Micro Python provided three types of sequences.
We have lists, tables and ranges in this lecture.
I'll show you how to use all three in a basic way so that you are prepared to use them in the lectures
that are coming up later in this course.
Let's begin with lists.
So lists are mutable sequences.
Mutable means that once you create a list, you can change it later.
You can edit individual items in a list and typically we use them to store items that are all of the
same kind.
So you can have a list, for example, that store integers or a list that contains floating point numbers
or strings.
Tuples, on the other hand, are immutable.
There are still sequences we can store multiple items in a temple, but unlike a list, you can't make
a change to a tuple once you create it again.
Unlike a list, though, in a table, typically you can store data items of different types.
So you can have a table that contains numbers and strings.
No problem at all.
Finally, Rangers' is a type of sequence that contains numbers and just like a tuple, it is immutable
and we use ranges to count in loops.
For example, is the most common case use for a range.
So let's have a look at some examples.
And these examples, again, are borrowed from the documentation, which is where you can go to do more
reading about each one of those.
So is the invitation for lists.
These are the documentation for tables.
And finally, you've got the range function here that we use to generate range sequences.
All right.
So let's begin with lists to create a list.
Just going to copy so that if there save myself from typing and making typing mistakes.
So this is how you can quickly create other ways as well, as you can see in the documentation.
But in most cases will be creating a list like this is a variable name is always the assignment operator.
And then we've got the square brackets that contain the items that we are storing in a list.
In this case, as you can see, we've got a list of strings.
We submit this command and now we have a list.
We can have a look at the contents of the list by just calling its name.
And you can see again is denoted by the open and closed square brackets and then the items inside the
list.
I can have a list of integers, for example, in TS, and it's exactly the same way, except that now
we've got the numbers.
Let's make it four.
And here's the list tab for code completion.
And these are the integers.
We can also do things such as list the integers in reverse order.
So let's say a list of letters and I can say that give me a list that is sorted in reverse, reverse
order.
And it would look like this just printed out now.
So internally, Python has rearranged the items in this list of letters and now can print it out.
And you can see that it's printed out in reverse order.
You can find more information about this and the various methods that are available here.
I'm taking some examples out of this documentation and also show you one more place.
So the conclusion that shows the functions that are available for a list in the Python three library
standard types documentation, then you can see that this is one of the example operators that he can
use of functions.
And the one that I've used is stored and I turned reverse into true so I can give me the listing or
items of the list in reverse order.
I can do the same thing for the numbers.
Of course I can say list.
Of integers and just a short reverse order like this, and now the numbers come out from top to bottom
review, highest and lowest.
I can also retrieve individual items out of a list by using the square bracket notation.
So, for example, if I want to see the second item in this list, which is index zero and one, then
I can call that like this list of letters, open square bracket index one and close the parentheses.
And that will give me a.
So this is the second item in this area, you can click on the area as well, and the object inspector
will also give you an index number and the value stored at that index number for go for the same thing.
But now index to as you see here, that's a and most getting a ratio in the exact same thing happens
with the list of the numbers and lets you do that exercise or least clear up and move on to tuples and
to create a tuple just copy and paste the table that I've already created here.
And you can see that I'm using the same method, essentially, as with every variable of the name of
the variable, the assignment operator.
But now instead of square brackets that I'm using for the lists, I'm using a parentheses, open and
close parentheses.
And inside, as far as the items of the table are concerned, you can see heterogeneous items.
I'm using integers and strings, floating point numbers, and that is no problem at all for a tuple
I can.
Print out the contents of the table like this on the show, and you can see that it comes back with
the parentheses instead of the square brackets.
And that's how, you know, whether you're working with a table or with a list is by looking at the
types of brackets that are used, whether they are parentheses or square brackets, in order to retrieve
an item from a table and using the same notation as I did earlier with the list.
And I'm going to use this time square brackets.
So this is the thing that sometimes can be confusing.
So, for example, if I want to print out, just going to click on the table, go and it appears on
the inspector I to print out the let's say the.
Item number one, index number one from the tuple, then I use the square bracket to indicate that and
hit enter and I'm getting a fair go for.
Indexed to that will give me the number to out, since that's what's stored in index to there's more
documentation about how those here in Python, three libraries, standard types, and you can see more
information about how to use tuples here.
All right.
Now, the last one that I want to show you is the range.
So the range to said we use that there's a function called range that creates a sequence of numbers
and the documentation for the range of the leadership right here in this tab.
So here's some examples of how you can use it to the function range if it requires at least one parameter.
But you can have more than one error in this example here has got two parameters and there's one more
down here.
Believe it's not listed here.
I'm going to give you an example.
We can have three parameters, but the source documentation and you can look at it right here, we can
see the range function and there are three parameters that is capable of receiving as a start stop.
And then if you want, you can define a step which can be negative or positive.
So let's have a look at a few examples here of how to use ranges.
So first of all, I'm is going to create a simple range.
It's a sequence of numbers that count from zero to one.
Let's check it out.
Check out its contents.
That is, you can see zero to 10.
And if I click on the object inspector, that's what comes up right here for use the list command and
ask Python to list the contents of this variable range zero to 10.
Then you'll see the actual components of this range.
You can see called square brackets, which means that there is a list in the square.
I was able to use the list command to show me the actual items in this list.
So the thing to take away from this is that the range function allows us to quickly create lists of
numbers.
So if you just use a single parameter, then the list of numbers that you get starts from zero and goes
all the way to the number just before the number that you have specified in the function.
So it's zero indexed and therefore is going to go up to nine.
The variation is to have a list such as this where we use the range function, but now we got the two
parameters.
The first number is the starting number and then we got the last number.
But the ending number.
And if you use the list function to check the contents of this list, you'll see that it starts from
five inclusive and goes to nine not inclusive.
The last variation of the range function that I want to show you is the one that used this, the third
parameter for the step.
So in this case, we've got range zero to 50, step five.
It's going to create a list that starts from zero finishes at forty nine, but count at five point thirty
five.
That will look like this.
This range.
All right.
Zero, five, 10, 15 and so on, up to forty five.
You can also go negative.
So I'm going to go to modify this is going to be from 50 to zero, Steph, minus five and five.
Use a minus sign here.
So there's going to be five zero and minus five.
And let's give a list to this range, 50 to zero.
And it looks like this.
So counting down of it can go down to negative numbers.
Maybe.
Let's try this.
Let's go up to minus 10.
You saw the mistake here that should be minus 10 and.
Let's make that 10:00 for the new range and let's print it out.
We can have negative numbers, so all this is going to be very useful.
You can see in.
This example here where we can use the range inside a full loop in order to create this loop based on
this range is something that will be using quite frequently in the lectures that are coming up.
OK, so that's about the three types of sequences in.