SlideShare a Scribd company logo
3
Most read
13
Most read
17
Most read
Python Array
What Is Array?
• An array is an mutable object which store a collection of values of
same data type under a single reference.
• An array store only homogeneous data type.
• Python's array is dynamic array.
• In Python we need declaration of array before use it in program.
• 'array' module must be imported for handling array in Python.
Advantages Of Array
I. Performance: For huge number of elements array take less memory
spaces and execution time also faster if compare with list.
II. Dynamic: Array's size is not fixed, so we can increase or decrease its
size at run time.
III. array module: Python provide 'array' module which contain
different methods, classes, objects to process array very easily.
Array vs. List
Topic Array List
Type of data structure
It is not built - in. We need
import 'array' module
before using array.
It is built - in. There are no
need to import any extra
module or package.
Necessity of declaration
Requires specific function
to declare an array.
No requirement of specific
function to declare a list.
Data type
Support only
homogeneous type.
Always support different
types of data in a list.
Array vs. List
Topic Array List
Handling large data
Store data very compact
way, so array more
efficient to handle large
data.
It is not so much efficient
to handle large data.
Numerical operations
Arryas are efficient for
various types of numerical
operations.
Lists can not handle
numerical operations
directly.
'array' Module
• If you working with array you need to import 'array' module.
• According to Python documentation ā€œThis module defines an object
type which can compactly represent an array of basic values:
characters, integers, floating point numbers.ā€
• Import array module as following way:
>>> import array
Declaring Array
• The syntax of array declaration is:
• 'array_name' is an identifier, which store reference of array object.
• 'array.array( )' first array represent 'array' module and second array
represent array class which resides in 'array' module.
• 'type_code' represent the data type of array's elements.
• '[ elements ]' represent list of values which we want to store in array.
array_name = array.array( type_code, [ elements ] )
List Of 'type_code'
type_code Data type Minimum size in
bytes
'b' Signed integer 1
'B' Unsigned integer 1
'i' Signed integer 2
'I' Unsigned integer 2
'l' Signed integer 4
'L' Unsigned integer 4
List Of 'type_code'
type_code Data type Minimum size in
bytes
'f' Floating point 4
'd' Double precision
floating point
8
'u' Unicode character 2
Array Declaration Example
>>> import array
>>> my_array = array.array( 'i', [ 5, 7, 11, 14 ] )
>>> for i in my_array:
print( i )
5
7
11
14
Programming Example
• A program to create an array to store the characters of word
'Welcome' and display all characters.
>>> import array
>>> my_array = array.array( 'u', [ 'W', 'e', 'l', 'c', 'o', 'm', 'e' ] )
>>> for i in my_array:
print( i )
W m
e e
l
c
o
Different Ways Of Importing Array Module
• Import array module with an alias or alternate name as below:
• Here 'arr' is an alias name of imported module.
• For example:
>>> import array as arr
>>> my_array = arr.array( 'i', [ 5, 7, 11, 14 ] )
>>> import array as arr
Different Ways Of Importing Array Module
• Second way of importing 'array' module is below:
• Here '*' means all. That means we import all things such as classes,
objects, variables etc. of array module in our program.
• For example:
>>> from array import *
>>> my_array = array( 'i', [ 5, 7, 11, 14 ] )
>>> from array import *
Indexing On Arrays
• An index represent position value of an element in an array.
• Array index always start from 0.
• For example:
>>> from array import *
>>> x = array( 'i', [ 5, 7, 11, 14 ] )
>>> print( x[ 2 ] )
11
5 7 11 14
x[ 0 ] x[ 1 ] x[ 2 ] x[ 3 ]
Accessing Array Using Index
- By for loop
from array import *
a = array( 'i', [ 34, 56,-12, 44 ] )
n = len( a )
for i in range( n ):
print( a[ i ], 't', end = ' ' )
34 56 -12 44
Accessing Array Using Index
- By while loop
from array import *
a = array( 'i', [ 34, 56,-12, 44 ] )
n = len( a )
i = 0
while i < n:
print( a[ i ], 't', end = ' ' )
i += 1
34 56 -12 44
Slicing On Array
• By slicing operation we can retrive a piece of array out of an array.
• The syntax of slicing is:
• For example:
array_name[ start: stop: stride ]
>>> x = array( 'i', [ 10, 20, 30, 40, 50, 60, 70, 80, 90 ] )
>>> print( x[ 1: 4 ] )
array('i', [20, 30, 40])
>>> print( x[ 1: : 2 ] )
array('i', [20, 40, 60, 80])
Slicing On Array
array('i', [20, 40, 60, 80])
>>> print( x[ : 8: 2 ] )
array('i', [10, 30, 50, 70])
>>> print( x[ -1: : ] )
array('i', [90])
>>> print( x[ : : -1 ] )
array('i', [90, 80, 70, 60, 50, 40, 30, 20, 10])
>>> print( x[ : -5: -1 ] )
array('i', [90, 80, 70, 60])
>>> print( x[ -5: : ] )
array('i', [50, 60, 70, 80, 90])
Programming Example
• A program to calculate sum of a range of elements in an array.
from array import *
x = array( 'i', [ 10, 20, 30, 40, 50, 60, 70, 80, 90 ] )
sum = 0
for i in x[ 2: 5 ]:
sum = sum + i
print( sum )

