Array: An array is an object that stores group of elements(or values) of the same datatype. The main use of an array is to store and process the large group of elements easily.
Arrays are similar to Lists but can store only one type of data, which means we can store only Integer or Float or String type but not the combination. Array uses less memory and faster than Lists.
Arrays are dynamic in size, no need to declare the size. The size will increase when elements are added, similarly, when elements removed, it automatically decrease its size in memory.
Syntax:
arrayname = array(type code, [elements] )
The type code represents the data type of elements storing in the array.
'i' for signed integer numbers/elements
'f' for floating point numbers/elements
'd' for double precision floating point numbers/elements
'u' for unicode character or string type elements
Types of Arrays: The Arrays are basically two types, Single and Multi Dimensional.
Single Dimensional: These are arrays represent only one row or one column of elements.
eg: ary1 = array('i', [10,13,17,19,20])
Multi Dimensional: These are arrays represent more than one row or column of elements.
eg: ary2 = array('i', [1,3,5,8,10],
[11,14,17,19,22],
[25,27,33,42,56])
The Python module 'Array' needs to import while working with arrays. Now, we will see how to create and process the arrays in the following:
# Importing Array Module
import array as ar
# Creating an integer type Array
x = ar.array('i', [2, 4, 6, 8])
print('The integer typer array x is: ', x)
# Importing Array Module (another way)
from array import *
# Creating a float type Array
y = array('d', [1.5, 2.5, -3.5, 4])
print('The float type array y is: ', y)
# Creating a string type Array
ary1 = array('u', ['a', 'b', 'c', 'd', 'e'])
print('The elements in string array are: ', ary1)
for ch in ary1:
print(ch, end=' ')
# Creating an array from another array
arr1 = array('d', [1.5, 2.5, -3.5, 4])
arr2 = array(arr1.typecode, (i * 3 for i in arr1))
l1 = len(arr1)
print('\nThe length of the array 1 is: ', l1)
print('The elements in array 1 are:', end='\n')
for i in range(l1):
print(arr1[i], end=' ')
print('\nThe elements in array 2 are:')
for i in arr2:
print(i, end=' ')
# Indexing array elements
x = array('i', [10, 20, 30, 40, 50, 60, 70])
y = x[2:4]
print('The array y:', y, '\n')
y = x[:4] # indexing first 4 elements from start
print('The array y:', y, '\n')
y = x[-4:-2] # indexing last 2 [-4-(-2)=-2] elements from right end
print('The array y:', y, '\n')
y = x[1:7:2] # indexing 1 to 7 elements with step2
print('\nThe every 2nd element between 1 and 7 of array y:\n', y)
# Appending elements to the Array
x = array('i', [10, 20, 30, 50, 70])
x.append(40)
x.append(60)
print('The array after Append: ', x)
# Inserting an element in 1st position into the Array
x.insert(1, 99)
print('The array after inserting 99 in 1st position: ', x)
# Removing an element from the Array
x.remove(30)
print('The array after removing the element 30: ', x)
# Removing the last element from an Array
n = x.pop()
print('The array after removing the last element', x)
print('The popped element in the array is: ', n)
# Finding the Index position of an element in Array
n = x.index(50)
print('The first occurrence of 50 in array is at : ', n)
# Converting an Array to List
L1 = x.tolist()
print('list: ', L1)
print('array: ', x)
Output:
The integer typer array x is: array('i', [2, 4, 6, 8])
The float type array y is: array('d', [1.5, 2.5, -3.5, 4.0])
The elements in string array are: array('u', 'abcde')
a b c d e
The length of the array 1 is: 4
The elements in array 1 are:
1.5 2.5 -3.5 4.0
The elements in array 2 are:
4.5 7.5 -10.5 12.0 The array y: array('i', [30, 40])
The array y: array('i', [10, 20, 30, 40])
The array y: array('i', [40, 50])
The every 2nd element between 1 and 7 of array y:
array('i', [20, 40, 60])
The array after Append: array('i', [10, 20, 30, 50, 70, 40, 60])
The array after inserting 99 in 1st position: array('i', [10, 99, 20, 30, 50, 70, 40, 60])
The array after removing the element 30: array('i', [10, 99, 20, 50, 70, 40, 60])
The array after removing the last element array('i', [10, 99, 20, 50, 70, 40])
The popped element in the array is: 60
The first occurrence of 50 in array is at : 3
list: [10, 99, 20, 50, 70, 40]
array: array('i', [10, 99, 20, 50, 70, 40])
Process finished with exit code 0
# ------------------------------------------------------------------------------------------------------------------------ #
Notes:
We can work with Arrays by using the 'numpy' package as well. The 'numpy' contains a large library of mathematical functions like linear algebra.
The package 'numpy' contains several classes, functions, variables etc. The 'numpy' is useful to create and process single and multi dimensional arrays as well.
Example1:import numpy
ary = numpy.array([10,12,15,17,20])
print(ary)
Example2:
import numpy as np
ary = np.array([10,12,15,17,20])
print(ary)
Example3:
from numpy import*
ary = array([10,12,15,17,20])
print(ary)
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.