| by Arround The Web | No comments

## Python Math Sqrt

Python is a high-level, executable, and general-purpose programming language that can be utilized for various applications. It is frequently utilized in ML/machine learning, data science, AI, and web development. One of the most commonly used mathematical functions in Python is the “sqrt()” method that can be utilized to compute the square root while dealing with mathematical computations.

This article will explore the Python “math.sqrt()” method in detail, covering its syntax, parameter values, return values, and exceptions, along with examples.

## What is the “math.sqrt()” Method in Python?

The “math.sqrt()” method is a built-in functionality in the “math” module of Python, which is utilized to calculate the square root of a given number. It takes/accepts a single argument, which is a number, and returns its square root.

Syntax of “math.sqrt()”

import math
math.sqrt(x)

In the above syntax, the “math” module is imported, and the “math.sqrt()” method is used to calculate the square root of the number “x”.

Parameter Values of “math.sqrt()”

The “math.sqrt()” method takes only one argument: a number. The particular number can be an “Int/Integer” or a “Float”. If the number is negative, the “math.sqrt()” method will return a complex number, which contains the absolute value of the square root.

Return Values of “math.sqrt()”

This method retrieves the input number’s square root. If the number is a positive integer or floating-point, the method will return a floating-point number. If the number is a negative integer or floating-point, the method will return a complex number instead.

## Examples of “math.sqrt()” in Python

Let’s look at the following examples of using the “math.sqrt()” method in Python:

Example 1: Determining the Square Root of a Positive Number

The below code is used to determine the square root value of the given number:

import math
x = 16
print(math.sqrt(x))

In the above code, the “math” module is imported, and a variable “x” with the value of “16” is defined, respectively. After that, the “math.sqrt()” method is called on “x” to return the square root accordingly.

Output

The snippet above displays the square root of the defined integer appropriately.

Example 2: Determining the Square Root of the Negative Number

The “math.sqrt()” method raises a “ValueError” exception if the passed argument value is negative. Here is an example:

import math
x = -16
print(math.sqrt(x))

In this code block, the “negative number” is passed as an argument to the “math.sqrt()” method to compute the square root accordingly.

Output

Based on the above snippet, the discussed exception is encountered.

## Conclusion

The “sqrt()” method of the “math” module in Python can be used to calculate the square root of a given number. This module also contains many other useful mathematical functions. The “math.sqrt()” method was discussed in this article along with its syntax, parameter values, return values, and exceptions.

Source: linuxhint.com