Python math.dist() – Distance between Two Points

Contents

Python math.dist()

math.dist(p, q) function returns the distance between the points p and q.

The points: p and q; must be a sequence or iterable of coordinates with same dimension.

Syntax

The syntax to call dist() function is

math.dist(p, q)

where

ParameterRequiredDescription
pYesA sequence or iterable representing a Point.
qYesA sequence or iterable representing a Point.

Examples

In the following example, we find the distance between the points p(2, 3) and q(7, 6) using dist() function.

Python Program

import math

p = (2, 3)
q = (7, 6)
result = math.dist(p, q)
print('dist(p, q) :', result)
Run Code Copy

Output

dist(p, q) : 5.8309518948453

We have taken points in 2-dimensional coordinate system and found the distance between two points. Similarly, we can take points in N-dimensional coordinate system and find the distance between them using dis() function.

In the following program, we find the distance between the points: p(1, 1, 1, 1) and q(3, 5, 5, 6), where the points are in 4-dimensional coordinate system.

Python Program

import math

p = (1, 1, 1, 1)
q = (3, 5, 5, 6)
result = math.dist(p, q)
print('dist(p, q) :', result)
Run Code Copy

Output

dist(p, q) : 7.810249675906656

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.dist() function.

Code copied to clipboard successfully 👍