Arrays
What is an array, Array length,
Looping array elements, Different
operations on array, Array Methods
Web ref: w3school , geeks ,
javatpoint , doc.python
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:
Create an array containing car names:
cars = ["Ford", "Volvo", "BMW"]
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
You refer to an array element by referring to the index number.
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)
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.
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")
# Python program to demonstrate
# Creation of Array
# importing "array" for array creations
import array as arr
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (a[i], end =" ")
print()
# creating an array with double type
b = arr.array('d', [2.5, 3.2, 3.3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (b[i], end =" ")
Function
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Creating a Function
In Python a function is defined using the def keyword:
Example
def my_function():
print("Hello from a function")
Calling a Function
To call a function, use the function name followed by parenthesis:
Example
def my_function():
print("Hello from a function")
my_function()
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just
separate them with a comma.
The following example has a function with one argument (fname). When the function is called, we pass along a first name, which
is used inside the function to print the full name:
Example
def my_function(fname):
print(fname + " Refsnes")
my_function("name-1")
my_function("name-2")
my_function("name-3")
Return Values
To let a function return a value, use the return statement:
Example
def my_function(x):
return 5 * x
Standard Library function
These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if you require
support for complex numbers. The distinction between functions which support complex numbers and those which don’t is made since
most users do not want to learn quite as much mathematics as required to understand complex numbers. Receiving an exception
instead of a complex result allows earlier detection of the unexpected complex number used as a parameter, so that the programmer
can determine how and why it was generated in the first place.
math.ceil(x)
Return the ceiling of x, the smallest integer greater than or equal to x. If x is not a float, delegates to x.__ceil__, which should
return an Integral value.
math.copysign(x, y)
Return a float with the magnitude (absolute value) of x but the sign of y. On platforms that support signed zeros, copysign(1.0,
-0.0) returns -1.0.
math.fabs(x)
Return the absolute value of x.
math.factorial(n)¶
Return n factorial as an integer. Raises ValueError if n is not integral or is negative.
Deprecated since version 3.9: Accepting floats with integral values (like 5.0) is deprecated.
math.floor(x)
Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__, which should return an Integral value.
math.frexp(x)
Return the mantissa and exponent of x as the pair (m, e). m is a float and e is an integer such that x == m * 2**e exactly. If x is zero, returns (0.0, 0), otherwise 0.5 <=
abs(m) < 1. This is used to “pick apart” the internal representation of a float in a portable way.
math.fsum(iterable)
Return an accurate floating point sum of values in the iterable. Avoids loss of precision by tracking multiple intermediate partial sums:
math.isinf(x)
Return True if x is a positive or negative infinity, and False otherwise.
math.isnan(x)
Return True if x is a NaN (not a number), and False otherwise.
math.isqrt(n)
Return the integer square root of the nonnegative integer n. This is the floor of the exact square root of n, or equivalently the greatest integer a such that a² ≤ n.