Numeric Functions

Let us understand some of the common numeric functions we use with Python as programming language.

  • We have functions even for standard operators such as +, -, *, / under a library called as operator. However, we use operators more often than functions.

    • add for +

    • sub for -

    • mul for *

    • truediv for /

  • We can use pow for getting power value.

  • We also have math library for some advanced mathematical operations.

  • Also, we have functions such as min, max to get minimum and maximum of the numbers passed.

4 + 5
Copy to clipboard
9
Copy to clipboard
5 % 4
Copy to clipboard
1
Copy to clipboard
import operator
from operator import add, sub, mul, truediv
Copy to clipboard
add?
Copy to clipboard
Docstring: add(a, b) -- Same as a + b.
Type:      builtin_function_or_method
Copy to clipboard
add(4, 5)
Copy to clipboard
9
Copy to clipboard
truediv(4, 5)
Copy to clipboard
0.8
Copy to clipboard
from operator import mod

mod(5, 4)
Copy to clipboard
1
Copy to clipboard
pow(2, 3) # This is also available under math library
Copy to clipboard
8
Copy to clipboard
pow?
Copy to clipboard
Signature: pow(x, y, z=None, /)
Docstring:
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)

Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
Type:      builtin_function_or_method
Copy to clipboard
import math
Copy to clipboard
math.pow?
Copy to clipboard
Docstring:
pow(x, y)

Return x**y (x to the power of y).
Type:      builtin_function_or_method
Copy to clipboard
math.ceil(4.4)
Copy to clipboard
5
Copy to clipboard
math.floor(4.7)
Copy to clipboard
4
Copy to clipboard
round(4.4)
Copy to clipboard
4
Copy to clipboard
round(4.7)
Copy to clipboard
5
Copy to clipboard
round(4.662, 2)
Copy to clipboard
4.66
Copy to clipboard
math.sqrt(2)
Copy to clipboard
1.4142135623730951
Copy to clipboard
math.pow(2, 3)
Copy to clipboard
8.0
Copy to clipboard
min?
Copy to clipboard
Docstring:
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
Type:      builtin_function_or_method
Copy to clipboard
min(2, 3)
Copy to clipboard
2
Copy to clipboard
max(2, 3, 5, 1)
Copy to clipboard
5
Copy to clipboard