Home » Python

math.sqrt() method with example in Python

Python math.sqrt() method: Here, we are going to learn about the math.sqrt() method with example in Python.
Submitted by IncludeHelp, on April 18, 2019

Python math.sqrt() method

math.sqrt() method is a library method of math module, it is used to find the square root of a given number, it accepts a positive number (integer or float) and returns square root.

Note:

  • If the given number is a negative number, it returns a "ValueEroor""ValueError: math domain error".
  • If we provide anything like string, except a number, it also returns a "ValueError""TypeError: a float is required".

Syntax of math.sqrt() method:

    math.sqrt(n)

Parameter(s): n – a number whose square root needs to be calculated.

Return value: float – it returns a float value that is the square root of given number n.

Example:

    Input:
    a = 2

    # function call
    print(math.sqrt(a))

    Output:
    1.4142135623730951

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

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

# importing math module
import math 

# numbers
a = 2
b = 12345
c = 10.21
d = 0
e = 0.0

# finding square roots of the numbers
print("square root of ", a, " is = ", math.sqrt(a))
print("square root of ", b, " is = ", math.sqrt(b))
print("square root of ", c, " is = ", math.sqrt(c))
print("square root of ", d, " is = ", math.sqrt(d))
print("square root of ", e, " is = ", math.sqrt(e))

Output

square root of  2  is =  1.4142135623730951
square root of  12345  is =  111.1080555135405
square root of  10.21  is =  3.1953090617340916
square root of  0  is =  0.0
square root of  0.0  is =  0.0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.