Way 1 – Using Power Operator
This program prompts the user to provide a number, calculates the square root using the power operator, and displays the result.
#typcast input to float
number = float(input("Please enter number : "))
#Calculate Square root
square_root = number ** 0.5
#Display the result
print("Square root = ",square_root)
We can use the power operator (**) in Python to calculate the square root of a number by raising it to the power of 0.5.
In this program, we prompt the user to enter a number using the input() function, and the input value is converted to a floating-point number using the float() function.
Then, we calculate the square root of the number by raising the number to the power of 0.5 using the power operator **.
At last, we use the print() function to display the square root of the number saved in the square_root variable.
Way 2 – Using Math module
The program calculates the square root of the number provided by the user using the math module and displays the result.
import math
number = float(input("Please enter number :"))
#using math.sqrt
square_root = math.sqrt(number)
print("Square Root = ", square_root)
In this program, we first import the math module, which provides various mathematical functions including sqrt() for calculating the square root.
Then, we prompt the user to enter a number using the input() function, and the input value is converted to a floating-point number using the float() function.
Next, we calculate the square root of the number using math.sqrt(), and store the result in the variable square_root.
Finally, we use the print() function to display the square root of the number.