Location>code7788 >text

Life in the Python World from Scratch - Built-in Modules (Math)

Popularity:789 ℃/2024-11-20 16:53:09

Life in the Python World from Scratch - Built-in Modules (Math)

PythonmathThe module provides a rich set of mathematical functions and constants to support basic mathematical operations, trigonometric functions, logarithms, exponents, etc. for scientific computing and engineering applications.

Mathematical Constants:

take note ofmathThe module's constants are stored as double-precision floating-point numbers, so they usually have only 15 to 17 digits of precision, and it is recommended to use thedecimal Module ormpmath storehouse

π (pi)

import math
print()
#Running result:3.141592653589793

τ(2π)

Some mathematicians advocate the use of τ instead of π, arguing that τ is more intuitive, especially when it comes to the angles and periodicity of circles

import math
print()
#Run result:6.283185307179586

e (Euler number)

import math
print()
#Run result:2.718281828459045

infinity

import math
print()
#Run the result:inf

Not a Number

Undefined or unrepresentable values, such as the result of dividing 0 by 0, the square root of a negative number (in the real number range), can be used to mark missing or invalid data points in data analysis and scientific calculations

import math
print()
#Run the result:nan

Mathematical Functions.

1. Basic mathematical functions.

(iterable) Returns the floating-point sum of the iterable objects compared to thesum(iterable)With higher precision.

import math
iterable=[0.1,0.2,0.3]
print((iterable))
#Run result:0.6

2. powers and logarithms

power function (math.)

  • (x, y): Returnx**y
  • (x): Returne**x

logarithmic function

  • (x[, base]): return logba**se(x), and returns the natural logarithm if no base is specified.
  • math.log10(x): Returns a logarithm with a base of 10.
  • math.log2(x): Returns the logarithm of the base 2.

3. Trigonometric functions

elementary trigonometric function

  • (x): return sin(x)。
  • (x): return cos(x)。
  • (x): return tan(x)。

inverse trigonometric function

  • (x): return arcsin(x)。
  • (x): return arccos(x)。
  • (x): return arctan(x)。
  • math.atan2(y, x): return arctan(y/x)。

4. Beyond functions

hyperbolic function

  • (x): return hyperbolic sine.
  • (x): Return to hyperbolic cosine.
  • (x): return hyperbolic tangent.

inverse hyperbolic function

  • (x): Returns the inverse hyperbolic sine.
  • (x): Returns the inverse hyperbolic cosine.
  • (x): return the inverse hyperbolic tangent.

5. Other functions

the factorial of a number, e.g. 5! = 5.4.3.2.1 = 120

(x): return the factorial of x

import math
x = 3
print((x))# run result :6

Integration and rounding

(x): Returns the smallest integer greater than or equal to x.

(x): Returns the largest integer less than or equal to x.

(x): Returns the integer part of x.

import math
x = 1.5
print((x))#Running result:2
print((x))#Run result:1
print((x))#Run result:1

Square roots and absolute values

(x): Returns the square root of x.

(x): Returns the integer part of the square root of x.

import math
x = 3
print((x))#Run result:1.7320508075688772
print((x))#Run result:1

(x)

Returns the absolute value of x.Note that the return is a floating point numberThe absolute value of a floating-point number is used to obtain the absolute value of an integer, using the built-in functionabs(x)

import math
x = -1
print((x)) #Run result:1.0
print(abs(x))#Run result:1

6. Combinations and permutations (available only in Python 3.8 and above)

(n, k): Returns the number of combinations that select k elements out of n elements.

(n, k): Returns the number of permutations of k elements out of n elements.

import math
n, k = 5, 3
print((n,k))#Running result:10
print((n,k))#Running result:60

The way to get started is right there.