python math
时间: 2023-10-14 19:29:35 浏览: 107
Python has a built-in module called math that provides many mathematical functions. Here are some examples of using math in Python:
1. Importing the math module:
```python
import math
```
2. Using the math module for basic arithmetic operations:
```python
print(math.sqrt(25)) # Output: 5.0 (square root)
print(math.pow(2, 3)) # Output: 8.0 (2 raised to the power of 3)
print(math.exp(2)) # Output: 7.38905609893065 (e raised to the power of 2)
print(math.pi) # Output: 3.141592653589793 (pi)
print(math.e) # Output: 2.718281828459045 (e)
```
3. Using the math module for trigonometric functions:
```python
print(math.sin(math.pi/2)) # Output: 1.0 (sine of pi/2)
print(math.cos(math.pi)) # Output: -1.0 (cosine of pi)
print(math.tan(math.pi/4)) # Output: 0.9999999999999999 (tangent of pi/4)
```
4. Using the math module for logarithmic functions:
```python
print(math.log10(100)) # Output: 2.0 (log base 10 of 100)
print(math.log(math.e)) # Output: 1.0 (natural log of e)
```
These are just a few examples of the many functions available in the math module.
阅读全文