# Python Math Examples

Use math methods. Review methods like sqrt, math.floor and math.ceil.**Math.** In ancient times, great advances were made in mathematics. A scroll shows a circle. It calculates its area. Today computers do these calculations.

**In Python we discover** many prebuilt functions. Consider a square root. We just call math.sqrt. No scroll is required for this calculation.

**Floor, ceil.** Two common math functions are floor and ceil. Floor removes the digits past the decimal place. In this example it changes 100.7 to 100.

floor**Ceil:** This rounds the number up to the next highest one. A ceiling is always above us.

**Tip:** When using methods like floor and ceil, consistency is helpful. If some parts of the program use these methods, others should too.

**Python program that imports math**
import math*
# Fractional number.
*n = *100.7**
# Absolute value.
*print(__math.floor__(n))
print(__math.ceil__(n))
**Output**
100
101

**Sum, fsum.** With sum, we add together the elements in a list. Fsum is a more accurate way to sum floating-point numbers. On integers, the methods are equal. But fsum is better for floats.

sum**Tip:** Sum and fsum can be used on any iterable collection. This includes the list, tuple and set.

**Caution:** If the iterable contains a non-numeric value, a TypeError will occur. We handle this in an except statement.

Error**Here:** In this example, the sum() method causes a rounding error to occur. The fsum() method returns a better sum.

**Python program that uses sum, fsum**
import math*
# Input list.
*values = [*0.9999999*, *1*, *2*, *3*]*
# Sum values in list.
*r = __sum__(values)
print(r)*
# Sum values with fsum.
*r = math.__fsum__(values)
print(r)
**Output**
6.999999900000001
6.9999999

**Truncate.** Truncating a number removes everything past the decimal place. This does not round the number. Instead it just eliminates the fractional part.

**Note:** The number before the decimal place is never changed with math.trunc. This is similar to casting to (int) in C-like languages.

**Python program that uses math.trunc**
import math*
# Truncate this value.
*value1 = *123.45*
truncate1 = __math.trunc__(value1)
print(truncate1)*
# Truncate another value.
*value2 = *345.67*
truncate2 = __math.trunc__(value2)
print(truncate2)
**Output**
123
345

**Pow built-in.** Exponentiation multiplies a number by itself a certain number of times. With math.pow we apply this operation. Math.pow is similar to the ** operator.

**But:** When math.pow is applied, the result is always converted to a float. This is not the case with the ** operator.

**Tip:** More examples of using the exponent operator are available on the numbers page.

Numbers: Square, Cube**Python program that uses math.pow**
import math*
# Use math.pow method.
*a = __math.pow__(2, 3)*
# Use operator.
*b = 2 ** 3*
# Print results.
*print(a)
print(b)
**Output**
8.0
8

**Sqrt.** In most programs, we do not need square roots. But when we do, the math.sqrt method is useful. It receives one argument. It returns the square root (in floating-point form).

**Tip:** If your program often uses square roots, a cache or lookup table may be helpful. You could memoize the result of math.sqrt for speed.

Memoize**Python program that uses math.sqrt**
import math
value1 = *9*
value2 = *16*
value3 = *100**
# Use sqrt method.
*print(__math.sqrt__(value1))
print(math.sqrt(value2))
print(math.sqrt(value3))
**Output**
3.0
4.0
10.0

**Pi, E.** You probably know the approximate values of E and pi. And you could specify these directly in a Python program. But with math.e and math.pi, we avoid this hassle.

**Python program that uses math.e, pi**
import math*
# This returns the value of e.
*print(__math.e__)*
# And this is pi.
*print(__math.pi__)
**Output**
2.718281828459045
3.141592653589793

**Abs built-in.** This is a built-in function in Python. We invoke it with one argument. Abs returns the absolute value of that argument.

abs

**Round.** This method rounds a number up or down. We call it with a float argument. We can specify how many digits past the decimal place we want to keep.

round**Compound interest.** Math is used in the real world. Please also check out the compound_interest Python implementation. It uses pow to compute an exponential function.

Compound Interest**Math is everywhere.** It is possible to directly compute mathematical functions. We could add methods that use arithmetic operators. But this adds complexity. It bloats programs.

**Instead,** we can use built-ins. These are found in the default and math modules. This approach is more effective. It is simpler. It makes programs easier to understand and maintain.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.