How to write a Python program for the square root of a number?

To compute the square root of a number in Python, you can use the sqrt() function from the math module of the Python standard library. The sqrt() function takes a number as an argument and returns its square root.

Here’s an example of how to use the sqrt() function to compute the square root of a number:

import math

def square_root(x):
    return math.sqrt(x)

print(square_root(4))  # Output: 2.0
print(square_root(9))  # Output: 3.0
print(square_root(16))  # Output: 4.0

In this example, we import the math module and define the square_root() function that takes a number x as an argument and returns its square root. The function uses the sqrt() function from the math module to compute the square root of x. Then, it returns the result.

the sqrt() function returns a float, even if the input is an integer. To get the result as an integer, you can use the int() function to cast the result to an integer. For example:

import math

def square_root(x):
    return int(math.sqrt(x))

print(square_root(4))  # Output: 2
print(square_root(9))  # Output: 3
print(square_root(16))  # Output: 4

How to write a Python program for the square root of a complex number

To compute the square root of a complex number in Python, you can use the cmath module of the Python standard library, which provides complex math functions and operations. The cmath module has a sqrt() function that takes a complex number as an argument and returns its square root.

Here’s an example of how to use the sqrt() function from the cmath module to compute the square root of a complex number in Python:

import cmath

def square_root(z):
    return cmath.sqrt(z)

print(square_root(1 + 1j))  # Output: (1.0986841134678098+0.45508986056222734j)
print(square_root(2 + 3j))  # Output: (1.9318516525781366+1.0344418537320665j)
print(square_root(4 - 5j))  # Output: (2.357022603955158-1.4907119849998599j)

In the above Python program for the square root of a complex number example, we import the cmath module and define the square_root() function that takes a complex number z as an argument and returns its square root. The function uses the sqrt() function from the cmath module to compute the square root of z. Then, it returns the result.

To test the function, we call it with different values of z and print the results to the console.

Keep in mind that the sqrt() function from the cmath module returns a complex number, even if the input is a real number. To get the result as a real number, you can use the real attribute of the result to get its real part. For example:

import cmath

def square_root(z):
    return cmath.sqrt(z).real

print(square_root(4))  # Output: 2.0
print(square_root(9))  # Output: 3.0
print(square_root(16))  # Output: 4.0

In the above code example, we import the cmath module and define the square_root() function that takes a complex number z as an argument and returns its square root as a real number. The function uses the sqrt() function from the cmath module to compute the square root of z and the real attribute to get its real part. Then, it returns the result.

Leave a Comment