Python Arrays and Built-in Math Functions
Arrays:
Like other programming languages, Python doesn't have built-in support for Arrays, instead lists are used. But in lists the items stored is not required to be of same data type.
Array module is used in order to create and manipulate arrays.
Array can store fixed number of items and these can be of same data type. Items in array are referred as elements and they can be of any data type. Index starts from 0.
Using arrays, we can
| Method | Description | Example | | --- | --- | --- | | | Access the elements of an array | names= ['Ram','Sita','Laxman'] arr = names[1] print(arr) Output: Sita | | | Modify the elements of an array | names= ['Ram','Sita','Laxman'] names[0] = "Rama" print(names) Output: ['Rama','Sita','Laxman'] | | append() | Adds an element to the end of the list | names= ['Ram','Sita','Laxman'] names.append('Rama') print(names) Output: ['Ram','Sita','Laxman','Rama'] | | clear() | Removes all the elements from the list | names= ['Ram','Sita','Laxman'] names.clear() print(names) Output: [] | | copy() | Returns a copy of the list | names= ['Ram','Sita','Laxman'] arr = names.copy() print(arr) Output: ['Ram','Sita','Laxman'] | | count() | Returns number of elements in the list | names= ['Ram','Sita','Laxman'] arr = names.Count('Sita') print(arr) Output: 1 | | index() | Returns the position of the value | names= ['Ram','Sita','Laxman'] arr = names.index("Laxman") print(arr) Output: 2 | | insert() | Adds an element at specific location | names= ['Ram','Sita','Laxman'] names.insert(0,"Rama") print(names) Output: ['Rama','Ram','Sita','Laxman'] | | pop() | Removes the element at specified position | names= ['Ram','Sita','Laxman'] names.pop(1) print(names) Output: ['Ram','Laxman'] | | remove() | Removes the first element with specified value | names= ['Ram','Sita','Laxman'] names.remove('Sita') print(names) Output: ['Ram','Laxman'] | | reverse() | Reverses the order of the list | names= ['Ram','Sita','Laxman'] names.reverse() print(names) Output: ['Laxman','Sita','Ram'] | | sort() | Sorts the list alphabetically | names= ['Ram','Sita','Laxman'] names.sort() print(names) Output: ['Laxman','Ram','Sita'] |
Built-In Math Functions:
| Functions | Description | Example | | --- | --- | --- | | min() | Finds the lowest value | m = min(2,4,1) print(m) Output: 1 | | max() | Finds the highest value | m = max(2,4,1) print(m) Output: 4 | | abs() | Returns positive number | a = abs(-3) print(a) Output : 3 | | pow(a,b) | Returns a to the power of b | a = pow(2,3) print(a) Output : 8 | | sqrt() | import math module and then use. Returns square root of the number. | import math a = math.sqrt(25) print(a) Output : 5 | | pi() | import math module and then use. Returns the value of pi which is 3.14 | import math a = math.pi() print(a) Output : 3.141592...... |
Subscribe to my newsletter
Read articles from bellam anu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by