More Related Content

PDF
Arrays in python
moazamali28
Ā 
PPT
Lecture 1_System Integration & Architecture
CAPINPINSerelyn
Ā 
PPTX
email writing ppt
aishvaryatamilarasoo
Ā 
PPTX
Advantages & Disadvantages of Email
Anirudh Kannan
Ā 
PPTX
C++ Overview PPT
Thooyavan Venkatachalam
Ā 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
Ā 
PDF
Introduction to c++ ppt
Prof. Dr. K. Adisesha
Ā 
PPTX
Flexible pavement presentation
Pawan Kumar
Ā 
Arrays in python
moazamali28
Ā 
Lecture 1_System Integration & Architecture
CAPINPINSerelyn
Ā 
email writing ppt
aishvaryatamilarasoo
Ā 
Advantages & Disadvantages of Email
Anirudh Kannan
Ā 
C++ Overview PPT
Thooyavan Venkatachalam
Ā 
Arrays In Python | Python Array Operations | Edureka
Edureka!
Ā 
Introduction to c++ ppt
Prof. Dr. K. Adisesha
Ā 
Flexible pavement presentation
Pawan Kumar
Ā 

What's hot (20)

PDF
Set methods in python
deepalishinkar1
Ā 
PDF
Python list
Mohammed Sikander
Ā 
PPTX
Datastructures in python
hydpy
Ā 
PPTX
Regular expressions in Python
Sujith Kumar
Ā 
PPTX
File handling in Python
Megha V
Ā 
PPTX
Linked list
akshat360
Ā 
PPTX
Chapter 15 Lists
Praveen M Jigajinni
Ā 
PDF
Tuples in Python
DPS Ranipur Haridwar UK
Ā 
PPTX
Arrays
Trupti Agrawal
Ā 
PPTX
Python Data Structures and Algorithms.pptx
ShreyasLawand
Ā 
ODP
Python Modules
Nitin Reddy Katkam
Ā 
PDF
Python tuple
Mohammed Sikander
Ā 
PPTX
Python list
ArchanaBhumkar
Ā 
PPT
Python Dictionaries and Sets
Nicole Ryan
Ā 
PDF
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
Ā 
PDF
Functions and modules in python
Karin Lagesen
Ā 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
Ā 
PDF
Array data structure
maamir farooq
Ā 
PPTX
stack & queue
manju rani
Ā 
Set methods in python
deepalishinkar1
Ā 
Python list
Mohammed Sikander
Ā 
Datastructures in python
hydpy
Ā 
Regular expressions in Python
Sujith Kumar
Ā 
File handling in Python
Megha V
Ā 
Linked list
akshat360
Ā 
Chapter 15 Lists
Praveen M Jigajinni
Ā 
Tuples in Python
DPS Ranipur Haridwar UK
Ā 
Arrays
Trupti Agrawal
Ā 
Python Data Structures and Algorithms.pptx
ShreyasLawand
Ā 
Python Modules
Nitin Reddy Katkam
Ā 
Python tuple
Mohammed Sikander
Ā 
Python list
ArchanaBhumkar
Ā 
Python Dictionaries and Sets
Nicole Ryan
Ā 
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
Ā 
Functions and modules in python
Karin Lagesen
Ā 
Chapter 05 classes and objects
Praveen M Jigajinni
Ā 
Array data structure
maamir farooq
Ā 
stack & queue
manju rani
Ā 
Ad

