Sequence data operations: Indexing
Indexing just means accessing elements. To access elements, the square brackets can be used. There
are many methods to access elements in python.
index() method finds the first occurrence of the specified value and returns its position
Syntax: object.index(sub[, start[, end]] ), object[index]
• Index of the element is used to access an element from ordered sequences
• The index starts from 0
• Negative indexing is used to access elements from the end of a list
• In negative indexing, the last element of a list has the index -1
String Indexing
The first let us look at what string data type is. A string is a sequence of one or more characters,
for example, it can contain letters, numbers and symbols and that can be either a constant or
variable. And strings are basically a mutable sequence in Python. So, to create a string we can
close a sequence of characters inside single, double or triple quotes. Let us see an example on
creating string. Create string includes A sequence of characters inside single, double or triple
quotes.
I am creating the string called strsample and it has a sequence of characters which describes the
word learning and storing that on two variable called strsample. So that becomes a string. So we
can print out strings by simply calling the print function. So when we print the string strsample
will be getting an output call learning. So the strsample is now a string.
List Indexing
So let us move to the
next sequence data that is called list.So here I am just going to illustrate you the indexing part using the list sequenced data. So syntax
is given here you can use the list name and you can use the index function. And inside the
function we can just give the element for which you want to find the position. So I have already
created a list call list lstsample which has few elements to it. It has both numbers and strings and
to find the index of the element sam I can use the index method.
Inside the method I can just give the element sam. So that gives me an output three because
index corresponds to Sam is 3. The index corresponds to 1 is 0 and the index corresponds 2 is 1
and index corresponds to a is 2 and index corresponds to sam is 3. But if you are going to find
the element corresponding to a given index, then you can get that using square brackets. So let us
just try that I am using lissample of two inside the square bracket.
So it is going to give me an element which response to the index 2 which is nothing but a here.
Similarly you can access any elements from the end of a list as well using the negative indexing.
So if I give lissample of -1 is going to give me the last element of the list lissample. So which is
nothing but 2.
Array Indexing
Similarly we can perform the indexing on an array. So we have already seen how to create an
array. So let us just use the same example. So I print out the values of array. The array sample
has values 1, 2, 3 and 4. So, here I just have an example to show you how you can perform the
negative index on this array. I am accessing the third last element of the array so it is going to
give me the value 2 which is nothing but the third last element of the array.
And similarly we can perform that on a tuple. So you can use the index method to find the
position of the given elements. So let me just use py so the index of py is 5. So here I am just
trying to find the element which responds to the second index which is 3 here. So, let us see how
the indexing works in set.
So we were already created a set sample which has example 24, 87.5 data 24 and data as
element to it. I am trying to use an index call 4 which gives me an error saying set object is not
subscriptable which means that you cannot perform the indexing operation set. As we know that
set is container to hold non sequential data. You will not be able to access the elements inside the
set using indexing.
Dictionary Indexing
So next we are going to see how we can perform indexing on a dictionary. So, Python dictionary
object provides a key-value pair facility. So the values in the dictionary are indexed by keys and
they are not held in any order. So, if you want to access the values from the dictionary then you
can access them by using their keys. So we have dictionary called dict sample which we have
already created which has keys as one second 3 and 4 and it also has a key value corresponding
to it.
For example first, 2, 3 and 4 so now let us see what happens when you are trying to access an
element using a index I am using index say for example that is 2 it gives you the key error that is
2 as we know that values in a dictionary or indexed by keys. So you will be able to access them
using keys not using the index. So now I am using key inside my square braces. So I am going to
get a value corresponding to the key that is 1.
So the value corresponding to the key one is first and as we know that he can may be a string or
integer value. So key can either have string as well as a integer. So here I have a key called
second so I will be able to access the value corresponding to the key second. So I am getting
output as 2. So, the value corresponding to key second is 2.
Range Indexing
So the last one example which is the last one is to illustrate the indexing using range. So we have
already created range sample which contains a value is 1, 5 and 9. So, you should be using an
appropriate value which is already there in your range if you are trying to give you a value which
is not in the range then it is going to throw you a value error. We are trying to check the index
corresponding to the value zero.
But in our case the range sample does not have the value 0 at all because it has only the values 1,
5, 9 in that case it throws a value error. So it says zero is not in range. But if you are trying to
find the position of the given element that is 9 here you will be able to do that because we
already have a range value that is 9 and it gives you the position of that particular element that is
2 and we can also get the elements given the index.
So, given the index as one, so it give the output as 5 because the index corresponding to the
value one is zero and the index corresponding to the value 5 is 1. But if you are trying to give the
index which is out of range then it is also going to throw you an error saying index error because
you have an error in the given index that is why the error says it is index error and saying range
object index out of range. Because you do not have any element corresponding to the index 9
that is, why the error says index error.
0 Comments:
Post a Comment