Python sqrt() Method with Examples

sqrt() Method in Python:

The square root of a number is returned by the math.sqrt() method.

Note: It should be noted that the number must be greater than or equal to 0.

Syntax:

math.sqrt(number)

Parameters

number: This is Required. A number whose square root is to be found.

  • If the number is less than zero, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

Return Value:

Returns a float value that represents a number’s square root.

Examples:

Example1:

Input:

Given Number = 36

Output:

The square root of a given number{ 36 } =  6.0

Example2:

Input:

Given Number = 225

Output:

The square root of a given number{ 225 } =  15.0

sqrt() Method with Examples in Python

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

Approach:

  • Import math module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the math.sqrt() function to get the square root value of a given number.
  • Store it in another variable.
  • Print the square root value of a given number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the number as static input and store it in a variable.
gvn_numb = 36
# Pass the given number as an argument to the math.sqrt() function to get the
# square root value of a given number.
# Store it in another variable.
squre_rootrslt = math.sqrt(gvn_numb)
# Print the square root value of a given number.
print("The square root of a given number{", gvn_numb, "} = ", squre_rootrslt)

Output:

The square root of a given number{ 36 } =  6.0
Similarly Check out for other numbers
import math
gvn_numb = -25
squre_rootrslt = math.sqrt(gvn_numb)
print("The square root of a given number{", gvn_numb, "} = ", squre_rootrslt)

Output:

Traceback (most recent call last):
  File "/home/47ab19f64e175f148c2a3f48359df760.py", line 3, in <module>
    squre_rootrslt = math.sqrt(gvn_numb)
ValueError: math domain error
import math
gvn_numb = 45.5
squre_rootrslt = math.sqrt(gvn_numb)
print("The square root of a given number{", gvn_numb, "} = ", squre_rootrslt)

Output:

The square root of a given number{ 45.5 } =  6.745368781616021

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

Approach:

  • Import math module using the import keyword.
  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the math.sqrt() function to get the square root value of a given number.
  • Store it in another variable.
  • Print the square root value of a given number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the number as user input using the int(input()) function
# and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given number as an argument to the math.sqrt() function to get the
# square root value of a given number.
# Store it in another variable.
squre_rootrslt = math.sqrt(gvn_numb)
# Print the square root value of a given number.
print("The square root of a given number{", gvn_numb, "} = ", squre_rootrslt)

Output:

Enter some random number = 225
The square root of a given number{ 225 } = 15.0