Similar to Python array (20)

PPTX
object oriented programing in python and pip
LakshmiMarineni
Ā 
PDF
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
Ā 
PPTX
Array-single dimensional array concept .pptx
SindhuVelmukull
Ā 
PPTX
numpydocococ34554367827839271966666.pptx
sankarhariharan2007
Ā 
DOCX
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
Ā 
PPTX
Yash Bhargava Array In programming in C
simranroy370
Ā 
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
GImpact
Ā 
PPT
Array
PRN USM
Ā 
PPTX
arrays in c# including Classes handling arrays
JayanthiM19
Ā 
PPTX
ARRAYS.pptx
MamataAnilgod
Ā 
PPT
C array and Initialisation and declaration
BalaKrishnan466
Ā 
PPT
show the arrays ppt for first year students
anilkumaralaparthi6
Ā 
PPTX
Arrays in C++
Kashif Nawab
Ā 
PPTX
arrays in c programming - example programs
stalin721831
Ā 
PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
Ā 
PPTX
ARRAY OPERATIONS.pptx
DarellMuchoko
Ā 
PDF
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
Ā 
PDF
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
Ā 
PDF
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
kamalabhushanamnokki
Ā 
PPTX
Arrays in Java with example and types of array.pptx
ashwinibhosale27
Ā 
object oriented programing in python and pip
LakshmiMarineni
Ā 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
Ā 
Array-single dimensional array concept .pptx
SindhuVelmukull
Ā 
numpydocococ34554367827839271966666.pptx
sankarhariharan2007
Ā 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
Ā 
Yash Bhargava Array In programming in C
simranroy370
Ā 
Unit4pptx__2024_11_ 11_10_16_09.pptx
GImpact
Ā 
Array
PRN USM
Ā 
arrays in c# including Classes handling arrays
JayanthiM19
Ā 
ARRAYS.pptx
MamataAnilgod
Ā 
C array and Initialisation and declaration
BalaKrishnan466
Ā 
show the arrays ppt for first year students
anilkumaralaparthi6
Ā 
Arrays in C++
Kashif Nawab
Ā 
arrays in c programming - example programs
stalin721831
Ā 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
Ā 
ARRAY OPERATIONS.pptx
DarellMuchoko
Ā 
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
Ā 
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
Ā 
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
kamalabhushanamnokki
Ā 
Arrays in Java with example and types of array.pptx
ashwinibhosale27
Ā 
Ad

Recently uploaded (20)

PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
Ā 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
Ā 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
Ā 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
Ā 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
Ā 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
Ā 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
Ā 
PDF
Software Development Methodologies in 2025
KodekX
Ā 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
Ā 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
Ā 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
Ā 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
Ā 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
Ā 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
Ā 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
Ā 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
Ā 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
Ā 
PDF
This slide provides an overview Technology
mineshkharadi333
Ā 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
Ā 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
Ā 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
Ā 
cloud computing vai.pptx for the project
vaibhavdobariyal79
Ā 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
Ā 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
Ā 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
Ā 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
Ā 
Software Development Methodologies in 2025
KodekX
Ā 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
Ā 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
Ā 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
Ā 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
Ā 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
Ā 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
Ā 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
Ā 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
Ā 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
Ā 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
Ā 
This slide provides an overview Technology
mineshkharadi333
Ā 

