Func Number Sqrt
## Python sqrt() Function
The `math.sqrt()` method is a built-in function in Python's standard library that returns the square root of a given number $x$.
---
## Syntax
To use the `sqrt()` function, you must first import the `math` module. It cannot be called directly without importing the module.
```python
import math
math.sqrt(x)
```
### Parameters
* **`x`**: A numeric expression (integer or float) greater than or equal to 0.
### Return Value
* Returns a float representing the square root of the number `x`.
---
## Code Examples
The following example demonstrates how to use the `math.sqrt()` method with different types of numeric inputs:
```python
#!/usr/bin/python3
import math # Import the math module
# Square root of a perfect square
print("math.sqrt(100) : ", math.sqrt(100))
# Square root of a prime number
print("math.sqrt(7) : ", math.sqrt(7))
# Square root of a mathematical constant (pi)
print("math.sqrt(math.pi) : ", math.sqrt(math.pi))
```
### Output
Running the code above will produce the following output:
```text
math.sqrt(100) : 10.0
math.sqrt(7) : 2.6457513110645907
math.sqrt(math.pi) : 1.7724538509055159
```
---
## Important Considerations
### 1. Negative Numbers
The standard `math.sqrt()` function does not support negative numbers. If you pass a negative number as an argument, Python will raise a `ValueError`.
```python
import math
# This will raise a ValueError: math domain error
print(math.sqrt(-9))
```
**Solution for Complex Numbers:**
If you need to calculate the square root of a negative number (which results in a complex number), use the `cmath` (complex math) module instead:
```python
import cmath
print(cmath.sqrt(-9)) # Output: 3j
```
### 2. Return Type
Regardless of whether the input is an integer or a float, `math.sqrt()` always returns a floating-point number (`float`).
YouTip