General Python

Python Program to Find the Square Root1 min read

Write a program to Find the Square Root in Python

Python 3 Code:


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 num.

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 num_sqrt.

Finally, the 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 and 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.

Leave a Comment