SlideShare a Scribd company logo
1 of 30
What is an Array?
How to Create an Array in Python?
Is Python List same as an Array?
Basic Array Operations
• Finding the length of an Array
• Addition
• Removal
• Concatenation
• Slicing
• Looping
Accessing Array Elements
www.edureka.co/python
What is an Array?
www.edureka.co/python
What is an Array?
www.edureka.co/python
→ a
a[0] a[1] a[2] a[3…98] a[99]
1 2 3 … 100
Var_Name
Values →
Index →
Basic structure
of an Array:
An array is basically a data structure which can hold more than one value at a
time. It is a collection or ordered series of elements of the same type.
Is Python List same as an Array?
www.edureka.co/python
Is Python List same as an Array?
Python Arrays and lists have the
same way of storing data.
Arrays take only a single data type
elements but lists can have any
type of data.
Therefore, other than a few
operations, the kind of operations
performed on them are different.
www.edureka.co/python
How to create Arrays in Python?
www.edureka.co/python
Arrays in Python can be created after importing the array module.
How to create Arrays in Python?
→ from array import *
USING *
3
→ import array → import array as arr
WITHOUT ALIAS
1 USING ALIAS
2
www.edureka.co/python
Accessing Array Elements www.edureka.co/python
❑ Access elements using index values.
❑ Indexing starts at 0 and not from 1. Hence, the index number is always 1 less than the length
of the array.
❑ Negative index values can be used as well. The point to remember is that negative indexing
starts from the reverse order of traversal i.e from right to left.
www.edureka.co/python
a[2]=3
Example: a[2]=3
Accessing Array Elements
Basic Array Operations www.edureka.co/python
Operations
Removing/ Deleting
elements of an array
Slicing
Looping through an Array
Basic Array Operations
Adding/ Changing
element of an Array
Finding the length of an
Array
Array ConcatenationArray Concatenation
www.edureka.co/python
Finding the length of an Array www.edureka.co/python
❑ Length of an array is the number of elements that are actually present in an array.
❑ You can make use of len() function to achieve this.
❑ The len() function returns an integer value that is equal to the number of elements present in that array.
Finding the length of an Array
www.edureka.co/python
Lengthofanarrayisdeterminedusingthelen()functionasfollows:
Finding the length of an Array
import array as arr
a=arr.array('d', [1.1 , 2.1 ,3.1] )
len(a)
len(array_name)
Output- 3
SYNTAX:
www.edureka.co/python
Adding elements to an Array
www.edureka.co/python
js
Insert()extend()append()
Used when you want to add
a single element at the end
of an array.
Used when you want to add
more than one element at
the end of an array.
Used when you want to add
an element at a specific
position in an array.
Functions used to add elements to an Array:
Adding elements to an Array
www.edureka.co/python
Thecodesnippetbelowimplementstheappend(),extend()andinsert() functions:
Adding elements to an Array
Array a= array('d', [1.1, 2.1, 3.1, 3.4])
Array b= array('d', [2.1, 3.2, 4.6, 4.5,
3.6, 7.2])
Array c=array('d', [1.1, 2.1,3.4, 3.1])
import array as arr
a=arr.array('d', [1.1 , 2.1 ,3.1] )
a.append(3.4)
print("Array a=",a)
b=arr.array('d',[2.1,3.2,4.6])
b.extend([4.5,3.6,7.2])
print("Array b=",b)
c=arr.array( 'd' , [1.1 , 2.1 ,3.1] )
c.insert(2,3.4)
print(“Arrays c=“,c)
OUTPUT-
www.edureka.co/python
Removing elements of an Array www.edureka.co/python
Removing elements of an Array
js
pop() remove()
Used when you want to
remove an element and
return it.
Used when you want to
remove an element with a
specific value without
returning it.
Functions used to remove elements of an Array:
www.edureka.co/python
Thecodesnippetbelowshowshowyoucanremoveelementsusingthesetwofunctions:
import array as arr
a=arr.array('d', [1.1, 2.2, 3.8, 3.1, 3.7])
print(“Popping last element”,a.pop())
print(“Popping 4th element”,a.pop(3))
a.remove(1.1)
print(a)
Popping last element 3.7
Popping 4th element 3.1
array('d', [2.2, 3.8])
OUTPUT-
Removing elements of an Array
www.edureka.co/python
Array Concatenation
www.edureka.co/python
Arrayconcatenation canbedoneasfollowsusingthe+symbol:
Array Concatenation
import array as arr
a=arr.array('d',[1.1 , 2.1 ,3.1,2.6,7.8])
b=arr.array('d',[3.7,8.6])
c=arr.array('d’)
c=a+b
print("Array c = ",c)
Array c= array(‘d’, [1.1, 2.1, 3.1, 2.6,
7.8, 3.7, 8.6])
OUTPUT-
www.edureka.co/python
Slicing an Array
www.edureka.co/python
Slicing an Array
import array as arr
a=arr.array('d',[1.1 , 2.1 ,3.1,2.6,7.8])
print(a[0:3])
OUTPUT -
array(‘d’, [1.1, 2.1, 3.1])
An array can be sliced using the : symbol. This returns a range of elements that we have
specified by the index numbers.
www.edureka.co/python
Looping through an Array
www.edureka.co/python
js
for while
Iterates over the items of an
array specified number of
times.
Iterates over the elements
until a certain condition is met.
We can loop through an array easily using the for and while loops.
Looping through an Array
www.edureka.co/python
Some of the for loop implementations are:
import array as arr
a=arr.array('d', [1.1, 2.2, 3.8, 3.1, 3.7])
print("All values")
for x in a:
print(x)
Allvalues
1.1
2.2
3.8
3.1
3.7
OUTPUT-
Looping through an Array using for loop
www.edureka.co/python
Example for while loop implementation
Looping through an Array using while loop
import array as arr
a=arr.array('d', [1.1, 2.2, 3.8, 3.1, 3.7])
b=0
while b<len(a):
print(a[b])
b=b+1
1.1
2.2
3.8
3.1
3.7
OUTPUT-
www.edureka.co/python
www.edureka.co/python

More Related Content

What's hot (20)

Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
NUMPY
NUMPY NUMPY
NUMPY
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Python strings
Python stringsPython strings
Python strings
 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Linked list
Linked listLinked list
Linked list
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
 
Python list
Python listPython list
Python list
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
 
NumPy
NumPyNumPy
NumPy
 
Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In Python
 

Similar to Arrays In Python | Python Array Operations | Edureka

Similar to Arrays In Python | Python Array Operations | Edureka (20)

STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Array properties
Array propertiesArray properties
Array properties
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
Collections framework
Collections frameworkCollections framework
Collections framework
 
Data structures
Data structuresData structures
Data structures
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
ACP-arrays.pptx
ACP-arrays.pptxACP-arrays.pptx
ACP-arrays.pptx
 
F# array searching
F#  array searchingF#  array searching
F# array searching
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)
 
