Python 3 – Number hypot() Method
In Python 3, the hypot()
method belongs to the math
module, and it is used to calculate the value of the hypotenuse of a right-angled triangle. The hypotenuse is defined as the longest side of a right-angled triangle, and it is always opposite the right angle. The hypot()
method takes two parameters representing the two legs of the right-angled triangle and returns the value of the hypotenuse.
Syntax
The syntax of the hypot()
method is as follows:
import math
math.hypot(x, y)
Here, x
and y
represent the two legs of the right-angled triangle.
Return Value
The hypot()
method returns the value of the hypotenuse of the right-angled triangle represented by the two legs x
and y
.
Examples
Let’s take a look at some examples to understand the usage of the hypot()
method:
Example 1: Using hypot() method without importing the math module
a = 3
b = 4
# Calculate the value of hypotenuse using hypot() method
c = hypot(a, b)
# Display output
print("The value of hypotenuse is:", c)
Output:
---> NameError: name 'hypot' is not defined
If we try to use the hypot()
method without importing the math
module, we will get a NameError
because hypot
is not defined. Therefore, we need to first import the math
module before using the hypot()
method, as shown in the following example.
Example 2: Using hypot() method after importing the math module
import math
a = 3
b = 4
# Calculate the value of hypotenuse using hypot() method
c = math.hypot(a, b)
# Display output
print("The value of hypotenuse is:", c)
Output:
The value of hypotenuse is: 5.0
In this example, we first imported the math
module and then used the hypot()
method to calculate the value of the hypotenuse of the right-angled triangle with legs a=3
and b=4
. The output is 5.0
.
Example 3: Using hypot() method to calculate the distance between two points
The hypot()
method can also be used to calculate the distance between two points (x1, y1)
and (x2, y2)
in a two-dimensional coordinate system. The distance between two points can be calculated by finding the length of the hypotenuse of the right-angled triangle formed by the two points and the origin (0, 0)
.
import math
x1 = 2
y1 = 3
x2 = -4
y2 = -1
# Calculate the distance between two points
distance = math.hypot(x2-x1, y2-y1)
# Display output
print("The distance between the points ({}, {}) and ({}, {}) is: {:.2f}".format(x1, y1, x2, y2, distance))
Output:
The distance between the points (2, 3) and (-4, -1) is: 6.32
In this example, we used the hypot()
method to calculate the distance between two points (2, 3)
and (-4, -1)
in a two-dimensional coordinate system. The output is 6.32
.
Example 4: Using hypot() method to calculate the distance between two points in a three-dimensional coordinate system
The hypot()
method can also be used to calculate the distance between two points (x1, y1, z1)
and (x2, y2, z2)
in a three-dimensional coordinate system. The distance between two points can be calculated by finding the length of the longest side of the right-angled triangle formed by the two points and the origin (0, 0, 0)
.
import math
x1 = 2
y1 = 3
z1 = 4
x2 = -4
y2 = -1
z2 = 5
# Calculate the distance between two points
distance = math.hypot(math.hypot(x2-x1, y2-y1), z2-z1)
# Display output
print("The distance between the points ({}, {}, {}) and ({}, {}, {}) is: {:.2f}".format(x1, y1, z1, x2, y2, z2, distance))
Output:
The distance between the points (2, 3, 4) and (-4, -1, 5) is: 7.35
In this example, we used the hypot()
method to calculate the distance between two points (2, 3, 4)
and (-4, -1, 5)
in a three-dimensional coordinate system. The output is 7.35
.
Conclusion
The hypot()
method is a useful tool for calculating the value of the hypotenuse of a right-angled triangle, as well as the distance between two points in two-dimensional or three-dimensional coordinate systems. It is part of the math
module in Python 3, and can be easily imported and used in your Python programs.