Arrays Concept in Python With Examples

By
Python Arrays 
  • An array is a collection of elements that are stored in contiguous memory locations.
  • An array holds fixed number of elements of the same data types.
  • An array can store multiple elements of the same data types.
  • An array is used to store multiple values in single variable.
e.g.
arr_val1="horse"
arr_val2="lion"
arr_val3="man"
  • An array in python can be handled a module named array.
e.g.
import array as aar

Representation of array:-
An array can be declared in different ways and different languages.Here We are going to declared the array in python language. 
  • Python array starts with 0 index.
  • Python array's elements can be accessed via its index.
  • The length of an array = total number of index+1.

e.g.
import array as arr
numbers=arr.array('u',['w','e','l','c','o','m','e'])
print("The length of an array is: ",len(numbers))
print(numbers[0],numbers[1],numbers[2],numbers[3],numbers[4],numbers[5],numbers[6])
print(numbers[-1],numbers[-2],numbers[-3],numbers[-4],numbers[-5],numbers[-6],numbers[-7])
Output:-
The length of an array is:  7
w e l c o m e
e m o c l e w
>>> 
Creating python Arrays:-
  • To create an array in python ,we have to import an array module first.
  • Insert some values of same data types and print the array elements in python.
e.g.
import array as arr
items=arr.array('i',[1,2,3,4,5,6,7,8])
print("The array elements are : ",items)
Output:-
The array elements are :  array('i', [1, 2, 3, 4, 5, 6, 7, 8])
>>>  

Descriptions:-
We have created an array of int data type.The letter ' i 'is a type code.

There are some python type codes as shown below:-
Note:-Here B,H,,I and L are used as a unsigned integer in c language.b,h,i and l are used as signed integer in c language.
Array operations in python:-
  1. Insertion:-It is used to add an element in array.
  2. Deletion:-It is used to delete an element from the array.
  3. Update:- It is used to update an array elements.
  4. Search:- It is used to search an element in given array.
  5. Traverse:-It is used to print the array elements one by one.
Accessing the python array elements:-
We can access the python array elements using the indices as given below:-
e.g.
import array as arr
items=arr.array('I',[1,2,3,4,5,6,7,8])
print("First element of array :",items[0])
print("Second element of array :",items[1])
print("Third element of array :",items[2])
print("Fourth element of array :",items[3])
print("Fifth element of array :",items[4])
print("Sixth element of array :",items[5])
print("Seventh element of array :",items[6])
print("Eighth element of array :",items[7])
Output:-
First element of array : 1
Second element of array : 2
Third element of array : 3
Fourth element of array : 4
Fifth element of array : 5
Sixth element of array : 6
Seventh element of array : 7
Eighth element of array : 8
>>> 
Slicing the Python Array:-
We can use slicing operator : in python array to access the range of items in an array.
e.g.
import array as arr
num=[10,20,30,40,50,60]
num_array=arr.array('i',num)
print(num_array[1: 4])
print(num_array[ :5 ])
print(num_array[ :-4 ])
print(num_array[ :])
Output:-
array('i', [20, 30, 40])
array('i', [10, 20, 30, 40, 50])
array('i', [10, 20])
array('i', [10, 20, 30, 40, 50, 60])
>>> 
Changing & Adding the elements in array
Arrays are mutable and their elements can be modified /changed in a similar way as lists.
e.g.
import array as arr
numbers=arr.array('i',[10,20,30,40,50,60])
print("Arrays elements are:",numbers)
numbers[0]=70
numbers[1]=90
print("Arrays elements after modified:",numbers)
numbers[2:5]=arr.array('i',[100,110,120])
print("Arrays elements after modified using slicing operators:",numbers)
Output:-
Arrays elements are: array('i', [10, 20, 30, 40, 50, 60])
Arrays elements after modified: array('i', [70, 90, 30, 40, 50, 60])
Arrays elements after modified using slicing operators: array('i', [70, 90, 100, 110, 120, 60])
>>> 
Deleting the elements from an array
We can easily delete one by one element from an array using 'del' statements.
e.g.
import array as arr
number=arr.array('i',[10,20,30,40,50,60,70,80])
print("Arrays elements are:",number)
#delete the 2nd element
del number[1]
print("Arrays elements are:",number)
#delete the 4th element
del number[3]
print("Arrays elements are:",number)
#delete the entire array
del number
print("Arrays elements are:",number)
Output:-
Arrays elements are: array('i', [10, 20, 30, 40, 50, 60, 70, 80])
Arrays elements are: array('i', [10, 30, 40, 50, 60, 70, 80])
Arrays elements are: array('i', [10, 30, 40, 60, 70, 80])
Traceback (most recent call last):
  File "C:/Users/RAMASHANKER VERMA/Desktop/arrays.py", line 12, in <module>
    print("Arrays elements are:",number)
NameError: name 'number' is not defined
>>> 
Length of an Array:-
The length of an array is defined as the number of items present in an array.We can calculate the length of an array using 'len' keyword.
Syntax:-
len(array_name)
e.g.
import array as arr
number=arr.array('i',[10,20,30,40,50,60,70,80])
print("Arrays elements are:",number)
print("The Length of an array is:",len(number))
Output:
Arrays elements are: array('i', [10, 20, 30, 40, 50, 60, 70, 80])
The Length of an array is: 8
>>> 
How to use remove() and pop() methods in arrays:-
We can use remove() and pop() methods to delete the items at the given index.
e.g.
import array as arr
items=arr.array('i',[2,4,6,8,10,12,14,16])
print("Arrays elements are:",items)
items.remove(10)
print("After remove 10 item arrays elements are:",items)
items.pop(3)
print("After pop(3),arrays elements are:",items)
Output:-
Arrays elements are: array('i', [2, 4, 6, 8, 10, 12, 14, 16])
After remove 10 item arrays elements are: array('i', [2, 4, 6, 8, 12, 14, 16])
After pop(3),arrays elements are: array('i', [2, 4, 6, 12, 14, 16])
>>> 
Concatenation of Array
We can concatenate any two or more arrays with sane data type using the + symbol.
e.g.
import array as arr
a=arr.array('i',[2,4,6,8])
b=arr.array('i',[1,3,5,7])
c=arr.array('i',[10,20,30,40])
d=a+b+c
print("Array of d = ",d)
Output:-
Array of d =  array('i', [2, 4, 6, 8, 1, 3, 5, 7, 10, 20, 30, 40])
>>> 
Python Arrays and Lists
In python, we can treat lists as arrays. But arrays hold the only similar data type however lists hold the similar as well as dissimilar data type both.
e.g.
import array as arr
a=arr.array('i',[2,4,6,8])
print("Arrays elements are:",a)
#lists elements
li=[1,2,3,4,'Hello',5,6,7,8,'ram']
print("Lists elements are:",li)
#pass the lists value in array
b=arr.array('i',li)
#It shows errors because of dissimilar items
print("Arrays elements are:",b)
Output:-
Arrays elements are: array('i', [2, 4, 6, 8])
Lists elements are: [1, 2, 3, 4, 'Hello', 5, 6, 7, 8, 'ram']
Traceback (most recent call last):
  File "C:\Users\RAMASHANKER VERMA\Desktop\arrays.py", line 8, in <module>
    b=arr.array('i',li)
TypeError: an integer is required (got type str)
>>> 

Watch Complete Lecture video


For more..

0 comments:

Post a comment

Powered by Blogger.