SlideShare a Scribd company logo
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
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
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 ]

More Related Content

PPTX
object oriented programing in python and pip
PDF
[ITP - Lecture 15] Arrays & its Types
PPTX
Python array
PDF
Python programming : Arrays
PDF
Unit-5-Part1 Array in Python programming.pdf
PDF
Class notes(week 4) on arrays and strings
PPTX
ARRAY OPERATIONS.pptx
DOC
Array properties
object oriented programing in python and pip
[ITP - Lecture 15] Arrays & its Types
Python array
Python programming : Arrays
Unit-5-Part1 Array in Python programming.pdf
Class notes(week 4) on arrays and strings
ARRAY OPERATIONS.pptx
Array properties

Similar to Array-single dimensional array concept .pptx (20)

PPSX
C Programming : Arrays
DOCX
Class notes(week 4) on arrays and strings
PPTX
Unit 2-Arrays.pptx
PPT
Arrays Basicfundamentaldatastructure.ppt
PPTX
Arrays in programming
PDF
Arrays in python
PPTX
Numpy in python, Array operations using numpy and so on
PPTX
arrays in c programming - example programs
PPTX
Data structures and algorithms arrays
PPTX
numpydocococ34554367827839271966666.pptx
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPTX
Array In C++ programming object oriented programming
PPTX
NUMPY [Autosaved] .pptx
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPT
C array and Initialisation and declaration
PPT
show the arrays ppt for first year students
PDF
Java arrays (1)
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
PDF
Arrays in java
C Programming : Arrays
Class notes(week 4) on arrays and strings
Unit 2-Arrays.pptx
Arrays Basicfundamentaldatastructure.ppt
Arrays in programming
Arrays in python
Numpy in python, Array operations using numpy and so on
arrays in c programming - example programs
Data structures and algorithms arrays
numpydocococ34554367827839271966666.pptx
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Array In C++ programming object oriented programming
NUMPY [Autosaved] .pptx
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Homework Assignment – Array Technical DocumentWrite a technical .pdf
C array and Initialisation and declaration
show the arrays ppt for first year students
Java arrays (1)
Unit4pptx__2024_11_ 11_10_16_09.pptx
Arrays in java
Ad

More from SindhuVelmukull (15)

PPTX
Python Dictionary concept -R.Chinthamani .pptx
PPTX
Input statement- output statement concept.pptx
PPTX
Web input forms.pptx
PPTX
Web protocol.pptx
PPTX
Web technology.pptx
PPTX
YIQ by R.Chinthamani.pptx
PPTX
PROPERTIES OF LIGHT by R.Chinthamani.pptx
PPTX
Overview of CG by R.Chinthamani.pptx
PPTX
Bundled Attributes by R.Chinthamani.pptx
PPTX
RGB Color.pptx
PPTX
Projection.pptx
PPTX
PPTX
Introduction to artificial intelligence
PPTX
COMPUTER NETWORK
Python Dictionary concept -R.Chinthamani .pptx
Input statement- output statement concept.pptx
Web input forms.pptx
Web protocol.pptx
Web technology.pptx
YIQ by R.Chinthamani.pptx
PROPERTIES OF LIGHT by R.Chinthamani.pptx
Overview of CG by R.Chinthamani.pptx
Bundled Attributes by R.Chinthamani.pptx
RGB Color.pptx
Projection.pptx
Introduction to artificial intelligence
COMPUTER NETWORK
Ad

Recently uploaded (20)

PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
English Language Teaching from Post-.pdf
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Congenital Hypothyroidism pptx
PPTX
How to Manage Bill Control Policy in Odoo 18
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
Odoo 18 Sales_ Managing Quotation Validity
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Landforms and landscapes data surprise preview
PPTX
ACUTE NASOPHARYNGITIS. pptx
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
PDF
Types of Literary Text: Poetry and Prose
PDF
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Revamp in MTO Odoo 18 Inventory - Odoo Slides
Renaissance Architecture: A Journey from Faith to Humanism
Onica Farming 24rsclub profitable farm business
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
English Language Teaching from Post-.pdf
Software Engineering BSC DS UNIT 1 .pptx
Cardiovascular Pharmacology for pharmacy students.pptx
Congenital Hypothyroidism pptx
How to Manage Bill Control Policy in Odoo 18
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
How to Manage Starshipit in Odoo 18 - Odoo Slides
Odoo 18 Sales_ Managing Quotation Validity
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Landforms and landscapes data surprise preview
ACUTE NASOPHARYNGITIS. pptx
vedic maths in python:unleasing ancient wisdom with modern code
Types of Literary Text: Poetry and Prose
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
Open Quiz Monsoon Mind Game Prelims.pptx

Array-single dimensional array concept .pptx

  • 1. By R.Chinthamani, Assistant Professor, Department of Computer Science, E.M.G.Yadava Women’s College, Madurai-14 PYTHON ASSIGNMENT
  • 2. 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.
  • 3. 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.
  • 4. • 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:
  • 5. 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
  • 6. 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.
  • 7. 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’.
  • 9. 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= ‘ ‘)
  • 10. 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]
  • 11. 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
  • 12. 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:
  • 13. 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.
  • 14. 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
  • 15. 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]
  • 16. 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”.
  • 17. 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.
  • 18. 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)
  • 19. 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]
  • 20. 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
  • 21. a b Creating view of an Array 2 1 3 4 5 1 3 4 5 2
  • 22. 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
  • 23. 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 ]