Module-10-INTEGRATIVE PROGRAMMING 2
Module-10-INTEGRATIVE PROGRAMMING 2
Tuguegarao City
Course Code :
Course Title : INTEGRATIVE PROGRAMMING 2
MODULE No. 10
TITLE: PYTHON ARRAY
INTRODUCTION
LEARNING 1.
OUTCOMES
LEARNING 1.
OBJECTIVES
Introduction:
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of
the same type together. This makes it easier to calculate the position of each element by simply adding an
offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the
name of the array).
Discussions:
For simplicity, we can think of an array a fleet of stairs where on each step is placed a value (let’s
say one of your friends). Here, you can identify the location of any of your friends by simply
knowing the count of the step they are on. Array can be handled in Python by a modular
named array. They can be useful when we have to manipulate only a specific data type values. A
user can treat lists as arrays. However, user cannot constraint the type of elements stored in a list.
If you create
arrays using the array module, all elements of the array must be of the same type.
Creating an Array
Array in Python can be created by importing array module. array(data_type, value_list ) is used to
create an array with data type and value list specified in its arguments.
Int array[10]={35,33,42,10,14,19,27,44,26,31}
0 1 2 3 4 5 6 7 8 9
Page 1
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
Output :
The new created array is : 1 2 3
The new created array is : 2.5 3.2 3.3
some of the data types are mentioned below which will help in creating an array of different data types.
Adding
Elements to
an Array
Elements can
be added to
the Array by
using built-
in insert() fu
n ction. Insert
is used to
insert one or
more data
elements into
an array.
Based on the
requirement,
a new
element can
be added at
the
beginning,
end, or any
given index of array. append() is also used to add the value mentioned in its arguments at the end of the
array. Extend() method add several items in your array list
[1,4, 2,3,] 4, 5,6,7
Page 2
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
Output :
Array before insertion : 1 2 3
Array after insertion : 1 4 2 3
Array before insertion : 2.5 3.2 3.3
Array after insertion : 2.5 3.2 3.3 4.4
Page 3
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
Output :
Access element is: 1
Access element is: 4
Access element is: 3.2
Access element is: 3.3
Page 4
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
# printing array after popping
print ("The array after popping is : ", end ="")
for i in range (0, 4):
print (arr[i], end =" ")
print("\r")
# using remove() to remove 1st occurrence of 1
arr.remove(1)
# printing array after removing
print ("The array after removing is : ", end ="")
for i in range (0, 3):
print (arr[i], end =" ")
Output:
The new created array is : 1 2 3 1 5
The popped element is : 3
The array after popping is : 1 2 1 5
The array after removing is : 2 1 5
Slicing of an Array
In Python array, there are multiple ways to print the whole array with all the elements, but to print a specific
range of elements from the array, we use Slice operation. Slice operation is performed on array with the use
of colon(:). To print elements from beginning to a range use [:Index], to print elements from end use [:-
Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use
[Start Index:End Index] and to print whole List with the use of slicing operation, use [:]. Further, to print
whole array in reverse order, use [::-1].
Page 5
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
Output :
Intial Array:
1 2 3 4 5 6 7 8 9 10
Page 6
UNIVERSITY OF CAGAYAN VALLEY
Tuguegarao City
print (arr.index(2))
# using index() to print index of 1st occurrenece of 1
print ("The index of 1st occurrence of 1 is : ", end ="")
print (arr.index(1))
Output:
The new created array is : 1 2 3 1 2 5
The index of 1st occurrence of 2 is : 1
The index of 1st occurrence of 1 is : 0
Output:
Array before updation : 1 2 3 1 2 5
Array after updation : 1 2 6 1 2 5
Array after updation : 1 2 6 1 8 5
Page 7