Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
We all have come across square roots in mathematics. It is undeniably one of the most important fundamentals and hence needs to be embedded in various applications. Python comes in handy to serve this purpose by making it really simple to integrate Square Roots in our programs. In this article, you will be learning how to find Square roots in Python.
Before going forward, let us take a look at the topics covered over here:
The square root is any number y such that x2 = y. Mathematically it is represented as x = √y. Python provides built-in methods to calculate square roots.
Now that we have a basic idea on what is a square root of a number and how to represent it, let’s move ahead and check how we can get the square root of a number in Python.
To calculate Square roots in Python, you will need to import the math module. This module consists of built-in methods namely sqrt() and pow() using which you can calculate the square roots. You can import it by simply using the import keyword as follows:
import math
Once this module is imported, you can make use of any function present within it.
The sqrt() function basically take one parameter and returns the square root it. The syntax of this function is:
SYNTAX:
sqrt(x) # x is the number whose square root needs to be calculated.
Now, let us take a look at an example of this function:
EXAMPLE:
from math import sqrt #absolute importing print(sqrt(25))
As you can see, the square root of 25 i.e 5 has been returned.
NOTE: In the above example, the sqrt() function has been imported using the absolute method. However, if you import the complete math module, you can execute the same as follows:
EXAMPLE:
import math print(math.sqrt(25))
OUTPUT: 5.0
Another method to compute the square root of any number is by using the pow() function. This function basically takes two parameters and multiplies them to compute the results. This is done in order to the mathematical equation where,
x2 = y or y=x**.5
The syntax of this function is as follows:
SYNTAX:
pow(x,y) # where y is the power of x or x**y
Now let us take a look at an example of this function:
EXAMPLE:
from math import pow print(pow(25,.5))
OUTPUT: 5.0
These functions can be used to solve many of the mathematical problems. Let us now take a look at the working example of one such application of these functions.
Let us try to implement the very famous Pythagoras Theorem using these functions.
Accept values of 2 sides of a triangle and calculate the value of its hypotenuse.
Pythagoras theorem states that in a right-angled triangle, the side opposite to the right angle called the hypotenuse is measured as the square root of the sum of squares of measures of the other two sides, which means
c=√(a2+b2) # where c is the hypotenuse
Here is the solution in Python:
from math import sqrt #Imported the square root function from math module from math import pow #Imported the power function from math module a=int(input("Enter the measure of one side of a right angled triangle:")) b=int(input("Enter the measure of another side of a right angled triangle:")) #input function is used to take input from user and is stored as string # which is then typecasted into an integer using the int() function. c=sqrt(pow(a,2)+pow(b,2)) #we have implemented the formula c=√(a2+b2) print(f"The measure of the hypotenuse is: {c} based on the measures of the other two sides {a} & {b}")
OUTPUT:
Enter the measure of one side of a right-angled triangle :3
Enter the measure of another side of a right-angled triangle: 4
The measure of the hypotenuse is: 5.0 based on the measures of the other two sides 3 & 4
This brings us to the end of this article on Square Root in Python. I hope you have understood everything clearly.
To get in-depth knowledge on Python along with its various applications, you can enroll for live Python certification course online with 24/7 support and lifetime access.
Got a question for us? Please mention it in the comments section of this “Square Root in Python” blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Data Science with Python Certification Course | Class Starts on 9th November,2024 9th November SAT&SUN (Weekend Batch) | View Details |
Data Science with Python Certification Course | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co