Write a program to Find the Square Root in Python
Python 3 Code:
# Python Program to calculate the square root
#to take the input from the user
num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
The code you provided is a simple Python program that calculates the square root of a number input by the user.
The program starts by using the
input() function to prompt the user to enter a number. The user’s input is stored as a string, so it needs to be converted to a float using the
float() function. This float value is stored in the variable
Next, the program calculates the square root of
num by raising it to the power of 0.5 (square root is the same as raising the number to the power of 0.5) and assigns the result to the variable
print() function is used to output the square root of the number to the console. The
% operator is used to format the output and insert the value of
num_sqrt into the string that is passed to the
print() function. The
%0.3f within the string is a format specifier, it tells Python to format the number as a floating-point number with 3 decimal places.
The output will depend on the input provided by the user, for example if the user inputs 8, the output will be “The square root of 8.000 is 2.828”.
Take your time to comment on this article.