Hello and welcome to this video.
In this video I will be showing you how to access the elements of a list so you can access the elements
of values in a list by using the index.
The index basically refers to the position of the elements in a list with a pint on list.
The index are zero based.
What that means.
The first element or item in the list is given a position of zero.
So let's take a look at an example.
So let's look at this list here which is called animals.
So with this list if we want to access say the bear.
This will be Position ZERO.
This would be position 1 and this will be position two.
So it is a zero based index.
So let's take a look at how we would do this.
So I've got some code here from a previous video so I'm going to comment out some lines here.
So because I don't want them to be printed I want them to be ignored.
So I just commented out all right.
So these are all commented out now.
So if I wanted to access the bay from this list this is how I would do it.
Type in print and in parentheses I refer to the name of the list.
So the name is called animals and then inside that are the square bracket and type in a zero.
So the zero is the index.
So what that means when this print.
It will just grab the zero index which is the first item or element in the list which is this code bear.
So I'm going to save that and if I open up my command prompt and just typing the name of my file which
is my list dot P Y and press enter and you can see is giving me the value of Bear which is the first
item on the list.
Now if I wanted to access the tiger the tiger I will have an index of 1.
I'll have to replace the 0 with a 1 and so on.
Depending on how many items you have on the list if you notice because I have commented up blocks of
my code the bits I have added a comment to it has not been displayed on the screen.
So comments are quite useful if you want to try different things and you don't want all the code in
your file to be acted upon.
So it blocks out the aspects you don't want to be executed or printed on the screen.
You can also access elements within a list from the back of the list.
So if you're doing a zero based index it would access the elements from the first.
Now if I change this from zero to say negative or one minus one what that will do it will access from
the back of the list it will not give me elephant rather than bed.
Let's check this out.
I'm just going to save the file and open up my command prompt and execute.
You can see now is giving me elephant instead of the bed because I have used a negative when you use
a negative index.
It gives you a value from the back of the list.
Another thing you can do with access in list items is that you can access a range.
For example I can if I wanted to access save from this to this you can specify that or if you want to
access if you want to start listing from a set an index you can.
So this is Position ZERO.
This is position well let's say I want to just print out anything from position 1 and not 0.
So the way we do that inside my print statement I would just do one and I'll do a call on what that
would do it will print out everything apart from the position of zero.
So let's check that out.
And I'm just going to type in the name of my file which is my list the p y and press enter and you can
see it prints out tiger lion pendant elephant but he skips the bear.
So by using the index number on the colon to print every 10 after that index as you can see here another
thing you can do is you can specify a range.
So let's say I only want the tiger and the lion printed on the screen.
So instead of just doing the one on the column I can come here and add three.
So this is Position ZERO position one position to position three.
What this will do it will print just position one position two but it will not print position number
three.
So let's save that and check that out.
Inside Hart terminal all command prompt.
So I just type in the name of my file which is my list.
The P Y press printer.
And as you can see this is just printed out the tiger and the lion the tiger is position one and the
lion is position 2.
It doesn't print position 3 so I'm giving it a range.
So I'll say print grab the values from position 1 and 2.
All right does what that does on the final thing I want to show you in this video is how to modify the
lists.
So if you want to change the item in the list let's say this item called Bear.
I want to change that to a dog the way I would do that.
I just type in the name of the list which is animals and how add the square bracket and add the index
of what I want to change which is the bear.
He has a index of zero and then I use the equals to sign to set a new value for what I want to change
it to.
So let's say I want to change the value of the bear to the dog.
That's how I would do that.
So I do control is to save on a Mac.
You can do command s.
Now I can test this if I do a print inside that print and type in the name of my list I'm passing the
index of 0.
Now this index of 0 will no longer see bear it should now see dog because I have made a modification
to that name.
So let's save that.
Let's go back into my command prompt.
I type in my list the p y and press enter and as you can see is giving me the value of dog.
So when you executing code it would do it.
Line by line.
So this here is printing out this code here on line 5.
That's this line and then this print here on May 7 refers to this dog that I am telling it to grab the
value of that index 0.
So that's basically how you can play around and access values and also modify values within a list.
Thanks for watching and bye for now.