Array-single dimensional array concept .pptx

SindhuVelmukull 27 views 23 slides Sep 13, 2024
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

Array-single dimensional array
Multi dimensional array


Slide Content

By R.Chinthamani , Assistant Professor, Department of Computer Science, E.M.G.Yadava Women’s College, Madurai-14 PYTHON ASSIGNMENT

ARRAY: An array is an object that stores a group of elements(or values) of some data type.The main advantages of any array is to store and process a group of element easily. Array can be store only one type of element.it mean we can store only one integer type elements or only one float type elements in to an array.

Arrays can increase or decrease their size dynamically.it means ,we need to declare the size of the array. ADVANTAGES OF ARRAYS: Arrays are similar to lists.The main difference is that arrays can store only one type of elements ,where as,lists can store different types of element array can use less memory than lists and they offer faster exection than files. The size array is not fixed in python.Hence ,we need not specify how many elements we are going to store into an array in the beginning.

Arrays can grow or shrink to memory dynamically (during runtime). Arrays are useful to handle a collection of elements like a group of number or characters. CREATING AN ARRAY: The type should be specified by using a type code at the time of creating array. arrayname = array(type code [element]) The type code ‘i’ represents interger type code array,we can store interger number is ‘f’ it represents floating type code array,where we can store number in decimal point. The important type codes are:

Typecode C Type Minimum size in byte ‘I’ signed integer 1 ‘L’ unsigned integer 4 ‘f’ floating point 4 ‘d’ double precision floating point 8 ‘u’ unicode character 2

IMPORTING THE ARRAY MODULE: There are three ways to import array module into our program. import array when we import the array module,we are able get the ‘array’ class of that module that help us to create an array. import array as ar Here the array is imported with an alternate name ‘ar’ hence we can refer array class as ‘ar’. from array import * ‘*’ symbol that represents ‘all’ The meaning of this statement is this import all the (class,object,variables etc) from the array module in our program.

A PYTHON PROGRAM TO CREATE AN INTEGER TYPE ARRAY: CODING: # creating an array import array a = array.array(‘i’, [5,6,-7,8]) print(“The array element are:”) for element in a: print(element) OUTPUT: 5 6 -7 8

INDEXING AND SLICING ON ARRAYS: Indexing: An index represents the position number of an element in an array. x = array (‘i’,[10,20,30,50]) To find out the number of element in an array we can use the len() function as: n = len(x) The len() function returns the number of elements in the array’x’ into ‘n’.

A PYTHON PROGRAM TO RETRIVE THE ELEMENTS OF AN ARRAY USING INDEX: CODING: # accessing elements of an array using index from array import* x = array(‘i’, [10,20,30,40,50]) # find number of element in the array n = len(x) # display array elements using indexing OUTPUT: for i in range(n): 10 20 30 40 50 print(x[i], end= ‘ ‘)

slicing: A slice represents a piece of the array .when we perform a ‘slicing’ operations on any array , we can retrive a piece of array that contains a group of element. The general format of slice is: arrayname[start:stop:stride] we can eliminate any one or any two in the items: ‘start’ ‘stop’,and ‘stride’ from the above syntax. arr[1:4]

A PYTHON PROGRAM TO RETRIVE AND DISPLAY ONLY A RANGE ELEMENTS FROM AN ARRAY USING SLICING: CODING: # using slicing to display elements of an array from array import * x = array (‘i’, [10,20,30,40,50,60.70]) # display element from 2nd to 4th only OUTPUT: for i in x[2:5]: 30 print(i) 40 50 60

TYPES OF ARRAY: single dimenstional array: These arrays represent only one row or one column of elements.for example, marks obtained by an student in 5 subjects can be written as ‘mark’ array as: marks = array(‘i’,[50,60,70,66,72]) multi-dimenstional arrays: These arrays represent more than one row and more than one column of elements.for example marks obtained for 3 student each one in 5 subjects can be written as ‘mark’ array as:

marks = array ([[50, 60, 70, 66, 72], [60, 62, 71, 56, 70], [55, 59, 80, 68, 65]]) The first students marks are written in first row. the second student marks are in second row and the third students marks are in third row. in each row,the marks of 5 subject is menstioned. Each row of the above array can be again represented as a single dimensional array .Thus the above array contains 3 single dimenstional arrays.Hence, it is called a two dimenstional array.

WORKING WITH ARRAY USING NUMPY: numpy is a package that contains several classes,functions variables etc. to deal with scientific calculations in python.numpy is useful to create and also process single and multi-dimenstional arrays. The array which are created using numpy are called n dimensional arrays where n can be any integer. if n =1,it represents a one dimensional array, we cannot store different datatype into same array To work numpy,we should first import numpy module in our python program import numpy This will import numpy module into our program so that we can use any of the objects from that package,But to refer to an object we should use the format: numpy.object

A PYTHON PROGRAM TO CREATE A SINGLE ARRAY USING NUMPY: CODING: # creating single dimensional array using numpy import numpy arr = numpy.array([10,20,30,40,50]) # create array print(arr) # display array OUTPUT: [10, 20, 30, 40, 50]

ALIASING THE ARRAYS: If ‘a’ is an array, we can assign it to ‘b’ as: b = a This is simple assignment that does not make any new copy of the array ‘a’. It means , ‘b’ is not new array and memory is not allocated to ‘b’.Also ,elements from ‘a’ are not copied in ‘b’. we should understand that we giving a new name to ‘b’ to the same array referred by ‘a’.It means the names ‘a’ and ‘b’ are referrencing same array.This is called “aliasing”.

a b Aliasing the array a as b 1 2 3 4 5 ‘Aliasing’ is to not ‘copying’. Aliasing means giving another name to the exitisting object Hence , any modification to the alias object will reflect in the existing object and vice versa.

A python program to alias an array and understand the affect of aliasing: Coding: # aliasing an array. from numpy import* a = arange(1, 6) # create a with elements 1 to 5. b = a #give another name b to a print(‘Original array:’, a) print(‘Alisa array:’, b) b[0]=99 # modify 0th element of b print(‘After modification:’) print(‘Original array:’, a) print(‘Alisa array:’, b)

Output: Original array:[1 2 3 4 5] Alisa array:[1 2 3 4 5 ] After modification: Original array:[99 2 3 4 5] Alisa array:[99 2 3 4 5]

Viewing And Copying Array: we can create another array that is same as an existing array. This is done by the view() method.This method creates a copy of an existing array such that the new array with also contain the same elements found in the existing array. The original array and the newlycreated arrays will share different memory location. if the newly created arrays is modified the original array will also be modified since elements in both the arrays will be like mirror images

a b Creating view of an Array 2 1 3 4 5 1 3 4 5 2

A python program to create a view of an array : coding: # creating view for an array from numpy import * a = array(1,6) #create a with elements 1 to 5 b = a.view() #create a view of a and call it b print(‘original array:’, a) print(‘new array :’, b) b[0] = 99 #modify 0th element of b

print(‘after modification :’) print(‘original array :’, a) print(‘new array:’,b) Output: Original array:[1 2 3 4 5] new array : [1 2 3 4 5 ]
Tags