Python Number functions


Here’s a list of commonly used numeric functions in Python, many of which are part of the built-in math module or native Python functions:

Native Python Numeric Functions

  1. abs(x): Returns the absolute value of a number x.

    abs(-5) # Output: 5
  2. round(x, n): Rounds a number x to n decimal places.

    round(3.14159, 2) # Output: 3.14
  3. max(iterable, *args): Returns the largest item in an iterable or the largest of two or more arguments.

    max(1, 5, 3) # Output: 5
  4. min(iterable, *args): Returns the smallest item in an iterable or the smallest of two or more arguments.

    min(1, 5, 3) # Output: 1
  5. sum(iterable, start=0): Returns the sum of all items in an iterable, starting with an initial value (default is 0).

    sum([1, 2, 3]) # Output: 6
  6. pow(x, y, z=None): Returns x raised to the power of y; if z is provided, it computes (x**y) % z.

    pow(2, 3) # Output: 8
  7. divmod(x, y): Returns a tuple (quotient, remainder) when dividing x by y.

    divmod(9, 4) # Output: (2, 1)
  8. int(x, base=10): Converts x to an integer, optionally specifying the base (default is base 10).

    int("101", 2) # Output: 5
  9. float(x): Converts x to a floating-point number.

    float(10) # Output: 10.0
  10. complex(real, imag): Returns a complex number with the specified real and imaginary parts.

    complex(2, 3) # Output: (2+3j)

math Module Functions

To access these functions, you need to import the math module.

import math
  1. math.sqrt(x): Returns the square root of x.

    math.sqrt(16) # Output: 4.0
  2. math.ceil(x): Rounds a number x upward to the nearest integer.

    math.ceil(4.2) # Output: 5
  3. math.floor(x): Rounds a number x downward to the nearest integer.

    math.floor(4.8) # Output: 4
  4. math.factorial(x): Returns the factorial of x.

    math.factorial(5) # Output: 120
  5. math.fabs(x): Returns the absolute value of x as a float.

    math.fabs(-5) # Output: 5.0
  6. math.gcd(x, y): Returns the greatest common divisor of x and y.

    math.gcd(48, 18) # Output: 6
  7. math.log(x, base=math.e): Returns the logarithm of x to the given base. If no base is specified, it defaults to the natural logarithm.

    math.log(100, 10) # Output: 2.0
  8. math.exp(x): Returns the value of e raised to the power of x.

    math.exp(2) # Output: 7.3890560989306495
  9. math.log10(x): Returns the base-10 logarithm of x.

    math.log10(1000) # Output: 3.0
  10. math.log2(x): Returns the base-2 logarithm of x.

    math.log2(16) # Output: 4.0
  11. math.degrees(x): Converts an angle x from radians to degrees.

    math.degrees(math.pi) # Output: 180.0
  12. math.radians(x): Converts an angle x from degrees to radians.

    math.radians(180) # Output: 3.141592653589793
  13. math.sin(x): Returns the sine of x (where x is in radians).

    math.sin(math.pi / 2) # Output: 1.0
  14. math.cos(x): Returns the cosine of x (where x is in radians).

    math.cos(math.pi) # Output: -1.0
  15. math.tan(x): Returns the tangent of x (where x is in radians).

    math.tan(math.pi / 4) # Output: 1.0

random Module (for random number generation)

The random module provides functions for generating random numbers.

import random
  1. random.random(): Returns a random float between 0.0 and 1.0.

    random.random() # Output: 0.7365982448189378 (example)
  2. random.randint(a, b): Returns a random integer between a and b, inclusive.

    random.randint(1, 10) # Output: 7 (example)
  3. random.uniform(a, b): Returns a random float between a and b.

    random.uniform(1, 10) # Output: 7.239812 (example)
  4. random.choice(sequence): Returns a random element from the non-empty sequence (like a list or string).

    random.choice([1, 2, 3, 4]) # Output: 2 (example)
  5. random.sample(population, k): Returns a list of k unique random elements from the population sequence.

    random.sample(range(100), 5) # Output: [47, 35, 62, 21, 16] (example)

Summary

Python provides a wide range of numeric functions, both built-in and through specialized modules like math and random. These functions cover essential mathematical operations, random number generation, and higher-level mathematical operations like logarithms and trigonometry.