SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
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 )

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Queues
QueuesQueues
Queues
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Array
ArrayArray
Array
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Python numbers
Python numbersPython numbers
Python numbers
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 

Ähnlich wie Python array (20)

Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Arrays
ArraysArrays
Arrays
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
array Details
array Detailsarray Details
array Details
 
Arrays
ArraysArrays
Arrays
 
Array-part1
Array-part1Array-part1
Array-part1
 

Kürzlich hochgeladen

Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...amber724300
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 

Kürzlich hochgeladen (20)

Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 

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 )