Array Function 230321 202235
Array Function 230321 202235
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
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?
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"
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
for x in cars:
print(x)
Example
cars.pop(1)
You can also use the remove() method to remove an element from the array.
Example
cars.remove("Volvo")
# Python program to demonstrate
# Creation of Array
print()
Creating a Function
In Python a function is defined using the def keyword:
Example
def my_function():
Example
def my_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):
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)
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)
math.isnan(x)
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.