Arrays (Strings)
Arrays are used to store multiple values of similar type in one single variable:
Example
Create an array containing car names:
cars = ["Ford", "Volvo", "BMW"]
Mark=[2,3,2.5,3.5,5,4,3]
L=[]
i=1
//Reading
While(True):
//Write
l[0]=”abcd”
x=L[1]
sort
search
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars
in single variables could look like this:
car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"
However, what if you want to loop through the cars and find a specific one?
And what if you had not 3 cars, but 300?
The solution is an array!
An array can hold many values under a single name, and you can access the
values by referring to an index number.
Access the Elements of an Array (Indexing)
You refer to an array element by referring to the index number.
Example Get the value of the first array item:
x = cars[0]
Example Modify the value of the first array item:
cars[0] = "Toyota"
The Length of an Array
Use the len() method to return the length of an array (the number of
elements in an array).
Example Return the number of elements in the cars array:
x = len(cars)
Note: The length of an array is always one more than the highest array
index.
Looping Array Elements
You can use the for in loop to loop through all the elements of an array.
Example Print each item in the cars array:
for x in cars:
print(x)
Adding Array Elements
You can use the append() method to add an element to an array.
Example Add one more element to the cars array:
cars.append("Honda")
Removing Array Elements
You can use the pop() method to remove an element from the array.
Example Delete the second element of the cars array:
cars.pop(1)
You can also use the remove() method to remove an element from the array.
Example Delete the element that has the value "Volvo":
cars.remove("Volvo")
Note: The list's remove() method only removes the first occurrence of the
specified value.
Array Methods
Python has a set of built-in methods that you can use on lists/arrays.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the
specified value
extend() Add the elements of a list (or any iterable), to
the end of the current list
index() Returns the index of the first element with the
specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified
value
reverse() Reverses the order of the list
sort() Sorts the list
Create a NumPy Array Object in Python
NumPy is used to work with arrays. The array object in NumPy is
called ndarray.
We can create a NumPy ndarray object by using the array() function.
import numpy as np
Create Array
arr = np.array([])
print(arr)
for I in arr:
print(I)
print(type(arr))
How do we make 0D , 1D and 2D array using Numpy
import numpy as np
//a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
For I in c:
print(I)
print(a)
print(b)
print(c)
// Print dimension of array
print(a.ndim)
print(b.ndim)
print(c.ndim)
Sequence
Strings, Array, List, Tuple, Set, Dictionary
// Indexing and Slicing
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
Array Slicing - print(arr[:2])
Array Indexing - print(arr[0])
In 2D array;
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])
Operations / Methods over numpy array;
from numpy import *
Sin(arr), cos, tan, log,
abs, sqrt, power(arr,n),
sum, min, max, size
mean, mode, median, std, var,
sort,
reshape() Method
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr)
arr=arr.reshape(2,5) or arr.reshape(5,2)
flatten() Method
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
arr=arr.flatten()
print(arr)
Other ways to make array
from numpy import *
arr=np.random.randint(1,20,50)
a= arrange(start, stop, stepsize)
a=arrange(1,20,2)
Matrix in numpy
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
m= matrix(arr)
print(m)
Matrix Methods
Methods
m.max(), m.min(), m.sum(),m.mean(),
m.sort(), m.transpose().
A=matrix([‘1 2 3: 4 5 6:7 8 9’]) // create matrix directly
B=matrix(‘5 6 7: 2 4 6’)
Matrix Addition : C=A+B
E=A-B
print(C)
Matrix Multiplication : D=A*B
print(D)