Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Practical Uses for Python's Square Root Function

00:00 In the previous lesson, I went off on a math tangent and showed you how to use Newton’s method to calculate square roots. In this lesson, I’ll return to a more practical space showing an example of using roots in code.

00:14 One of the most famous uses of square roots is in the Pythagorean theorem. This theorem states that the square of the length of the long side of a right angle triangle, the hypotenuse, is equal to the sum of the squares of the other sides.

00:29 Or, c squared is equal to a squared plus b squared. Rearranging that, and you get that the length of the hypotenuse is the square root of the sum of the squares of the other sides. Let’s use that in practice.

00:46 Say that you’ve got to figure out the area of the roof of a house so that you can determine how much to spend on new shingles. The only tool you have is one of those fun little laser measuring tools, and no ladder. Could you do it? With a few simple measurements you could, and you don’t have to go anywhere near the roof. Start by pointing the laser doohickey at the peak of the roof to get the height of the top point from the ground.

01:12 Then point it at the base of the roof to get the height of the building without the roof. Then turn the device to the side and measure the width and depth of the house, and you’re all set to go.

01:25 The key to this is seeing the right angle triangle hidden inside of the roof structure. You can get the b side of the right angle triangle by dividing the width of the house by 2.

01:37 You can get the value of a by taking the difference between the height of the peak and the height of the house without the roof. Add some Pythagorean magic, and you get the length of the sloping part of the roof. Multiply that slope by the depth, then by 2 because you’ve got two sides to the roof, and you’re good to go.

01:57 Let’s do this in code.

02:01 Inside of area.py, I’ve defined the roof_area() function. It takes four parameters: the house’s peak, the height of the house without the roof, the width of the house, and the depth of the house. Lines 5 and 6 calculate the values of a and b in the triangle. Line 7 is the Pythagorean theorem.

02:20 There’s your square root! Line 9 calculates the area of one side of the roof by multiplying the length of the sloping edge, just determined, by the depth of the house. Then line 10 returns the result, multiplying the roof_side by 2 for the two parts of the roof.

02:39 Let’s import that,

02:45 then run the calculation.

02:50 And there you go! You know your roof area. You can go buy some shingles. For my American friends, those numbers might seem weird. They’re reasonable for a house in meters.

03:00 This would be about a 1,000 square-foot house. A meter is about the same as a yard, so if you want to think in feet, multiply all the arguments to roof_area() by 3.

03:11 Square roots get used in a lot of different kinds of formulas, including: Distance calculations, especially those used to determine location when using GPS. Calculating normal distributions, also known as the bell curves—the reason I passed school.

03:27 Finding the period of a pendulum. Doing math with vectors and finding their magnitudes, which is common in a lot of physics problems. The orbital velocity of satellites formula and its sister formula for escape velocity. Frequency harmonics, and other wave calculations like the speed of sound.

03:48 You’ve lasted through the square root course! Last up, a quick summary.

Become a Member to join the conversation.