to visualise it we can say:
array: 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 , .........., 100
array's
index : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , ........., 99
So now you can easily see how arrays are indexed in python so If using python code which is (discussed below), call out a specific index number it will give me the value stored at that specific index number. Another benefit of indexing is that if in an array values are repeating they will still have a unique different index number so that they are uniquely identified.
THE DIFFERENCE BETWEEN LISTS AND ARRAYS.
The question regarding array is that what is the main difference between array and lists since their functionality is same. the answer is pretty simple and straight-forward that is: arrays can store only single data type value, one array can be specified to only contain one data type values be it integers or strings etc ,while list can store any data type value be it strings, integers or floats together in one list.
Its benefit? to put it simply if you want a number to be multiplied by many integers you put those integers into an array so that whole array can be multiplied whereas if you put those in a list and tried to multiply them, python will show an error.
HOW TO CREATE ARRAYS
to create an array in python we first need to import the array module which can be done by typing:
import array or import array as arr or from array import *
a simple code to create an array would be:
import array
a = array.array('i',[1,2,3,4,5,6,7])
print(a)
ACCESSING ARRAYS
Values or elements in arrays are accessed using their unique address i.e their index number (discussed above). before I show you the code you need to know that their exists a negative indexing too just like the normal indexing starts from 0 and goes from left to right, the negative indexing starts from right with its first index number as -1 and goes to left increasing in its negative number. so if an array has 100 as its last vaue its index would be 99 but its negative index would be -1. and when accessing arrays both the index numbers either 99 or -1 can be used to call the value 100.
a code to access values in an array:
import array
a = array.array('i',[1,2,3,4,5,6,7])
print(a[2])
if you look closely there is minimal difference in the codes written above that's the benefit of python as it makes task simpler and quicker. As you can see when accessing a value from inside an array you simply type its index number with the array's variable in the print function. In this print function we are printing out the a of 2 that is the value stored in the array a at index place 2 and the output will be 3.
THE LENGTH OF AN ARRAY
To find the length of an array as in how many values does an array holds you can simply use the python len function. what you will do is that you will create another variable let's say length and in that you will use the len function to count the number of values in that array and then print that variable length.
a code to find length of an array:
import array
a = array.array('i',[1,2,3,4,5,6,7])
length = len(a)
print(length)
CHANGING VALUES IN ARRAY
To change a certain value in array you can simply take that particular value's index number as variable and assign a new value to it.
a code to change a certain value:
import array
a = array.array('i',[1,2,3,4,5,6,7])
a[2] = 5
print(a)
In this code i wanted to change the value stored at index 2 so I took it as my variable a[2] and assigned it the value 5 and when I printed it the array had value 5 on the index number 2.
ADDING VALUES IN AN ARRAY
You can easily add values in an array by using python's in-built append function. you just take the array's variable and append the new value into it.
a code appending a value into an array:
import array
a = array.array('i',[1,2,3,4,5,6,7])
a.append(8)
print(a)
So in this code I just appended value 8 into the array variable and when we print this array out we will surely see the value 8 right after 7.
REMOVING VALUES FROM AN ARRAY
To remove a value from an array thank to python we again use an in-built function called pop. yeah it is called pop in the sense to burst that value like a baloon so it doesnt exist anymore.
a code removing a value from an array:
import array
a = array.array('i',[1,2,3,4,5,6,7])
a.pop(3)
print(a)
In this code the value stored at index number 3 is being popped out and when we print it we won't see the 3rd value that was 4.
So these were some basics of arrays in python. hope you all did learn something from it.
Comments
Post a Comment