Python math.dist() Method

Python math 模块Python math Module

Python math.dist(p, q)The method returns the Euclidean distance between two points p and q, given as a sequence (or iterable) of coordinates. The two points must have the same dimension.

The arguments passed must be positive integers.

Python Version: 3.8

Syntax

The syntax of the math.dist() method is as follows:

math.dist(p, q)

Parameter Description:

  • p-- Required, specifies the first point.
  • q-- Required, specifies the second point.

Return Value

Returns a floating-point value representing the Euclidean distance between p and q.

Example

The following example returns the Euclidean distance between two points:

Example

# Import math package
import math

p = [3]
q = [1]

# Calculate Euclidean distance
print (math.dist(p, q))

p = [3, 3]
q = [6, 12]

# Calculate Euclidean distance
print (math.dist(p, q))

Output:

2.0
9.486832980505138

Python math 模块Python math Module

Other Extensions