Generally, we add elements in lists, tuples and we delete items,
insert elements etc but how can we do that, how can we access a particular element,
this is done by using index number.
PDF FOR INDEX IN PYTHON
Ø
Index numbers are nothing but numbers which are
given to elements in a list or tuple or array etc to access them.
Ø
Index number starts with zero and ends with one
less than the given number but
For example
List = [1,5,8,13,24,31]
0 1 2 3 4 5
Ø
Each index refers to one element, as we know
here the elements in the list are 6 but the maximum index will be 5 this is
because index starts with zero so maximum index will always be one less than
the length of list or tuple or array etc.
ADVANTAGES in Python:
Ø
It makes our work easier like we can easily
delete the element we want to delete by just using its index number.
Ø
There are lot of ways to print elements using
index values.
Ø
By directly using that element in square
brackets
For example: Python_closet = [7,27,56,65,48]
0 1 2 3 4
If we want to print only one element of the list then we can print using its index number in square brackets like this
Print(python_closet[3]) // here index 3 refers to the 4th element in the list//
65
Syntax = print(list name or tuple name [index number])
Here we printed pythoncloset[3]for the 4th element in list because as we all know that index value starts with zero so for 4th element in list its index will be one less than it
EX: if we want to print second element then print(python_closet[1])
If we want to print third element then print(python_closet[2]) like this we can use index number .
If in case we have a nested list or a nested tuple then we can access elements like this:
Python_closet=[2,36,56,[1,4,6,7]]
Here how can we take indexes for this nested list that will be your question, well it’s so simple.
Here the complete nested list will be considered as an index like
0, 1 2 , 3
[2, 36, 54, [1 ,4, 6, 7]]
0, 1 , 2 , --- 3 ---
If we want to print 6 from the above list
Then print(python_closet[3][2])
6
Same for number 7 print(python_closet[3][3])
7
This is because whole second list is a list and, in that list, again we take index values from zero, so if we want a number then bracket of index number of that list (to access elements inside that list), index of the element in that list. This might be somewhat confusing but when u try this on your IDE you will understand. this is the method for nested ones
*** Accessing elements using negative indexes
We can also access elements using negative indexes like -1 and -2
For example:mypython = [31,65,69,72,84]
-5 , -4 , -3,-2 ,-1
Here in negative indexes the last number of the list or the index of the last element of list will be -1, as we know indexes start from zero but if we want to use negative index then last element will always have an index of -1 and then it comes in reverse -2 ,-3 and so on
Example:
print(mypython[-1])
84
Print(mypython[-3])
69
*** Accessing elements using slicing
Pythontuple=(25, 36 ,42 ,79 ,51)
0 , 1 , 2 , 3 , 4
Ø
using ":" this operator we can print from one
particular element to another element.
print(pythontuple[0:3])
so from the first index to the second index we give it will print all the
elements in between that two indexes including first index but it will exclude
last index that means in above case it starts printing from index 0 to 2 but it
will not print the element in 3rd index which means here the last
element
example: print(pythontuple[0:3]) 3rd index will be excluded and
till 2nd index elements will be printed
25 , 36, 42
print(pythontuple[2:4])
42 ,
4th index will be excluded because it is the element given after colon or the last index, it prints from 2nd index and 3rd index and it stops.
Ø
If we don’t define any index value before
operator then it automatically takes the first
Element
Example: print(pythontuple[:3])
25 , 36 , 42
Here we didn’t mention an index before colon which means I didn’t give any index number from which index the elements should be printed , when we don’t mention any index before colon then computer will automatically consider 0 which means here 0 index so from there it starts printing elements , if we want to print elements from first index then we need not mention zero it will automatically consider zero if we don’t write any number.
What happens if we don’t give last index, I mean what happens when we don’t mention any number after colon
Example: print(pythontuple[2:])
Here we didn’t give the last index or index after colon so what happens is it will print till the last element in that tuple
Print(pythontuple[2:])
42,79,51
So here it prints till last element
What happens if we don’t give both the numbers, which means what happens when we don’t mention the index before colon and after the colon also
Then it prints all the values from first element to last
element in that tuple Including first and last element also. so, if you want to
print that complete tuple just put: this symbol and print
Print(pythontuple[:])
25 ,35 ,42 ,79 ,51
Ø This
is what happens because if we don’t define any index value before the symbol it
automatically takes 0 as first index and if we don’t mention any index after
symbol it prints till the last element. so, if we don’t give both then all
elements in that list or tuple will be printed.
**** programming is not about reading some books or remembering some functions ,it is about practicing the more you do the more you will get so just don’t read and stop ,read and try to do code so that u can remember stuff and can understand still better when you try TO DO them on your IDE.
YOU CAN DOWNLOAD PDF HERE INDEX IN PYTHON
My instagram page to Learn Python from basics.
Comments
Post a Comment