Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more
Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c
To make everything easy: We refer CPython as Python
Basically, the array is the collection of similar kinds of data types. In arrays, elements are stored in contiguous memory locations. Python arrays are similar to the python list but the difference is python array can contain elements of similar types only. Similar to the python lists, indexing is possible in python arrays.
Creating a Python ArrayPython array can be created by importing array module. In python, to create an array, we need to first declare the code of data type of the elements.
Syntax of the array is given as:
>>>array(data_type_code, value_list)
So, during the creation of an array, we've to first write the datatypes and then corresponding value_list. These are enclosed inside the () brackets.
Now, let's create an array.
import array as arr
arr1=arr.array("i",[1,2,4,3,5,6])
print("The Array elements are:\n")
for i in arr1:
print(1,end=" ")
Output
The Array elements are:
1 2 4 3 5 6
Different datatype's specification codes used are listed below:
Code | C Type | Python Type | Min byte |
---|---|---|---|
b | signed char | int | 1 |
B | unsigned char | Unicode | 2 |
u | Py_UNICODE | Unicode | 2 |
h | signed short | int | 2 |
H | unsigned short | int | 2 |
i | signed int | int | 2 |
l | signed long | int | 4 |
L | unsigned long | int | 4 |
f | float | float | 4 |
d | double | float | 8 |
To access array items use simply use array indexing. Which is similar to list indexing.
#program that access the array elements.
alpha_Array = arr.array('u', ['a','b','c','d','e'])
print("First Element: ",alpha_Array[0])
print("Last Elements: " ,alpha_Array [-1])
print("Second Last Element: ", alpha_Array[-2])
Output
First Element: a
Last Elements: e
Second Last Element: d
Modifying array items-Adding items to the array.
Arrays are almost similar to lists. We can use append(), insert(), extend(), etc functions to perform some modifications.
>>>alpha_array = arr.array('u', ['a','b','c','d','e'])
>>>print(alpha_array)
array('u', 'abcde')
>>>alpha_array.append('f')
>>>print(alpha_array)
array('u', 'abcdef')
>>>alpha_array.insert(0,'z')
>>>alpha_array.insert(-1,'y')
array('u', 'zabcdeyf')
>>>alpha_array.extend(['r','s','t'])
>>>print(alpha_array)
array('u', 'zabcdeyfrst')
Removing items from an Array
We can simply use del statement, remove(), pop(), etc functions in an array to remove items just similarly we do in a list.
>>>#Program to delete items from an array with different methods.
>>>alpha_array = arr.array('u', ['Z','a','b','c','d','e','y','f','r','s','t'])
>>>print(alpha_array)
array('u', 'Zabcdeyfrst')
>>>#The pop() method.
>>>alpha_array.pop()
>>>alpha_array.pop()
>>>print(alpha_array)
array('u', 'Zabcdeyfr')
>>>#The del statement.
>>>del alpha_array[0]
>>>print(alpha_array)
array('u', 'acdeyfr')
>>>#The remove() method.
>>>alpha_array.remove('c')
>>alpha_array.remove('d')
>>>print(Alpha_Array)
array('u', 'abeyfr')
Slicing an Array
We can access the required range of elements by slicing an array. It is done using the slicing operator(':').
Example
>>>import array as arr
>>>int_array = arr.array('i',[23,3,4,2,5,45,6,3,65,43])
>>>print(int_array[:]) #prints all items.
array('i', [23, 3, 4, 2, 5, 45, 6, 3, 65, 43])
>>>print(int_array[1:]) #prints 1st index to end.
array('i', [3, 4, 2, 5, 45, 6, 3, 65, 43])
>>>print(int_array[:-1]) #prints second last index to beginning.
array('i', [23, 3, 4, 2, 5, 45, 6, 3, 65])
>>>print(int_array[2:6]) #prints second index to fifth index.
array('i', [4, 2, 5, 45])
>>>print(int_array[:-6]) #prints beginning to 5th index.
array('i', [23, 3, 4, 2])
Changing items with slicing
We can use slicing to insert, replace and delete items from an array. We can modify it for the required range of items using slicing.
Example
>>>import array as arr
>>>arr1 = arr.array('i',[1,2,4,5,6])
>>>print(arr1)
array('i', [1, 2, 4, 5, 6])
>>>arr1[1:4]= arr.array('i',[8,9,10]) #replaces 1st, 2nd and 3rd indexes.
>>>print(arr1)
array('i', [1, 8, 9, 10, 6])
>>>del arr1[1:3] #delete 1st & 2nd indexes.
>>>print(arr1)
array('i', [1, 10, 6])
Seaching items in an array.
We can find the index of a particular element of an array by using the index() method.
Example
import array as arr
arr1 = arr.array('i',[1,2,3,4,5,6])
print(arr1)
print("Index of 3 is: ", arr1.index(3))
print("Index of 4 is: ",arr1.index(4))
print("\nIndex of whole items of an array.")
for i in range(6):
print(f"Index of {arr1[i]}: ",arr1.index(arr1[i]))
Output
array('i', [1, 2, 3, 4, 5, 6])
Index of 3 is: 2
Index of 4 is: 3
Index of whole items of an array.
Index of 1: 0
Index of 2: 1
Index of 3: 2
Index of 4: 3
Index of 5: 4
Index of 6: 5