Python Program for hypot() Function

In the previous article, we have discussed Python Program for atanh() Function
hypot() Function in Python:

The square root of the sum of squares of the specified arguments is returned by the Python hypot() function.

The Euclidean norm is returned by the math.hypot() method. The Euclidian norm is the distance between the origin and the given coordinates.

This method was only used prior to Python 3.8 to find the hypotenuse of a right-angled triangle: sqrt(x*x + y*y).

Since Python 3.8, this method is also used to compute the Euclidean norm. The coordinates passed are assumed to be like in n-dimensional cases (x1, x2, x3, …, xn). So sqrt(x1*x1 + x2*x2 + x3*x3…. xn*xn) is used to calculate Euclidean length from the origin.

Syntax:

math.hypot(x1, x2, x3,. . . .xn )

Parameters:

x1, x2, x3,. . . . ,xn: These are required. These are numbers. Two or more coordinates are represented by two or more points.

Return Value:

For n inputs, the return value is a float representing the Euclidean distance from the origin, or the hypotenuse of a right-angled triangle for two inputs.

  • If the value argument is both a positive and a negative integer, the hypot() function returns the Output.
  • If it’s not a number, the hypot() function throws a TypeError.

Examples:

Example1:

Input:

Given first number = 2
Given second number = 3

Output:

The result after applying hypot() function on above given first and second { 2 , 3 } numbers =  3.605551275463989

Example2:

Input:

Given first number = 4  
Given second number = 3

Output:

The result after applying hypot() function on above given first and second { 4 , 3 } numbers =  5.0

Program for hypot() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the first number as static input and store it in another variable.
  • Give the second number as static input and store it in another variable.
  • Apply math.hypot() function on the given first and the second numbers to get the square root of the sum of squares of the specified arguments.i.e (given first and second numbers )
  • Store it in another variable.
  • Print the above result which is the result after applying math.hypot() function.
  • Give the number as static input and store it in another variable.
  • Apply math.hypot() function to the given list element and above-given number and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 4, 0]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 2
# Give the second number as static input and store it in another variable.
gvn_numb2 = 3
# Apply math.hypot() function on the given first and the second numbers to get
# the square root of the sum of squares of the specified arguments.i.e
# (given first and second numbers )
# Store it in another variable.
fnl_rslt = math.hypot(gvn_numb1, gvn_numb2)
# Print the above result which is the result after applying math.hypot() function.
print("The result after applying hypot() function on above given first and second {",
      gvn_numb1, ",", gvn_numb2, "} numbers = ", fnl_rslt)
# Give the number as static input and store it in another variable.
gvn_numb3 = 5
# Apply math.hypot() function to the given list element and above given number
# and print it.
print("The result after applying hypot() function on the given list element gvn_lst[1] and above given number = ", math.hypot(
    gvn_lst[1], gvn_numb3))

Output:

The result after applying hypot() function on above given first and second { 2 , 3 } numbers =  3.605551275463989
The result after applying hypot() function on the given list element gvn_lst[1] and above given number =  5.385164807134504

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give first and second numbers respectively as user input using int(), map(), input(), and split() functions.
  • Store them in two separate variables.
  • Apply math.hypot() function on the given first and the second numbers to get the square root of the sum of squares of the specified arguments.i.e (given first and second numbers )
  • Store it in another variable.
  • Print the above result which is the result after applying math.hypot() function.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Apply math.hypot() function to the given list element and above-given number and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give first and second numbers respectively as user input using 
# int(), map(), input(), and split() functions.
gvn_numb1, gvn_numb2 = map(int, input(
    "Enter two random numbers separated by spaces = ").split())
# Apply math.hypot() function on the given first and the second numbers to get
# the square root of the sum of squares of the specified arguments.i.e
# (given first and second numbers )
# Store it in another variable.
fnl_rslt = math.hypot(gvn_numb1, gvn_numb2)
# Print the above result which is the result after applying math.hypot() function.
print("The result after applying hypot() function on above given first and second {",
      gvn_numb1, ",", gvn_numb2, "} numbers = ", fnl_rslt)
# Give the number as user input using the int(input()) function and store it in another variable.
gvn_numb3 = int(input("Enter some random number = "))
# Apply math.hypot() function to the given list element and above given number
# and print it.
print("The result after applying hypot() function on the given list element gvn_lst[2] and above given number = ", math.hypot(
    gvn_lst[2], gvn_numb3))

Output:

Enter some random List Elements separated by spaces = 2 3 4 1 
Enter two random numbers separated by spaces = 4 6
The result after applying hypot() function on above given first and second { 4 , 6 } numbers = 7.211102550927978
Enter some random number = 3
The result after applying hypot() function on the given list element gvn_lst[2] and above given number = 5.0

Know all about Mathematical Functions in Python by going through our Python Mathematical Methods Examples well explained step by step.