The sqrt(): Math Function of PythonThe 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 ErrorWe 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”. ApplicationWe 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:
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 ConclusionIn 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