How To Do Math With Lists in python ?

ProgrammingPythonServer Side Programming

We  not only use lists to store a collection of values, but we also use it to perform some mathematical calculations or operations to do.

Example 1

import math
data = 21.6
print('The floor of 21.6 is:', math.floor(data))

Output

The floor of 21.6 is: 21

How To Calculate the Weighted Average of a List

Example 2

cost = [0.424, 0.4221, 0.4185, 0.4132, 0.413]
cases = [10, 20, 30, 40, 50]
cost = [23, 10, 5, 32, 41]
weight= [10, 20, 30, 40, 50]
for i in range(len(cost)):
cost[c] = (cost[i] * weight[i] / sum(weight))
cost = sum(cost)
print(cost)

Output

72.84444444444445

Example 3

import math
degree = 180
radian = math.radians(degree)

print('The given angle is :', radian )
print('sin(x) is :', math.sin(radian ))
print('cos(x) is :', math.cos(radian ))
print('tan(x) is :', math.tan(radian ))

Output

The given angle is : 3.141592653589793
sin(x) is : 1.2246467991473532e-16
cos(x) is : -1.0
tan(x) is : -1.2246467991473532e-16

Following are the few Python Math Functions

  • ceil(x):Returns the smallest integer value greater than or equal to x.
  • copysign(x, y): Returns x with a 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 a remainder when x is divided by y
  • frexp(x): Returns a mantissa and exponent of x as the pair (m, e)
  • fsum(iterable): Returns the 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 the positive or negative infinity
  • isnan(x): Returns True if x is the NaN
  • ldexp(x, i) : Returns x * (2**i)
  • modf(x): Returns a fractional and integer parts of x
  • trunc(x): Returns a 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
raja
Sri
Published on 27-Jun-2019 18:45:21
Advertisements