Functions and their use in Python
Function is an arrangement of a statement that execute like a data processing or computing. Whenever you define a function you define the name of the function. You can call a function by its name, it is called a function call.
Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
The function takes an argument and returns a result.The result is called aReturn value.
Built-in Functions
Python provides a number of built-in function which convert the values from one type to the other type such as maxand minfunction. It gives us the largest and smallest value in a list.The max function declares "largest character" whereas the min function declare the"smallest character"in the string.
Syntax
max(iterable, *iterables[,key, default])
max(arg1, arg2, *args[, key])
Example:
max(2,3,4)
Output:
>>>max(2,3,4)
>>> 4
min(2,3,4)
Output:
>>>min(2,3,4)
>>> 2
Types of Conversion Functions
Integer Conversion:
The int function converts the any value of an integer. We can convert integer number in many types such as int to float, int to string etc. Some of the examples are:
int can convert floating-point value to integer, it doesn't round off or cuts the fractional part .
int(5.99999)
int(-4.9)
Output:
>>>int(5.99999)
>>>5
>>>int(-4.9)
>>>-4
Float Conversion:
float convets the integer and string into floating point numbers.
float(89)
Output:
>>>float(89)
>>>89.0
String Conversion:
str converts its arguments in a string
str(30)
str(4.909090)
Output:
>>>str(30)
>>>'30'
>>>str(4.909090)
>>>'4.909090'
Math Functions
Python provides some of the usual mathematical functions in math module. Before using that module you have to import it.
import math
For creating module object we use the word math. To get some information by printing the module object .The module object consists of the functions and variables defined to access one of the functions, you have to specify the name of the module and the name of the function, distinct by a dot . This format is called dot notation.
print(math)
>>><module 'math' (built-in)>
ratio= signal_power / noise_power
decibels = 16 * math.log10(ratio)
radians = 0.6
height = math.sin(radians)
Some useful Math function are:
Function | Description |
---|---|
ceil(x) | Returns the smallest integer greater than or equal to x. |
copysign(x, y) | Returns x with the sign of y |
fabs(x) | Returns the absolute value of x |
factorial(x) | Returns the factorial of x |
floor(x) | Returns the largest integer less than or equal to x |
fmod(x, y) | Returns the remainder when x is divided by y |
frexp(x) | Returns the mantissa and exponent of x as the pair (m, e) |
fsum(iterable) | Returns an accurate floating point sum of values in the iterable |
isfinite(x) | Returns True if x is neither an infinity nor a NaN (Not a Number) |
isinf(x) | Returns True if x is a positive or negative infinity |
isnan(x) | Returns True if x is a NaN |
ldexp(x, i) | Returns x * (2**i) |
modf(x) | Returns the fractional and integer parts of x |
trunc(x) | Returns the truncated integer value of x |
exp(x) | Returns e**x |
expm1(x) | Returns e**x - 1 |
log(x[, base]) | Returns the logarithm of x to the base (defaults to e) |
log1p(x) | Returns the natural logarithm of 1+x |
log2(x) | Returns the base-2 logarithm of x |
log10(x) | Returns the base-10 logarithm of x |
pow(x, y) | Returns x raised to the power y |
sqrt(x) | Returns the square root of x |
acos(x) | Returns the arc cosine of x |
asin(x) | Returns the arc sine of x |
atan(x) | Returns the arc tangent of x |
atan2(y, x) | Returns atan(y / x) |
cos(x) | Returns the cosine of x |
hypot(x, y) | Returns the Euclidean norm, sqrt(x*x + y*y) |
sin(x) | Returns the sine of x |
tan(x) | Returns the tangent of x |
degrees(x) | Converts angle x from radians to degrees |
radians(x) | Converts angle x from degrees to radians |
acosh(x) | Returns the inverse hyperbolic cosine of x |
asinh(x) | Returns the inverse hyperbolic sine of x |
atanh(x) | Returns the inverse hyperbolic tangent of x |
cosh(x) | Returns the hyperbolic cosine of x |
sinh(x) | Returns the hyperbolic cosine of x |
tanh(x) | Returns the hyperbolic tangent of x |
erf(x) | Returns the error function at x |
erfc(x) | Returns the complementary error function at x |
gamma(x) | Returns the Gamma function at x |
lgamma(x) | Returns the natural logarithm of the absolute value of the Gamma function at x |
pi | Mathematical constant, the ratio of circumference of a circle to it's diameter (3.14159...) |
e | mathematical constant e (2.71828...) |