[python] Understand the math module in one hour, take you from entry to proficiency

Python's math library provides many mathematical functions, including trigonometric functions, exponential functions, logarithmic functions, constants, and more. The following is a summary of the knowledge points of the math library:

  1. constant

The math library provides some constants, such as π, the natural constant e, and so on. These constants can be obtained through constant variables in the math library.

import math

print(math.pi)  # 输出π
print(math.e)   # 输出自然常数e
  1. Trigonometric functions

The math library provides some trigonometric functions, such as sin, cos, tan, etc. The arguments to these functions can be in radians or degrees.

import math

# 弧度制
print(math.sin(0))      # 输出0.0
print(math.cos(math.pi))# 输出-1.0
print(math.tan(math.pi/4))# 输出1.0

# 角度制
print(math.sin(math.radians(0)))      # 输出0.0
print(math.cos(math.radians(180)))   # 输出-1.0
print(math.tan(math.radians(45)))    # 输出1.0
  1. inverse trigonometric functions

The math library provides some inverse trigonometric functions, such as asin, acos, atan, etc. These functions return values ​​in radians.

import math

print(math.asin(0.5))   # 输出0.5235987755982989
print(math.acos(0.5))   # 输出1.0471975511965979
print(math.atan(1))     # 输出0.7853981633974483
  1. Exponential and logarithmic functions

The math library provides exponential functions and logarithmic functions, such as exp, log, log10, etc.

import math

print(math.exp(1))      # 输出2.718281828459045
print(math.log(10))     # 输出2.302585092994046
print(math.log10(100))  # 输出2.0
  1. Power and square root functions

The math library provides power functions and square root functions, such as pow, sqrt, etc.

import math

print(math.pow(2, 3))   # 输出8.0
print(math.sqrt(4))     # 输出2.0
  1. rounding function

The math library provides rounding functions, such as ceil, floor, trunc, etc.

import math

print(math.ceil(1.1))   # 输出2
print(math.floor(1.9))  # 输出1
print(math.trunc(1.9))  # 输出1

Summarize:

The math library provides many mathematical functions, including constants, trigonometric functions, inverse trigonometric functions, exponential functions, logarithmic functions, power functions, square root functions, rounding functions, etc. Mathematical calculations can be conveniently performed using these functions.

Guess you like

Origin blog.csdn.net/qq_41604569/article/details/131306788