Introduction Python math.hypot() Method with Examples

Python hypot() is an inbuilt function that calculates the hypotenuse of a right triangle. It’s a method of math module. hypot(x,y): x,y are the two sides of the right triangle, hypotenuse = √x * x + y * y.

Python math.hypot()

Syntax

math.hypot(x, y)


Parameter(s):

  • x, y – numbers to calculate Euclidean norm.

Return Value

  • It returns a value of float data type, which is the calculated result of the Euclidean form. If the number argument is the positive integer and Negative integer, hypot function returns the output. If a number argument is not the number, hypot function return TypeError.

Examples of Python math.hypot() method

Example 1

x = 2
y = 3

# function call
print(math.hypot(x,y))


Output


3.605551275463989

Example 2

Python code to demonstrate example of math.hypot() method


# python code to demonstrate example of 
# math.hypot() method 

# importing math module
import math 

# numbers
x = 2
y = 3
print(math.hypot(x,y))

x = 2.3
y = 3.34
print(math.hypot(x,y))

x = 0
y = 3
print(math.hypot(x,y))

x = 2
y = 0
print(math.hypot(x,y))

x = -2
y = 3
print(math.hypot(x,y))

x = -2
y = -3
print(math.hypot(x,y))


Output


3.605551275463989
4.055317496818221
3.0
2.0
3.605551275463989
3.605551275463989

Example 3:

Try to pass 1 integer value and 1 character value as the parameter of hypot() function and display the result.


x1 = 3.5
y1 = 'b'
print(math.hypot(x1, y1))

Output


TypeError: must be real number, not str


In this example, we can see that while passing one integer and one character parameter, we get a type error. Both the parameters must be real numbers.

Example 4:

Python hypot() with list and tuple


import math

Tup = (11, 21, 3, -46, 5)
Lis = [-11, 21, -3.5, -46, 7.5]

print('Python HYPOT value of Positive Number = %.2f' % math.hypot(3, 4))
print('Python HYPOT value of Negative Number = %.2f' % math.hypot(3, -4))

print('Python HYPOT value of Tuple Item = %.2f' % math.hypot(Tup[2], Tup[3]))
print('Python HYPOT value of List Item = %.2f' % math.hypot(Lis[3], Lis[4]))

print('Python HYPOT value of Multiple Number = %.2f' %
      math.hypot(3 + 6 - 4, 9 - 5))

print('Python HYPOT value of String Number = %.2f',
      math.hypot('AppDividend', 'Python'))

In the above code, we have defined a Python list and tuple.

We have passed some elements of tuple and list to the math.hypot() function. If we pass the string as a parameter to the function, then it will return an error.

Output


python app.py
Python HYPOT value of Positive Number = 5.00
Python HYPOT value of Negative Number = 5.00
Python HYPOT value of Tuple Item = 46.10
Python HYPOT value of List Item = 46.61
Python HYPOT value of Multiple Number = 6.40
Traceback (most recent call last):
  File "app.py", line 16, in <module>
    math.hypot('AppDividend', 'Python'))
TypeError: a float is required


Python math method hypot() return the Euclidean norm, sqrt(xx + yy).

Thanks for reading !

#python #programming

Introduction Python math.hypot() Method with Examples
9.05 GEEK