AppDividend
Latest Code Tutorials

Python hypot() Function | Math.hypot() in Python Example

0

Python hypot() is an inbuilt method that is defined under the math module, which is returned Euclidean norm, which is sqrt(a*a+b*b). We can use a math module by importing it. The syntax for it would be import math; after importing, we use to call this function using the static object. It is an essential function as it reduces the complexities of the problem associated.

Euclidean norm can be defined as the length of the vector from the origin point to the given point here, suppose x and y.

Python hypot()

Content Overview

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.

Syntax

math.hypot(a,b)

where a and b are numeric variables.

Parameters

It takes two parameters a, b, which are numbers which we will use to find the Euclidean form. 

If any other type of values is passed ( suppose char), then it will throw a type error.

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.

Example program on hypot() functions in Python

Example 1: Write a program to show the working of hypot() function in Python.

import math

x1 = 3.5
y1 = 5.2
x2 = 6
y2 = 7
x3 = 9.2
y3 = 10
print("Euclidean norm for x=", x1, " y=", y1, " hypot=", math.hypot(x1, y1))
print("Euclidean norm for x=", x2, " y=", y2, " hypot=", math.hypot(x2, y2))
print("Euclidean norm for x=", x3, " y=", y3, " hypot=", math.hypot(x3, y3))

Output

Euclidean norm for x= 3.5  y= 5.2  hypot= 6.2681735776859275
Euclidean norm for x= 6  y= 7  hypot= 9.219544457292887
Euclidean norm for x= 9.2  y= 10  hypot= 13.588230201170422

In this example, we have seen that by passing a valid parameter in the hypot() function, we got our output as Euclidean form.

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

import math

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.

Python hypot() with list and tuple

See the following code.

# app.py

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

Conclusion

Python math method hypot() return the Euclidean norm, sqrt(x*x + y*y).

See also

Python atan()

Python atan2()

Python acos()

Python asin()

Python log(2)

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.