Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Python, kids! Python! And Triangles! And learnin's!

DZone's Guide to

Python, kids! Python! And Triangles! And learnin's!

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

So! Let's talk math, because it's fun. We'll use python for this because we don't have to compile it, it's easy, and it's useful in simulation projects like Micropolis.

Let's get some things out of the way first. Python's easy to run:

$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or
"license" for more information.
>>>

Exiting python is also easy: CTRL-D does the trick.

We'll use different fonts to show system output and user input. A monotype font is computer-generated text; we'll use a grey background to show user input. For special characters (like CTRL-D) we'll use italics with the grey background. So the interaction above would be:

$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or
"license" for more information.
>>> "hello" 'hello' >>> [CTRL-D] $

Okay, that was fun. ("Really?") Now, we'll be doing some stuff with a text editor, too, because we'll want to reload things instead of having to retype them, so we might as well open an editor somewhere as well.

A triangle is a figure that has three sides, right? That's pretty basic stuff. There's something called a "right triangle," which has one special thing about it: one of the angles is always ninety degrees. (A ninety degree angle is the kind of angle that squares have, for a simple explanation. A more complex explanation, if you're interested, is that a circle, divided into four equal parts, will have four right angles intersecting at its center. If that confused you, go back to the bit about the square. It's simpler.)

A right triangle is surprisingly, amazingly useful. However, before we can learn to use it, we need to learn a little more about it.

For simplicity, let's look at a triangle, and label its parts:

Right triangle

Let's rename the vertical line as segment “A.” The line at the bottom, let's call that one “B.” The “Hypotenuse” - which is, by the way, the correct name for that segment – is what we will label “C.”

Nu!1 Let's play with this triangle some. That hypotenuse – line “C”, you remember – has a specific length. Always. That length depends on the length of A and B, but if you know the length of any two sides of a right triangle, you can always tell the length of the third. (In fact, you can also tell what the specific angles are, but that's not important to us right now.)

The way we do this is through something called the “Pythagorean theorem.” Pythagoras was a Greek mathematician who lived around five hundred years before Christ was born (i.e., the beginning of the Common Era). We don't know a lot about him because his writings haven't survived, but everyone knows something about him because the Pythagorean theorem is one of the most useful mathematical formulas mankind has at its disposal. He may not even have come up with it, but he gets credit for it. (Maybe the "something we know" about Pythagoras is that the Pythagorean Theorem is named after him... which isn't much, but last I checked, that's more than I had named after me.)

What's a theorem? Well, a theorem is something that can be proved. Simple enough, eh? (It's actually a little more complex than that, like everything else, but let's not confuse the issue any more than we already have.)

Here's what the Pythagorean Theorem actually says: “In any right triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle).” Looking at our diagram again, C is the hypotenuse, remember?

So if we say “A” and “B” are the lengths of the sides they represent, then we can always determine the length of C like this: a^2 + b^2=c^2   \,

Exciting stuff, huh? Let's explain everything there. The a, b, and c represent the lengths of the sides they represent with c being the side opposite the right angle.

The little superscripts there are called exponents. An exponent is a way of saying “this number multiplied by itself X times.” So 42 is the same as 4 * 4; 33 is the same as 3 * 3 * 3. The way c2 is shown as the result means that whatever number we get from adding a2 and b2, we need to determine the "square root" - the "4" in "42" up there.

So... let's walk through this a little bit, shall we? Let's look at our triangle, and plug in some numbers. Let's make a 3, and b 4. That means that our equation looks like this: c=(square root of) 3*3+4*4

Let's start mathematicizing! (Yes, I made this word up.) First off, we apply our exponents, so the formula looks like this:

c=(square root) 9+16 - remembering that 32 = 3 * 3 = 9 and 42 = 4*4 = 16.

Now, our next step is to do that addition, so our formula now looks like this: c = (square root of) 25

If you remember anything about our friends the exponents and their cousins the square roots, then you know that 5*5=25, which means that c here is 5.

We've now walked through the Pythagorean theorem in somewhat painful detail, but hopefully you understand it now.

We started off, though, with python! We immediately left it behind – it might be a good time to get back to it.

So let's write the Pythagorean theorem as a function, so we can always determine c, given a and b. (We could do other stuff, too, like – given c and a, determine b. But let's hold off on that for now.)

Let's do the simple thing first, and use the numbers we put in before. Let's get Python to give us c, if a and b are 3 and 4, respectively.

$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or
"license" for more information.
>>> import math >>> a=3 >>> b=4 >>> math.sqrt(math.pow(a,2)+math.pow(b,2)) 5.0 >>> [CTRL-D] $

Whoa! That's exactly the sort of result we wanted! There's a few strange things: the "import math" there makes “sqrt” -- Python's name for the function that gives you the square root of a number – and pow (which applies exponents to numbers), then we assign a and b, then we... tell python to evaluate the square root of the addition of the squares of a and b. (That's the Pythagorean theorem, after all!)

That's a lot of typing to do over and over again, though, so let's go to our handy dandy editor and write a method to do this for us.

Open a file called “triangle.py” -- we'll be doing more stuff with triangles over time. We'll be creating a module called "triangle," stored in this file.

In this file, we need the following text, most of which will look very similar to what we just saw above:

import math
def hypotenuse(a, b):
return math.sqrt(math.pow(a,2)+math.pow(b,2))

Now, we can use this like so:

$ python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or
"license" for more information.
>>> import triangle >>> triangle.hypotenuse(3,4) 5.0 >>> [CTRL-D] $

If we want to, we can play with other numbers: try a=4, b=5! (You'll find that the result is 6.4031 or so.) Feel free to put in whatever you like, and see what you get.


1Okay, I'm sorry - “nu” is a Hebrew word meaning something like “well?” or “so?” It's also used to say silly things like “let's move on.” Blame me for being a Jew.

 

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}