-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Math 1
More file actions
55 lines (46 loc) · 1.98 KB
/
Python Math 1
File metadata and controls
55 lines (46 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
math
The Python math module provides a comprehensive collection of mathematical functions and constants, making it a go-to tool for performing mathematical operations in Python. It includes functions for trigonometry, logarithms, powers, and other common mathematical calculations.
Here’s a quick example:
>>> import math
>>> math.sqrt(16)
4.0
Key Features
Provides mathematical constants, such as π (pi) and e
Offers a range of functions for trigonometry, logarithms, and more
Supports operations for floating-point numbers
Frequently Used Classes and Functions
Object Type Description
math.pi Constant The mathematical constant π
math.e Constant The base of natural logarithms, e
math.sqrt() Function Computes the square root of a number
math.sin() Function Computes the sine of a number (radians)
math.cos() Function Computes the cosine of a number (radians)
math.log() Function Computes the natural logarithm (base e)
Examples
Calculate the sine of π/2:
>>> import math
>>> math.sin(math.pi / 2)
1.0
Compute the logarithm of 10:
>>> math.log(10)
2.302585092994046
Find the square root of 25:
>>> math.sqrt(25)
5.0
Common Use Cases
Performing trigonometric calculations for scientific and engineering applications
Calculating logarithms and exponentials for data analysis
Using mathematical constants in formulas
Real-World Example
Imagine you’re working on a physics simulation and need to calculate the trajectory of a projectile. The math module can help compute the necessary trigonometric values to determine its path:
>>> import math
>>> velocity = 20 # m/s
>>> angle = 45 # degrees
>>> angle_rad = math.radians(angle)
>>> horizontal_velocity = velocity * math.cos(angle_rad)
>>> vertical_velocity = velocity * math.sin(angle_rad)
>>> horizontal_velocity
14.142135623730951
>>> vertical_velocity
14.14213562373095
In this example, the math module is used to convert degrees to radians and compute the horizontal and vertical components of the velocity, demonstrating its utility in physics calculations.