340 likes | 1.75k Views
** Python Certification Training: https://www.edureka.co/python **<br>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: <br>What is an array?<br>Is python list same as an array?<br>How to create arrays in python?<br>Accessing array elements<br>Basic array operations<br> - Finding the length of an array<br> - Adding Elements<br> - Removing elements<br> - Array concatenation<br> - Slicing<br> - Looping <br><br>Python Tutorial Playlist: https://goo.gl/WsBpKe<br>Blog Series: http://bit.ly/2sqmP4s<br><br>Follow us to never miss an update in the future.<br>YouTube: https://www.youtube.com/user/edurekaIN<br>Instagram: https://www.instagram.com/edureka_learning/<br>Facebook: https://www.facebook.com/edurekaIN/<br>Twitter: https://twitter.com/edurekain<br>LinkedIn: https://www.linkedin.com/company/edureka
E N D
What is an Array? Is Python List same as an Array? How to Create an Array in Python? Accessing Array Elements Basic Array Operations • Finding the length of an Array • Addition • Removal • Concatenation • Slicing • Looping www.edureka.co/python
What is an Array? www.edureka.co/python
What is 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. → → Var_Name a Basic structure of an Array: 1 2 3 … 100 Values → Index → a[0] a[1] a[2] a[3…98] a[99] www.edureka.co/python
Is Python List same as an Array? www.edureka.co/python
Is Python List same as an Array? 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. Python Arrays and lists have the same way of storing data. www.edureka.co/python
How to create Arrays in Python? www.edureka.co/python
How to create Arrays in Python? Arrays in Python can be created after importing the array module. 2 3 1 USING ALIAS USING * WITHOUT ALIAS → import array → import array as arr → from array import * www.edureka.co/python
Accessing Array Elements www.edureka.co/python
Accessing Array Elements a[2]=3 ❑ 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. Example: a[2]=3 www.edureka.co/python
Basic Array Operations www.edureka.co/python
Basic Array Operations Finding the length of an Array Array Concatenation Array Concatenation Slicing Adding/ Changing element of an Array Operations Removing/ Deleting elements of an array Looping through an Array www.edureka.co/python
Finding the length of an Array www.edureka.co/python
Finding the length of an Array ❑ 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. www.edureka.co/python
Finding the length of an Array SYNTAX: len(array_name) L ength of an array is determ ined using the len len() () function as follow s: import array as arr a=arr.array('d', [1.1 , 2.1 ,3.1] ) len(a) Output- 3 www.edureka.co/python
Adding elements to an Array www.edureka.co/python
Adding elements to an Array Functions used to add elements to an Array: js extend() append() Insert() 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. Used when you want to add a single element at the end of an array. www.edureka.co/python
Adding elements to an Array The code snippet below im plem ents the append(), extend() and insert() append(), extend() and insert() functions: 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- 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]) www.edureka.co/python
Removing elements of an Array www.edureka.co/python
Removing elements of an Array Functions used to remove elements of an Array: js pop() remove() Used when you want to remove an element with a specific value without returning it. Used when you want to remove an element and return it. www.edureka.co/python
Removing elements of an Array The code snippet below show s how you can rem ove elem ents using these tw o functions: 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 4thelement”,a.pop(3)) a.remove(1.1) print(a) OUTPUT- Popping last element 3.7 Popping 4th element 3.1 array('d', [2.2, 3.8]) www.edureka.co/python
Array Concatenation www.edureka.co/python
Array Concatenation A rray concatenation can be done as follow s using the + sym bol: 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) OUTPUT- Array c= array(‘d’, [1.1, 2.1, 3.1, 2.6, 7.8, 3.7, 8.6]) www.edureka.co/python
Slicing an Array www.edureka.co/python
Slicing an Array An array can be sliced using the : symbol. This returns a range of elements that we have specified by the index numbers. OUTPUT - import array as arr a=arr.array('d',[1.1 , 2.1 ,3.1,2.6,7.8]) print(a[0:3]) array(‘d’, [1.1, 2.1, 3.1]) www.edureka.co/python
Looping through an Array www.edureka.co/python
Looping through an Array We can loop through an array easily using the for and while loops. js for while Iterates over the items of an array specified number of times. Iterates over the elements until a certain condition is met. www.edureka.co/python
Looping through an Array using for loop Some of the for loop implementations are: OUTPUT- A ll values 1.1 2.2 3.8 3.1 3.7 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) www.edureka.co/python
Looping through an Array using while loop Example for while loop implementation import array as arr a=arr.array('d', [1.1, 2.2, 3.8, 3.1, 3.7]) b=0 OUTPUT- 1.1 2.2 3.8 3.1 3.7 while b<len(a): print(a[b]) b=b+1 www.edureka.co/python