Friday, April 13, 2018

Array Basic Operations


Basic Operations

Following are the basic operations supported by an array.
·        Traverse − print all the array elements one by one.
·        Insertion − Adds an element at the given index.
·        Deletion − Deletes an element at the given index.
·        Search − Searches an element using the given index or by the value.
·        Update − Updates an element at the given index.

Array is created in Python by importing array module to the python program. Then the array is declared as shown below.

from array import *

arrayName = array(typecode, [Initializers])

typecode are the codes that are used to define the type of value the array will hold.

Some commontypecodesused are:
Typecode
Value
b
Represents signed integer of size 1 byte
B
Represents unsigned integer of size 1 byte
c
Represents character of size 1 byte
i
Represents signed integer of size 2 bytes
I
Represents unsigned integer of size 2 bytes
f
Represents floating point of size 4 bytes
d
Represents floating point of size 8 bytes

Before looking at various array operations lets create and print an array using python.

The below code creates an array named array1.
from array import *

array1 = array('i', [10,20,30,40,50])

for x in array1:
 print(x)

When we compile and execute the above program, it produces the following result

Output

10
20
30
40
50