Python array

  • 2. What Is Array? • An array is an mutable object which store a collection of values of same data type under a single reference. • An array store only homogeneous data type. • Python's array is dynamic array. • In Python we need declaration of array before use it in program. • 'array' module must be imported for handling array in Python.
  • 3. Advantages Of Array I. Performance: For huge number of elements array take less memory spaces and execution time also faster if compare with list. II. Dynamic: Array's size is not fixed, so we can increase or decrease its size at run time. III. array module: Python provide 'array' module which contain different methods, classes, objects to process array very easily.
  • 4. Array vs. List Topic Array List Type of data structure It is not built - in. We need import 'array' module before using array. It is built - in. There are no need to import any extra module or package. Necessity of declaration Requires specific function to declare an array. No requirement of specific function to declare a list. Data type Support only homogeneous type. Always support different types of data in a list.
  • 5. Array vs. List Topic Array List Handling large data Store data very compact way, so array more efficient to handle large data. It is not so much efficient to handle large data. Numerical operations Arryas are efficient for various types of numerical operations. Lists can not handle numerical operations directly.
  • 6. 'array' Module • If you working with array you need to import 'array' module. • According to Python documentation ā€œThis module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers.ā€ • Import array module as following way: >>> import array
  • 7. Declaring Array • The syntax of array declaration is: • 'array_name' is an identifier, which store reference of array object. • 'array.array( )' first array represent 'array' module and second array represent array class which resides in 'array' module. • 'type_code' represent the data type of array's elements. • '[ elements ]' represent list of values which we want to store in array. array_name = array.array( type_code, [ elements ] )
  • 8. List Of 'type_code' type_code Data type Minimum size in bytes 'b' Signed integer 1 'B' Unsigned integer 1 'i' Signed integer 2 'I' Unsigned integer 2 'l' Signed integer 4 'L' Unsigned integer 4
  • 9. List Of 'type_code' type_code Data type Minimum size in bytes 'f' Floating point 4 'd' Double precision floating point 8 'u' Unicode character 2
  • 10. Array Declaration Example >>> import array >>> my_array = array.array( 'i', [ 5, 7, 11, 14 ] ) >>> for i in my_array: print( i ) 5 7 11 14
  • 11. Programming Example • A program to create an array to store the characters of word 'Welcome' and display all characters. >>> import array >>> my_array = array.array( 'u', [ 'W', 'e', 'l', 'c', 'o', 'm', 'e' ] ) >>> for i in my_array: print( i ) W m e e l c o
  • 12. Different Ways Of Importing Array Module • Import array module with an alias or alternate name as below: • Here 'arr' is an alias name of imported module. • For example: >>> import array as arr >>> my_array = arr.array( 'i', [ 5, 7, 11, 14 ] ) >>> import array as arr
  • 13. Different Ways Of Importing Array Module • Second way of importing 'array' module is below: • Here '*' means all. That means we import all things such as classes, objects, variables etc. of array module in our program. • For example: >>> from array import * >>> my_array = array( 'i', [ 5, 7, 11, 14 ] ) >>> from array import *
  • 14. Indexing On Arrays • An index represent position value of an element in an array. • Array index always start from 0. • For example: >>> from array import * >>> x = array( 'i', [ 5, 7, 11, 14 ] ) >>> print( x[ 2 ] ) 11 5 7 11 14 x[ 0 ] x[ 1 ] x[ 2 ] x[ 3 ]
  • 15. Accessing Array Using Index - By for loop from array import * a = array( 'i', [ 34, 56,-12, 44 ] ) n = len( a ) for i in range( n ): print( a[ i ], 't', end = ' ' ) 34 56 -12 44
  • 16. Accessing Array Using Index - By while loop from array import * a = array( 'i', [ 34, 56,-12, 44 ] ) n = len( a ) i = 0 while i < n: print( a[ i ], 't', end = ' ' ) i += 1 34 56 -12 44
  • 17. Slicing On Array • By slicing operation we can retrive a piece of array out of an array. • The syntax of slicing is: • For example: array_name[ start: stop: stride ] >>> x = array( 'i', [ 10, 20, 30, 40, 50, 60, 70, 80, 90 ] ) >>> print( x[ 1: 4 ] ) array('i', [20, 30, 40]) >>> print( x[ 1: : 2 ] ) array('i', [20, 40, 60, 80])
  • 18. Slicing On Array array('i', [20, 40, 60, 80]) >>> print( x[ : 8: 2 ] ) array('i', [10, 30, 50, 70]) >>> print( x[ -1: : ] ) array('i', [90]) >>> print( x[ : : -1 ] ) array('i', [90, 80, 70, 60, 50, 40, 30, 20, 10]) >>> print( x[ : -5: -1 ] ) array('i', [90, 80, 70, 60]) >>> print( x[ -5: : ] ) array('i', [50, 60, 70, 80, 90])
  • 19. Programming Example • A program to calculate sum of a range of elements in an array. from array import * x = array( 'i', [ 10, 20, 30, 40, 50, 60, 70, 80, 90 ] ) sum = 0 for i in x[ 2: 5 ]: sum = sum + i print( sum )