Python math.sqrt() Method

Musfirah Waseem Jan 30, 2023
  1. Syntax of Python math.sqrt() Method
  2. Example 1: Use the math.sqrt() Method in Python
  3. Example 2: Errors When Using math.sqrt() Method
  4. Example 3: Alternative of the math.sqrt() Method
  5. Example 4: Implement the math.sqrt() Method in Applications
Python math.sqrt() Method

Python math.sqrt() method calculates the square root of the number x. Note that the input parameter must be greater than or equal to 0.

Syntax of Python math.sqrt() Method

math.sqrt(x)

Parameters

x Any positive number or 0 that needs to be computed for the square root.

Return

This method returns a floating point value representing the square root of a number.

Example 1: Use the math.sqrt() Method in Python

import math

x = 2

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

x = 0

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

x = math.inf

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

x = 198.67

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

Output:

The square root of 2 is 1.4142135623730951.
The square root of 0 is 0.0.
The square root of inf is inf.
The square root of 198.67 is 14.09503458669045.

Note that the values may only be positive, and the arguments can be in integers or float.

The square root of the number x < 0 does not exist.

Example 2: Errors When Using math.sqrt() Method

import math

x = -4

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

x = "h"

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

x = 2 + 8j

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

x = [2, 3, 4]

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

Output:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    value=math.sqrt(x)
ValueError: math domain error
Traceback (most recent call last):
  File "main.py", line 11, in <module>
    value=math.sqrt(x)
TypeError: must be real number, not str
Traceback (most recent call last):
  File "main.py", line 17, in <module>
    value=math.sqrt(x)
TypeError: can't convert complex to float
Traceback (most recent call last):
  File "main.py", line 23, in <module>
    value=math.sqrt(x)
TypeError: must be real number, not list

These methods have a certain application in astronomical computations and are also used in mathematical computations related to geometry.

Example 3: Alternative of the math.sqrt() Method

import math

x = 4

value = math.sqrt(x)

print(f"The square root of {x} is {value}.")

y = 4 ** 0.5

print(f"The square root of {x} using multiplication is {y}.")

z = math.pow(4, 0.5)

print(f"The square root of {x} using the power method is {z}.")

Output:

The square root of 4 is 2.0.
The square root of 4 using multiplication is 2.0.
The square root of 4 using the power method is 2.0.

Any of the above-stated methods can be used to find the square root of a number.

Example 4: Implement the math.sqrt() Method in Applications

import math


def prime(x):

    if x == 1:

        return False

    for i in range(2, (int)(math.sqrt(x)) + 1):

        if x % i == 0:

            return False

    return True


x = 23
if prime(x):

    print(f" {x} is a prime number.")

else:

    print(f" {x} is not a prime number.")

y = 6
if prime(y):

    print(f" {y} is a prime number.")

else:

    print(f" {y} is not a prime number.")

Output:

 23 is a prime number.
 6 is not a prime number.

The above program is just one of the examples of the applications of this method.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python Math