The sqrt(): Math Function of Python

The sqrt(): Math Function of Python

The sqrt() function is a built-in function in Python for performing operations related to math. The sqrt() function is used for returning the square root of any imported number.

In this tutorial, we will discuss how we can use the sqrt() function in Python.

Syntax:

Parameter:

‘N’ is any number which is greater or equal to 0 (N >= 0).

Returns:

It will return the square root of the number “N”, that is passed as the parameter.

Example:

Output:

The square root of 7:  2.6457513110645907
The square root of 49:  7.0
The square root of 115:  10.723805294763608

Error

We get an error while executing the sqrt() function if the number passed as a parameter is less than “0” due to a runtime error.

Example:

Output:

The square root of 16:  4.0
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-79d85b7d1d62> in <module>
      5 
      6 # printing the error when x less than 0
----> 7 print ("The square root of -16: ", m.sqrt(-16))
      8 

ValueError: math domain error

Explanation:

As at first, we passed “16” as a parameter, and we got the output as “4”, but for “-16”, which is less than “0”, we got an error, saying “math domain error”.

Application

We can also use the sqrt() function for creating an application for performing math functions. Suppose we have a number “N”, of which we have to check whether it is a prime number or not.

Approach:

We will use the following approach:

  • Step1: we will import the math module.
  • Step 2: we will create a function “check_if” for checking if the given number is prime or not.
  • Step 3: we will check if the number is equal to 1. If “yes”, it will return false.
  • Step 4: we will run a for loop from range “2” to “sqrt(N)”.
  • Step 5: we will check if any number between the given range divides “N”.

Example:

Output:

#1:

Enter the number you want to check:  12
The number is not prime

#2:

Enter the number you want to check:  11
The number is prime

#3:

Enter the number you want to check:  53
The number is prime

Conclusion

In this tutorial, we have discussed how to use Python’s sqrt() function for calculating the square root of any number greater than 0.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263373.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论