❮ Math Methods
Example
Find the Euclidean distance between one and two dimensional points:
# Import math Library
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))
The result will be:
2.0
9.486832980505138
Run Example »
Definition and Usage
The math.dist() method returns the Euclidean distance between two points (p and q), where p and q are the coordinates of that point.
Note: The two points (p and q) must be of the same dimensions.
Syntax
Parameter Values
p | Required. Specifies point 1 |
q | Required. Specifies point 2 |
Technical Details
A float value, representing the Euclidean distance between p and q |
3.8 |
❮ Math Methods
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Math module in Python contains a number of mathematical operations, which can be performed with ease using the module. math.dist() method in
Python is used to the Euclidean distance between two points p and q, each given as a sequence (or iterable) of coordinates. The two points must have the same dimension.
This method is new in Python version 3.8.
Syntax: math.dist(p, q)
Parameters:
p: A sequence or iterable of coordinates representing first point
q: A sequence or iterable of coordinates representing second pointReturns: the calculated Euclidean distance between the given points.
Code #1: Use of math.dist() method
import math
P = 3
Q = -8
eDistance = math.dist([P], [Q])
print(eDistance)
Code #2:
import math
Px = 3
Py = 7
Qx = -5
Qy = -9
eDistance = math.dist([Px, Py], [Qx, Qy])
print(eDistance)
P = [3, 6, 9]
Q = [1, 0, -2]
eDistance = math.dist(P, Q)
print(eDistance)
Output:
17.88854381999832 12.688577540449518Code #3:
import math
P = [3, 9, 7, 2, 4, 5]
Q = [-5, -3, -9, 0, 6, 2]
eDistance = math.dist(P, Q)
print(eDistance)
Output:
21.93171219946131Reference: Python math library
Write a Python program to compute the distance between the points (x1, y1) and (x2, y2). Pictorial Presentation: Sample Solution:- Python Code: Sample Output: Flowchart: The following tool visualize what the computer is doing step-by-step as it executes the said program: Python
Code Editor: Have another way to solve this solution? Contribute your code (and comments) through Disqus. Previous: Write a Python program to compute the future value of a specified principal amount, rate of interest, and a number of years.Python Basic: Exercise-40 with Solution
Visualize Python code execution:
Next:
Write a Python program to check whether a file exists.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Given each iterable to construct a tuple by adding an index:
>>> a = ['Hello', 'world', '!'] >>> list(enumerate(a)) [(0, 'Hello'), (1, 'world'), (2, '!')]- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation