Arrays plays a crucial role in python.An array is a special variable,which can hold more than one value at a time.
An array can hold many values under a single name,and you can access the values by reffering to an index number.
Array in Python can be created by importing an array module.
Array Methods:
append():Adds an element at the end of the list.
clear():Removes all elements from the list.
copy():Returns a copy of the list.
count():Return the number of elements with the specefied value.
reverse():Reverse the order of the list.
index():Returns the index of the first element with the specified value.
insert():Add the element at the specified position.
pop():Removes the elements at the specified position.
import array as arr
a = arr.array('i', [11,12,13])
print("Array before insertion : ",end=" ")
for i in range(10,13):
print(a[i], end=" ")
print()
a.insert(11,14)
print("Array after insertion : ", end=" ")
for i in (a):
print()
b = arr.array('d', [12.5, 13.2, 13.3])
print("Array before insertion : ", end=" ")
for i in range(10, 13):
print(b[i], end=" ")
print()
b.append(14.5)
print("Array after insertion : ", end=" ")
for i in (b):
print(i, end=" ")
print()
Output:
Array before insertion : 11,12,13
Array after insertion : 11,14,12,13
Array before insertion : 12.5,13.2,13.3
Array after insertion : 12.5,13.2,13.3,14.5