We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 22
Arrays in PythonArrays
Array is basically a data structure which can hold more than one value at a time.
It is a collection or ordered series of items at same time
Each item stored in an array is called an element.
Each location of an element in an array has a numerical index, which is used to
identify the element.
Format: array_name = array(‘type_code’ [initializer])How to create an Arrays in Python
™ Arrays in Python can be created after importing the array module
™ There are three methods to import array library
import array
Import array as arr
from array import *Accessing Array Element
™ Accessing elements using index value
™ Index starts from 0 to n-1 where n is the number of elements
™ -ive index value can be used as well, -ive indexing starts from reverse orderExample
import array as arr
a =ar.array('i', [2, 4, 6, 8])
print('First element:", a[0])
print("‘Second element:", a[1])
print(‘last element:", a[-1])
Output
First element is: 2
Second element: 4
last element: 8Basic Array Operations
™ Finding the length of an array
™ Adding/Changing element of an array
™ Removing/deleting element of an array
™ Array concatenation
™ Slicing
® Looping through an array
™ sortingFinding the length of an array
= Number of element present in the array
™ Using len() function to find the length
® The len() function returns an integer value that is equal to the number of elements
present in that arrayFinding the length of an array
= Syntax
len(array_name)
from array import *
a =array('i’, [2, 4, 6, 8])
print(len(a)) output
= ExampleAdding elements to an array
™ Using functions to add elements
™ append( is used to add a single element at the end of an array
® extend() is used to add a more than one element at the end of an array
™® insert() is used to add an element at the specific position in an arrayExample
from array import *
numbers = array('i’, [1, 2, 3])
= Output
array(‘i', [1, 2, 3, 4])
numbers.append(4)
print(numbers)
array(‘i', [1, 2, 3, 4, 5, 6, 7])
numbers.extend([5, 6, 7])
print(numbers)
array(‘i', [1, 2, 3, 8, 4, 5, 6, 7])
numbers.insert (3,8)
print(numbers)Removing elements of an array
® Functions used to removing elements of an array
® pop() is used when we want to remove an element and return it.
™ remoye() is used when we want to remove an element with a specific value
without returning it.Example
import array as arr
numbers = arr.array(‘i', [10, 11, 12, 12, 13])
numbers.remove(12)
print(numbers) # Output: array(‘i, [10, 11, 12, 13])
numbers.pop(2) # Output: 12
print(numbers)Arrays Concatenation
™ Arrays concatenation is done using + sign
= Example
import array as n
a=n.array(‘i, [10, 11, 12, 12, 13])
b =n.array(i’, [21, 22, 35, 26,39])
c=at+b
print(c) output
array(’, [10, 11, 12, 12, 13, 21, 22, 35, 26, 39]}Slicing an array
® Anarraycan be sliced using the : symbol
® This returns a range of elements that we have specified by the index number
= Example
import array as arr
numbers _list = [2, 5, 62, 5, 42, 52, 48, 5]
numbers_array = arr.array(‘’, numbers_list)
print(numbers_array[2:5]) # 3rd to Sth
print(numbers_array[:-5]) # beginning to Sth
print(numbers_array[5:]) # 6fh to end
print(numbers_array[:])_ # beginning to endLooping through an array
® For loop used to iterates over the items of an array specified number of
times
® While loop iterates the elements until a certain condition is met
= Example
from array import *
numbers_list = [2, 5, 62, 5, 42, 52, 48, ]
a = array(', numbers _list)
for xin a:
print(x)Sorting of an array
® sort the elements of the array in ascending order
= Use sorted() buit in function to sort the array
® Format: sorted(array_name)Example
from array import *
a= array(‘i,[4,3,6,2,1])
b=sorted(a)
for xin b:
print(x)
Output:
ak wowSorting of array in descending order
® Format: sorted(array_name, reverse=true)
™ Example:
from array import * output:
a= array('',[4,3,6,2,1]) 6
b=sorted(a, reverse=true)
for xin b:
print(x)
—- NWRArray Methods
# Python has a set of built-in methods that you can use on lists/arrays.
Method Description
on ‘Adds an element at the end of the lst
cleart) Removesall the elements from the list
copvl) Returns. copy of the list
until Returns the number of elements with the specified value
extend Add the elements of alist (or any iterabie), to the end of the:
current list
index) Retums the indexof the first element with the specified value
nse ‘Adds an element at the specified position
popll Removes the element at the specitied position
remove Removes the first item with the specified value
reverse) Reverses the order of the list
st Sorts the lstTYPES OF ARRAYS
® As we know that arrays are of 3 types which include
® 1D Array
@ 2D Array
® Multi D Array
In python we use
Package Numpy
We have to install it using python tools(PIP)
First of all import library that is numpy as npImport numpy
Example | D Array
™ There are three methods to import numpy
import numpy #1D Array
. Import numpy as no.
import numpy as np
Q =np.array({10,20,30,40,50])
from numpy import * print (a)
= Output
array([10,20,30,40,50))Example 2D Array
Import numpy as np
#2D Array
#3x3 Array
B=np.array([
[10,20,30,],.
[40,50,60],
[70,80,90}})
print (6)
Example 3D Array
Import numpy as np
# 3D Array
# 3x3x3
C=np.array([
[[1.2,3],[4,5.6].[7,8,9]],
{[9.8,7].[6.5,4],[3.2,1]],
[[1.2,3],[4,5,6],[7,8,9]
})
Print (c)