~1 min read • Updated Oct 1, 2025
Program Overview
This Python program calculates the distance between two points in a 2D plane.
It uses the Euclidean distance formula:
distance = √((x₂ - x₁)² + (y₂ - y₁)²)
Python Code:
import math
# Read coordinates from user
x1 = float(input("Enter x₁: "))
y1 = float(input("Enter y₁: "))
x2 = float(input("Enter x₂: "))
y2 = float(input("Enter y₂: "))
# Calculate Euclidean distance
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
# Display result
print("\n--- Result ---")
print(f"Distance between the two points: {distance:.4f}")
Sample Output:
Enter x₁: 2
Enter y₁: 3
Enter x₂: 7
Enter y₂: 6
--- Result ---
Distance between the two points: 5.8300
Step-by-Step Explanation:
- The user enters coordinates for two points
- The program applies the Euclidean formula using math.sqrt()
- The result is printed with four decimal places
Written & researched by Dr. Shahin Siami