Arrays - Inside
In array Module, array class provides large number of array Method, which can be used to process arrays in many ways.
Array Methods are called by below syntax:
Object name .method ()
Below are some array methods and its functions:
- append(a) : Adds an element a at the end of an array x
- count(a): Return number of occurrence of a in array x.
- fromlist(1st): Appends items from the 1st to end of an array
- fromstring(s): Appends item from string s to end of an array a.
- index(x): Return the position of first occurrence of x in the array.
- insert(I,a): Insert a in the position I of array x.
- pop(a): Removes element x from an array x and return it.
- pop(): Remove last element from an array x.
- remove(a): Remove the first occurrence a in the array x.
- reverse(): Reverse the order of elements in the array a.
- tolist(): Convert the array x to list.
Example 1: Write a Python Program to understand various array Methods.
Example 2: Write a Program to take student Score in to an array , find its total marks and its percentage.
let’s Understand this , when first statement is executed , then user will provide the marks along with space , which is indicated by split() method. This marks are stored in string str. Now we wil convert this string to array arr by using below command
marks = [int(num) for num in str]
Where marks is name of array . Now if you see the For loop. In array marks , it will take the each number from str and stores it in to num and num is converted to in to integer by int(num).
Finally this num is stored in marks array.
Example 3: Write a Python program to search the position of an element in an array and display it by using index() method.