How to write square root in Python?

Python has a predefined sqrt() function that returns the square root of a number. It defines the square root of a value that multiplies itself to give a number. The sqrt() function is not used directly to find the square root of a given number, so we need to use a math module to call the sqrt() function in Python.

For example, the square root of 144 is 12.

Now, let's see the syntax of the square root function to find the square root of a given number in Python:

Syntax:

Parameters:

x : It is the number. in which the number should be greater than 0 and can be a decimal or integer.

Return:

The output is the square root value.

Note:

  • The output of the sqrt() method will be a floating-point value.
  • If the given input is a negative number, then the output will be a ValueError. ValueError is returned because any negative number's square root value is not considered a real number.
  • If the input is anything but a number, then the sqrt() function returns NaN.

Example:

The example usage of sqrt() function in Python.

Code

Output:

4.0  

How to write square root in Python

1. Using math.sqrt() method

The sqrt() function is an inbuilt function that returns the square root of any number. Following are the steps to find the square root of a number.

  1. Start the program
  2. Define any number whose square root to be found.
  3. Invoke the sqrt() function and pass the value that you have defined in step 2 and store the result in a variable.
  4. Print the square root.
  5. Terminate the program.

Python math.sqrt() method Example 1

Python example program to find the square root of a given integer number.

Code

Output:

Square root of number 36 is : 6.0
Square root of number 625 is : 25.0
Square root of number 144 is : 12.0
Square root of number 64 is : 8.0

Python math.sqrt() method Example 2

Let's create a python program that finds the square root of a decimal numbers.

Code

Output:

The Square root of 4.5 is 2.1213203435596424
The Square root of 627 is 25.039968051096054
The Square root of 6.25 is 2.5
The Square root of 0 is 0.0

Python math.sqrt() method Example 3

In the following program, we have read a number form the user and find the square root.

Code

Output:

Enter a number to get the Square root: 25
Square root of the number is: 5.0

1. Using math.pow() function

The pow() is an inbuilt function that is used in Python to return the power of a number. It has two parameters. The first parameter defines the number and second parameter defines the power raise to that number.

Python math.pow() method Example

Let's see an example program for math.pow() function:

Code

Output:

Enter the number :628 
The Square Root of the given number 628.0 = 25.059928172283335

3. Using Numpy module

NumPy module is also an option to find the square root in python.

Python Numpy Example

Let's see an example program to find the square root a given list of numbers in an array.

Code

Output:

Square roots of the given array are: [ 1. 2. 3. 4. 5. ]

4. Using ** Operator

We can also use the exponent operator to find the square root of the number. The operator can be applied between two operands. For example, x**y. It means that left operand raised to the power of right.

Following are the steps to find the square root of a number.

Step 1. Define a function and pass the value as an argument.

Step 2. If the defined number is less than 0 or negative, it returns nothing.

Step 3. Use the exponential ** sign to find the power of a number.

Step 4. Take the numeric value from the user.

Step 5. Call the function and store its output to a variable.

Step 6. Display the Square Root of a number in Python.

Step 7. Exit from the program.

Python ** Operator Example 1

Let's implement the above steps in a Python program and calculate the square root of a number.

Code

Output:

Enter a numeric value: 256 
Square Root of the 256 = 16.0

Explanation:

As we can see in the above example, first we take an input (number) from the user and then use the exponent ** operator to find out the power of a number. Where 0.5 is equal to √ (root symbol) to raise the power of a given number. At last, the code prints the value of the num and the comparing square root esteem utilizing the format() function. On the off chance that the client inputs a negative number, the capability will not return anything and the result will be clear.

Python ** Operator Example 2

Let's create a Python program that finds the square root of between the specified range. In the following program, we have found the square root of all the number between 0 to 30.

Code

Output:

Square root of a number 0 = 0.0
Square root of a number 1 = 1.0
Square root of a number 2 = 1.4142135623730951
Square root of a number 3 = 1.7320508075688772
Square root of a number 4 = 2.0
Square root of a number 5 = 2.23606797749979
Square root of a number 6 = 2.449489742783178
Square root of a number 7 = 2.6457513110645907
Square root of a number 8 = 2.8284271247461903
Square root of a number 9 = 3.0
Square root of a number 10 = 3.1622776601683795
Square root of a number 11 = 3.3166247903554
Square root of a number 12 = 3.4641016151377544
Square root of a number 13 = 3.605551275463989
Square root of a number 14 = 3.7416573867739413
Square root of a number 15 = 3.872983346207417
Square root of a number 16 = 4.0
Square root of a number 17 = 4.123105625617661
Square root of a number 18 = 4.242640687119285
Square root of a number 19 = 4.358898943540674
Square root of a number 20 = 4.47213595499958
Square root of a number 21 = 4.58257569495584
Square root of a number 22 = 4.69041575982343
Square root of a number 23 = 4.795831523312719
Square root of a number 24 = 4.898979485566356
Square root of a number 25 = 5.0
Square root of a number 26 = 5.0990195135927845
Square root of a number 27 = 5.196152422706632
Square root of a number 28 = 5.291502622129181
Square root of a number 29 = 5.385164807134504
Square root of a number 30 = 5.477225575051661

Conclusion:

All in all, there are multiple ways of tracking down the square root value of a given number in Python. We can utilize the number related math module, the ** operator, the pow() method, or the NumPy module, contingent upon our prerequisites.






Latest Courses