Exploring New Techs!

Python – Arrays

 

Hello Technotizers, this article will focus on Python – Arrays data structure and its most commonly used methods.

1.      Python - Arrays:

An array is a container like object which holds a fix number of items of the same data type. Arrays are used to store multiple values in a single variable. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms in order to understand Array.

  • Element− Each item or value stored in an array is called an element.
  • Index – The corresponding location of an element in an array has a numerical index, which is used to identify the element.

Ø  Array Representation:

Below is an illustration of how an array is represented.

 



According to the above illustration, following points are observed:

·         Index of an array begins with 0.

·         Array length is 8 so that it can store 8 elements.

·         Elements are accessed with the help of indexes. For e.g. element 14 is present at index 4.

Ø  Basic operations on an array:

·         Traverse:  Access all array elements one by one.

·         Insert: Add new element at the given index.

·         Delete: Delete an array element at a given index.

·         Search: Search an element using index or value.

·         Update: Update an element at the given index.


Ø  Declaration of an array:

Array is formed by importing the array module in the program. The array is then declared as below:

from array import *                        
arrayName = array(typecode, [Initializers])

Typecode is the code used to define the type of the values the array will hold. Some common typecodes are given below:

Typecode

Value

b

Signed integer of size 1 byte

B

Unsigned integer of size 1 byte

c

Character of size 1 byte

i

Signed integer of size 2 bytes

l

Unsigned integer of size 2 bytes

f

Floating point of size 4 bytes

d

Floating point of size 8 bytes


   
Below code creates an array of name my_array:

from array import *                      

my_array = array('i', [5,10,15,20,25,30])

for x in my_array:                       

    print(x)                             

 In the above code, ‘i’ is the type of the values in the array which is Signed integer of size 2 bytes.

The above code when executed gives the following output.

 

Output:

 

5 

10

15

20

25

30

 

Ø  Accessing Array Elements:

Array elements are accessed so that they can be used in other parts of the code. Index of the element is used to access it.

 

from array import *                      

my_array = array('i', [5,10,15,20,25,30])

my_element = my_array[0]                 

print(my_element)                        

print(my_array[3])                       

The above code gives the following output:

5  

20 


The output specifies that element 5 is present at index 0 and element 20 is present at index 3.

Ø   Insertion Operation:

A new element can be inserted into the array at the beginning, end or at the middle. The in-built python method insert() is used to add new element. The syntax will be : array_name.insert(index,value)

from array import *                       
my_array = array('i', [5,10,15,20,25,30]) 
my_array.insert(2,35)                     
for x in my_array:                        
    print(x)                              

The output is given below:

Element 35 is added at index 2.

5 

10

35

15

20

25

30

 

Ø  Deletion Operation:

Deletion refers to removing an existing element from the array and re-organizing all elements of an array. Here, we remove a data element at the middle of the array using the python in-built remove() method. The syntax will be array_name.remove (element)

from array import *                        
my_array = array('i', [5,10,15,20,25,30])  
my_array.remove(20)                        
for x in my_array:                         
    print(x)                               

The output is given below where we can see that element 20 is deleted.

5 

10

15

25

30

 

Ø  Search Operation

We can perform a search for an array element based on its value or its index. Here, we search a data element using the python in-built index() method. The syntax will be array_name.index(element). The method returns the index where the element is present.

from array import *                       
my_array = array('i', [5,10,15,20,25,30]) 
print(my_array.index(20))                 

The output is given below where the index of element 20 is obtained which is 3.

3  

Ø  Update Operation:               

Update operation refers to updating an existing element from the array at a given index. Here, we simply reassign a new value to the desired index we want to update.

from array import *                       
my_array = array('i', [5,10,15,20,25,30]) 
my_array[3] = 40                          
for x in my_array:                        
    print(x)                              

The output is given below where the element 20 is replaced by 40.

5 

10

15

40

25

30


Here, we end this article. Hope it was helpful. See you then!!

Keep coding and exploring new techs!!

0 comments:

Post a Comment