## numpy.sqrt#

Return the non-negative square-root of an array, element-wise.

Parameters : **x** array_like

The values whose square-roots are required.

**out** ndarray, None, or tuple of ndarray and None, optional

A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.

**where** array_like, optional

This condition is broadcast over the input. At locations where the condition is True, the *out* array will be set to the ufunc result. Elsewhere, the *out* array will retain its original value. Note that if an uninitialized *out* array is created via the default out=None , locations within it where the condition is False will remain uninitialized.

****kwargs**

For other keyword-only arguments, see the ufunc docs .

Returns : **y** ndarray

An array of the same shape as *x*, containing the positive square-root of each element in *x*. If any element in *x* is complex, a complex array is returned (and the square-roots of negative reals are calculated). If all of the elements in *x* are real, so is *y*, with negative elements returning nan . If *out* was provided, *y* is a reference to it. This is a scalar if *x* is a scalar.

A version which returns complex numbers when given negative reals. Note that 0.0 and -0.0 are handled differently for complex inputs.

*sqrt* has–consistent with common convention–as its branch cut the real “interval” [*-inf*, 0), and is continuous from above on it. A branch cut is a curve in the complex plane across which a given complex function fails to be continuous.

>>> np.sqrt([1,4,9]) array([ 1., 2., 3.])

>>> np.sqrt([4, -1, -3+4J]) array([ 2.+0.j, 0.+1.j, 1.+2.j])

>>> np.sqrt([4, -1, np.inf]) array([ 2., nan, inf])

## numpy.sqrt¶

Return the positive square-root of an array, element-wise.

Parameters :**x** : array_like

The values whose square-roots are required.

**out** : ndarray, optional

Alternate array object in which to put the result; if provided, it must have the same shape as *x*

**y** : ndarray

An array of the same shape as *x*, containing the positive square-root of each element in *x*. If any element in *x* is complex, a complex array is returned (and the square-roots of negative reals are calculated). If all of the elements in *x* are real, so is *y*, with negative elements returning `nan` . If *out* was provided, *y* is a reference to it.

`lib.scimath.sqrt` A version which returns complex numbers when given negative reals.

*sqrt* has–consistent with common convention–as its branch cut the real “interval” [*-inf*, 0), and is continuous from above on it. A branch cut is a curve in the complex plane across which a given complex function fails to be continuous.

>>> np.sqrt([1,4,9]) array([ 1., 2., 3.])

>>> np.sqrt([4, -1, -3+4J]) array([ 2.+0.j, 0.+1.j, 1.+2.j])

>>> np.sqrt([4, -1, numpy.inf]) array([ 2., NaN, Inf])

## NumPy sqrt()

The sqrt() function computes the square root of each element in an array.

#### Example

`import numpy as np array1 = np.array([4, 9, 16, 25]) # compute square root of each element in array1 result = np.sqrt(array1) print(result) # Output: [2. 3. 4. 5.]`

### sqrt() Syntax

The syntax of sqrt() is:

`numpy.sqrt(array, out=None, where=True)`

### sqrt() Arguments

The sqrt() function takes following arguments:

- array — the input array for which the square root is computed
- out (optional) — the output array where the result will be stored
- where (optional) — condition specifying which elements should be updated

### sqrt() Return Value

The sqrt() function returns an array that contains the square root of each element in the input array.

### Example 1: Find Square Root of Array Elements

`import numpy as np array1 = np.array([36, 49, 100, 256]) # compute square root of each element in array1 result = np.sqrt(array1) print(result)`

**Output**

[ 6. 7. 10. 16.]

In the above example, we have used the sqrt() function to compute the square root of each element in array1 .

In our case, the square root of **36** is **6**, the square root of **49** is **7**, the square root of **100** is **10**, and the square root of **256** is **16**. So the resulting array is [ 6. 7. 10. 16.] .

**Note**: Even though the resulting values are floating-point numbers, they are the exact square roots of the corresponding integers in the input array

### Example 2: Use out to Store Result in a Desired Array

`import numpy as np # create a 2-D array array1 = np.array([[4, 9, 16], [25, 36, 49]]) # create an empty array with the same shape as array1 result = np.zeros_like(array1, dtype=float) # calculate the square root of each element in array1 and store the result in result np.sqrt(array1, out=result) print(result)`

**Output**

[[2. 3. 4.] [5. 6. 7.]]

Here, sqrt() is used with the out argument set to result . This ensures that the result of calculating the square root is stored in result .

### Example 3: Use where to Compute Square Root of Filtered Array

`import numpy as np array1 = np.array([4, 9, 16, 25]) # compute the square root of each element in array1 # where the element is odd (arr%2==1) result = np.sqrt(array1, where=(array1 % 2 == 1)) print(result)`

**Output**

[0. 3. 0. 5.]

## .sqrt()

In NumPy, the **.sqrt()** method is used to calculate the positive square root of a number or the elements of an array. It is commonly employed in mathematical computations such as solving quadratic equations, applying the Pythagorean Theorem, modelling normal distributions, and more.

### Syntax

`numpy.sqrt(array, out=None, where=True)`

- array : A number or array-like structure containing the elements to which the method is to be applied.
- out (Optional): The array where the result is to be stored. If not provided, a new array is created and used for storing the results.
- where (Optional): The condition (array of boolean values) that determines the elements on which the method is to be applied.
- If the condition is True for a particular element, the square root is computed for that element.
- If the condition is False for a particular element, the square root is not computed for that element and the original element is retained.
- If not provided, the square root is computed for all elements.

### Example

The below example shows the .sqrt() method in use:

# Importing the 'numpy' library as 'np'import numpy as np# Computing the square root of only those elements in the array which is greater than or equal to 5result = np.sqrt([9,-4,25], where=np.array([9,-4,25]) >= 5)print(result)The output of the above code is shown below:

[3.00000000e+000 6.50227506e-310 5.00000000e+000]### Codebyte Example

In this codebyte example, the .sqrt() method only computes the square root of the elements of the array which are greater than 0:

usVisit uscodeHide codeCode

OutputHide outputHide outputLoading.