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

PDF
Arrays in python
PDF
Unit-5-Part1 Array in Python programming.pdf
PPTX
ARRAY OPERATIONS.pptx
PDF
Python programming : Arrays
PPTX
Week 11.pptx
PPTX
object oriented programing in python and pip
PPTX
Python array
PPTX
ACP-arrays.pptx
Arrays in python
Unit-5-Part1 Array in Python programming.pdf
ARRAY OPERATIONS.pptx
Python programming : Arrays
Week 11.pptx
object oriented programing in python and pip
Python array
ACP-arrays.pptx

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

PDF
PDF
Numpy.pdf
PPT
Basic Introduction to Python Programming
PPTX
numpydocococ34554367827839271966666.pptx
PPTX
CC-104_Lesson-2_array.pptx. introduction
PDF
Arrays in python
PPTX
NUMPY-2.pptx
PPTX
Lecture 2 _Foundions foundions NumPyI.pptx
PDF
‏‏Lecture 2.pdf
PPTX
Python for Beginners
PPTX
Brixon Library Technology Initiative
PPTX
L-30-35huujjjhgjnnjhggbjkiuuhhjkiiijj.pptx
PPTX
numpy code and examples with attributes.pptx
PPTX
NUMPY [Autosaved] .pptx
PPTX
Unit 1 array based implementation
PPTX
Usage of Python NumPy, 1Dim, 2Dim Arrays
PPT
CAP776Numpy (2).ppt
PPT
CAP776Numpy.ppt
PPT
2025pylab engineering 2025pylab engineering
PPTX
Data Analyzing And Visualization Using Python.pptx
Numpy.pdf
Basic Introduction to Python Programming
numpydocococ34554367827839271966666.pptx
CC-104_Lesson-2_array.pptx. introduction
Arrays in python
NUMPY-2.pptx
Lecture 2 _Foundions foundions NumPyI.pptx
‏‏Lecture 2.pdf
Python for Beginners
Brixon Library Technology Initiative
L-30-35huujjjhgjnnjhggbjkiuuhhjkiiijj.pptx
numpy code and examples with attributes.pptx
NUMPY [Autosaved] .pptx
Unit 1 array based implementation
Usage of Python NumPy, 1Dim, 2Dim Arrays
CAP776Numpy (2).ppt
CAP776Numpy.ppt
2025pylab engineering 2025pylab engineering
Data Analyzing And Visualization Using Python.pptx
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)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PPTX
Onica Farming 24rsclub profitable farm business
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Sunset Boulevard Student Revision Booklet
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
From loneliness to social connection charting
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
Landforms and landscapes data surprise preview
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
102 student loan defaulters named and shamed – Is someone you know on the list?
Module 3: Health Systems Tutorial Slides S2 2025
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
Onica Farming 24rsclub profitable farm business
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
Sunset Boulevard Student Revision Booklet
Software Engineering BSC DS UNIT 1 .pptx
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
Week 4 Term 3 Study Techniques revisited.pptx
NOI Hackathon - Summer Edition - GreenThumber.pptx
UPPER GASTRO INTESTINAL DISORDER.docx
Renaissance Architecture: A Journey from Faith to Humanism
From loneliness to social connection charting
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Landforms and landscapes data surprise preview
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table

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 ]