Python Program - Find the Square Root
here's a Python program that finds the square root of a number using the math
module:
import math # Define a number num = 25 # Find the square root using the math.sqrt() function sqrt_num = math.sqrt(num) # Print the square root print("The square root of", num, "is", sqrt_num)
In this program, we first import the math
module, which provides mathematical functions and constants.
We then define a number called num
that we want to find the square root of.
To find the square root of num
, we can use the math.sqrt()
function, which takes a number as its argument and returns its square root.
We assign the result of math.sqrt(num)
to a variable called sqrt_num
.
Finally, we can print the square root using the print()
function and string formatting.
If we run this program, the output will be:
The square root of 25 is 5.0
Note that the math.sqrt()
function only works with real numbers, and will raise a ValueError
exception if called with a negative argument. If you need to find the square root of a negative number, you can use the cmath.sqrt()
function from the cmath
module, which works with complex numbers.