1 / 30

Arrays In Python | Python Array Operations | Edureka

** 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

EdurekaIN
Download Presentation

Arrays In Python | Python Array Operations | Edureka

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. What is an Array? www.edureka.co/python

  3. 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

  4. Is Python List same as an Array? www.edureka.co/python

  5. 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

  6. How to create Arrays in Python? www.edureka.co/python

  7. 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

  8. Accessing Array Elements www.edureka.co/python

  9. 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

  10. Basic Array Operations www.edureka.co/python

  11. 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

  12. Finding the length of an Array www.edureka.co/python

  13. 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

  14. 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

  15. Adding elements to an Array www.edureka.co/python

  16. 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

  17. 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

  18. Removing elements of an Array www.edureka.co/python

  19. 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

  20. 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

  21. Array Concatenation www.edureka.co/python

  22. 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

  23. Slicing an Array www.edureka.co/python

  24. 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

  25. Looping through an Array www.edureka.co/python

  26. 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

  27. 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

  28. 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

  29. www.edureka.co/python

More Related