Net (f#) array
Net (f#)  arrayNet (f#)  array
Net (f#) array
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
numpy.pdf
numpy.pdfnumpy.pdf
numpy.pdf
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 

More from Edureka!

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaEdureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaEdureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaEdureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaEdureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaEdureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaEdureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaEdureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaEdureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaEdureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaEdureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | EdurekaEdureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEdureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEdureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaEdureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaEdureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaEdureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaEdureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaEdureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | EdurekaEdureka!
 

More from Edureka! (20)

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
 

Recently uploaded

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Recently uploaded (20)

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

Arrays In Python | Python Array Operations | Edureka

  • 1.
  • 2. What is an Array? How to Create an Array in Python? Is Python List same as an Array? Basic Array Operations • Finding the length of an Array • Addition • Removal • Concatenation • Slicing • Looping Accessing Array Elements www.edureka.co/python
  • 3. What is an Array? www.edureka.co/python
  • 4. What is an Array? www.edureka.co/python → a a[0] a[1] a[2] a[3…98] a[99] 1 2 3 … 100 Var_Name Values → Index → Basic structure of an Array: An array is basically a data structure which can hold more than one value at a time. It is a collection or ordered series of elements of the same type.
  • 5. Is Python List same as an Array? www.edureka.co/python
  • 6. Is Python List same as an Array? Python Arrays and lists have the same way of storing data. Arrays take only a single data type elements but lists can have any type of data. Therefore, other than a few operations, the kind of operations performed on them are different. www.edureka.co/python
  • 7. How to create Arrays in Python? www.edureka.co/python
  • 8. Arrays in Python can be created after importing the array module. How to create Arrays in Python? → from array import * USING * 3 → import array → import array as arr WITHOUT ALIAS 1 USING ALIAS 2 www.edureka.co/python
  • 9. Accessing Array Elements www.edureka.co/python
  • 10. ❑ Access elements using index values. ❑ Indexing starts at 0 and not from 1. Hence, the index number is always 1 less than the length of the array. ❑ Negative index values can be used as well. The point to remember is that negative indexing starts from the reverse order of traversal i.e from right to left. www.edureka.co/python a[2]=3 Example: a[2]=3 Accessing Array Elements
  • 11. Basic Array Operations www.edureka.co/python
  • 12. Operations Removing/ Deleting elements of an array Slicing Looping through an Array Basic Array Operations Adding/ Changing element of an Array Finding the length of an Array Array ConcatenationArray Concatenation www.edureka.co/python
  • 13. Finding the length of an Array www.edureka.co/python
  • 14. ❑ Length of an array is the number of elements that are actually present in an array. ❑ You can make use of len() function to achieve this. ❑ The len() function returns an integer value that is equal to the number of elements present in that array. Finding the length of an Array www.edureka.co/python
  • 15. Lengthofanarrayisdeterminedusingthelen()functionasfollows: Finding the length of an Array import array as arr a=arr.array('d', [1.1 , 2.1 ,3.1] ) len(a) len(array_name) Output- 3 SYNTAX: www.edureka.co/python
  • 16. Adding elements to an Array www.edureka.co/python
  • 17. js Insert()extend()append() Used when you want to add a single element at the end of an array. Used when you want to add more than one element at the end of an array. Used when you want to add an element at a specific position in an array. Functions used to add elements to an Array: Adding elements to an Array www.edureka.co/python
  • 18. Thecodesnippetbelowimplementstheappend(),extend()andinsert() functions: Adding elements to an Array Array a= array('d', [1.1, 2.1, 3.1, 3.4]) Array b= array('d', [2.1, 3.2, 4.6, 4.5, 3.6, 7.2]) Array c=array('d', [1.1, 2.1,3.4, 3.1]) import array as arr a=arr.array('d', [1.1 , 2.1 ,3.1] ) a.append(3.4) print("Array a=",a) b=arr.array('d',[2.1,3.2,4.6]) b.extend([4.5,3.6,7.2]) print("Array b=",b) c=arr.array( 'd' , [1.1 , 2.1 ,3.1] ) c.insert(2,3.4) print(“Arrays c=“,c) OUTPUT- www.edureka.co/python
  • 19. Removing elements of an Array www.edureka.co/python
  • 20. Removing elements of an Array js pop() remove() Used when you want to remove an element and return it. Used when you want to remove an element with a specific value without returning it. Functions used to remove elements of an Array: www.edureka.co/python
  • 21. Thecodesnippetbelowshowshowyoucanremoveelementsusingthesetwofunctions: import array as arr a=arr.array('d', [1.1, 2.2, 3.8, 3.1, 3.7]) print(“Popping last element”,a.pop()) print(“Popping 4th element”,a.pop(3)) a.remove(1.1) print(a) Popping last element 3.7 Popping 4th element 3.1 array('d', [2.2, 3.8]) OUTPUT- Removing elements of an Array www.edureka.co/python
  • 23. Arrayconcatenation canbedoneasfollowsusingthe+symbol: Array Concatenation import array as arr a=arr.array('d',[1.1 , 2.1 ,3.1,2.6,7.8]) b=arr.array('d',[3.7,8.6]) c=arr.array('d’) c=a+b print("Array c = ",c) Array c= array(‘d’, [1.1, 2.1, 3.1, 2.6, 7.8, 3.7, 8.6]) OUTPUT- www.edureka.co/python
  • 25. Slicing an Array import array as arr a=arr.array('d',[1.1 , 2.1 ,3.1,2.6,7.8]) print(a[0:3]) OUTPUT - array(‘d’, [1.1, 2.1, 3.1]) An array can be sliced using the : symbol. This returns a range of elements that we have specified by the index numbers. www.edureka.co/python
  • 26. Looping through an Array www.edureka.co/python
  • 27. js for while Iterates over the items of an array specified number of times. Iterates over the elements until a certain condition is met. We can loop through an array easily using the for and while loops. Looping through an Array www.edureka.co/python
  • 28. Some of the for loop implementations are: import array as arr a=arr.array('d', [1.1, 2.2, 3.8, 3.1, 3.7]) print("All values") for x in a: print(x) Allvalues 1.1 2.2 3.8 3.1 3.7 OUTPUT- Looping through an Array using for loop www.edureka.co/python
  • 29. Example for while loop implementation Looping through an Array using while loop import array as arr a=arr.array('d', [1.1, 2.2, 3.8, 3.1, 3.7]) b=0 while b<len(a): print(a[b]) b=b+1 1.1 2.2 3.8 3.1 3.7 OUTPUT- www.edureka.co/python