** Python Certification Training: https://www.edureka.co/python **
This Edureka PPT on 'Arrays in Python' will help you establish a strong hold on all the fundamentals in the Python programming language. Below are the topics covered in this PPT:
What is an array?
Is python list same as an array?
How to create arrays in python?
Accessing array elements
Basic array operations
- Finding the length of an array
- Adding Elements
- Removing elements
- Array concatenation
- Slicing
- Looping
Python Tutorial Playlist: https://goo.gl/WsBpKe
Blog Series: http://bit.ly/2sqmP4s
Follow us to never miss an update in the future.
YouTube: https://www.youtube.com/user/edurekaIN
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
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
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.
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
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
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
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
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
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
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
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