python sqrt

Using the sqrt() method in Python Difficulty Level: Beginner
The sqrt() function is a built-in function in the Python programming language that returns the square root of any number entered into it.

Pay close attention, geeks! With the Python Programming Foundation Course, you can strengthen your foundations and learn the fundamentals of python sqrt.

To begin with, make sure you are well prepared for your interview. With the Python DS Course, you may improve your understanding of data structures. Moreover, to get started on your Machine Learning adventure, enroll in the Machine Learning - Basic Level Course.

Syntax:

math.sqrt(x)

Parameter:

x is any positive integer greater than zero.

Returns:

When you enter a number, it returns the square root of that number.

passed as a parameter to the function

This is a Python3 program that will demonstrate the

# sqrt() is a function.

# Include the math module in your program.

add math to the mix

# Display the square root of zero.

print(math.sqrt(0))

# calculate the square root of four

print(math.sqrt(4))

the square root of 3.5 should be printed

print(math.sqrt(3.5))

Output:

0.0

2.0

1.8708286933869707

An error occurred during the execution of x0 because of a runtime error.

This is a Python3 program to demonstrate an error in

# sqrt() is a function.

# Include the math module in your program.

add math to the mix

# report the error when x is less than zero

print(math.sqrt(-1))

Output:

Traceback (from the most recent call to the most recent call):

File "/home/67438f8df14f0e41df1b55c6c21499ef.py", line 8, in "/home/67438f8df14f0e41df1b55c6c21499ef.py"

print(math.sqrt(-1))

ValueError: a math domain error has occurred.

Practical Application: Given a number, determine if it is a prime number or not.

The approach is to run a loop from 2 to sqrt(n) and verify if any number in the range (2-sqrt(n)) divides n in the loop.

# Python program to demonstrate the practical use of the sqrt() function

# include the math module

add math to the mix

# function to determine whether a prime number exists or not

check(n) for def:

if n == 1 then

return the value False

# starting with 1 and going up to sqrt (n)

with respect to x in the range(2, (int)(math.sqrt(n),+1):

If n percent x equals zero, the following is true:

return the value False

return the value True

# driver's license code

n = 23 people

if check(n) is true:

print("prime")

else:

make a printout ("not prime")

Leave a Reply

Your email address will not be published. Required fields are marked *