Learn to Code via Tutorials on Repl.it!

← Back to all posts
Pythagorean Theorem! 📐
Bookie0 (6567)


How are y'all doing? Hope everyone is chilling and doing well!

Welcome to a tutorial on the Pythagorean Theorem!!! 🎉

You've probably already heard of Mr. Pythagoras and his famous Theorem, and with this tutorial we'll be learning about him, looking at what his Theorem is about, some fun facts about it, and finally a small program to calculate the hypothenuse and/or determine if a triangle is right-angled (and a few other things)!

Let's start!! xD

Full contents:

  • 1) Pythagoras
    - Life
    - Famous Works

  • 2) The Pythagorean Theorem
    - What it Is
    - Equation
    - How it Works

  • 3) Fun Facts
    - Theorem Well Known Before
    - Syntesthesia
    - Death
    - 4 Lives
    - Numbers

  • 4) The Program
    - How to Use
    - Brief Explanation of Code (And a bit of Math)

  • 5) Links
    - Sources
    - Helpful Links

  • 6) Closing

1) Pythagoras


Behold the man himself:

Pythagoras was an Ancient Greek Philosopher born in Samos, Greece in about ~570 BCE. He died in Metapontum, Italy, in about ~490 BCE, at the age of around 75 years old. Pythagoras spent most of his early life on the island of Samos, but when he was about 40, he emigrated to a city in Italy, Cortona. This is where most of his philosophical activity occurred.

Pythagoras was well brought up and well educated; he could play the lyre and recite poems as well as Homer. He was also interested in philosopy, mathematics, astronomy, and music. Pythagoras was influenced by some men like Pherekydes (a thinker), Thales (a mathematician), and Anaximander (a philospher). He was often called the first "True Mathematician"!

In about ~518 BCE, Pythagoras founded a philosophical and religious school in Crotona (a Greek colony in Italy). This was where he and some followers lived and worked. They worked together on discoveries and theories.

Famous Works

  • Numerical System

Pythagoras started the idea of a numerical system, which is the beginning of mathematics. In a nutshell, the numerical system is:

"A numeral system is a writing system for expressing numbers; that is, a mathematical notation for representing numbers of a given set, using digits or other symbols in a consistent manner. "

(From here)

  • Numbers with "personalities"

According to him, numbers were vital and made up the world.

Each number had its own personality - masculine or feminine, perfect or incomplete, beautiful or ugly. This feeling modern mathematics has deliberately eliminated, but we still find overtones of it in fiction and poetry. Ten was the very best number: it contained in itself the first four integers - one, two, three, and four [1 + 2 + 3 + 4 = 10] - and these written in dot notation formed a perfect triangle.

  • Pythagoras' Philosopy

Pythagoras and his followers hypothesized that everything in the universe could be clarified, or explained, with numbers. For example, today, we use numbers for calculating chances of precipitation, how fast a vehicle is going, wind speeds, etc.

  • Music Theory

This one is kinda an unusual one; it's about intervals in music, and I found it pretty interesting:

Pythagoras is also credited with the discovery that the intervals between harmonious musical notes always have whole number ratios. For instance, playing half a length of a guitar string gives the same note as the open string, but an octave higher; a third of a length gives a different but harmonious note; etc.

Non-whole number ratios, on the other hand, tend to give dissonant sounds. In this way, Pythagoras described the first four overtones which create the common intervals which have become the primary building blocks of musical harmony: the octave (1:1), the perfect fifth (3:2), the perfect fourth (4:3) and the major third (5:4). The oldest way of tuning the 12-note chromatic scale is known as Pythagorean tuning, and it is based on a stack of perfect fifths, each tuned in the ratio 3:2.

To represent it better:


2) The Pythagorean Theorem

What it Is

You all most likely know about the Pythagorean Theorem, and we all encounter it at school in math class.

So here's a little crash course if you havn't been listening!

Basically, let's say we have a rectangle triangle ABC with a 90˚ right angle on B (not real lengths):

Let's say side AB = 3 and side BC = 4. What does side AC equal?

That's when we'll use this formula (when I write ^2 it means squared or 'to the power of 2'):


AC^2 = AB^2 + BC^2

Meaning side AC squared equals the sum of sides AB squared and BC squared. So using the measures from the previous example:

There, now we have AC^2 = 25! Now to just find side AC, we can square root it:

Voilà, we've found out that AC, the hypothenuse of triangle ABC equals 5.

How it Works

Now why and how does this interesting equation work? To visualize it better, imagine if you put squares with length the side of each side of the triangle, like this:

To calculate the area of a square, it's side^2, so with the formula that squares each side of the triangle, we now have squares. So the area of the red square (on the hypothenuse) is equal to the sum of the areas of the two other squares.

3) Fun Facts

Some cool facts about Pythagoras and the Pythagoras Theorem:

  • Apparently, this theorem was well known by Indian, Chinese, Babylonian, and Greek mathematicians well before Pythagoras lived.

  • Some historians think Pythagoras had a medical condition called synesthesia. People affected with synesthesia makes them able to see music, "hear" colors, and connect certain smells with people's name. This is perhaps how he thought of his Music Theory.

  • How exactly Pythagoras died is still a mystery. Pythagoras' followers went into conflic with supporters of democracy and other topcis, and Pythagoras' group's houses where they met were bruned down. Maybe Pythagoras was killed in those burnings, or maybe he escaped to Metapontum ad eventually died there.

  • Pythagoras declared to have lived 4 lives. Yes, 4. He also once said he remember those lives.

  • The Pythagoreans believed that the number 1 was symbol of unity and was the origin of all things. 2 meant female, 3 meant male. The number 4 represented justice. 10 was the most perfect number as 1 + 2 + 3 + 4 = 10.

4) The Program

How To Use

So when you run the code, you'll find yourself in the main menu. You'll have different modes to choose from; the first mode where you can find the length of the hypothenuse in a right-angled triangle, the second mode where you can check if a triangle is right-angled, and the last mode where you can check your history and find out your previous calculations.

For the mode 1 (by the way, I'm using getch() so you don't have to press [enter], unlike input()), you'll have to enter the lengths of the triangle other than the hypothenuse. Like the program says:

Where side C is the hypothenuse, and side A and B are the other sides.

After entering those values, you can also choose how many digits after the decimal point (5 max). Next, the program shall calculate for you the length of the hypothenuse and detail the steps.

Then when you get back to the menu and try out the second mode to check if a triangle is right-angled, this time you'll have to enter the 3 lengths of the triangle. It'll check if it's right-angled, again detailing the steps, and will give the final verdict.

Finally, with the history, you'll have all your recent calculations, for both mode 1 and mode 2. You can also clear your history if you want.

Brief Explanation of Code (And a bit of Math)

So first, some imports like math (for square rooting), os (to clear the console screen), getch, and time. Next, some variables holding ANSI escape codes for the colors. After that, you'll see the dict history_operation that holds the calculation as item and the result as value. There's also the variable incrementer for outputting the contents of the history (with a for loop). Finally, there's the digits_after_decimal variable that's initally 2.

Next we have the 2 functions for the 2 modes. Those functions have float(input())s for getting the numbers from the users. Then, the program calculates all the values using ** and math.sqrt and gives the result (with nice colors); either the length of the hypothenuse or wether the triangle is right-angled or not. At the end of each functions, it saves the results in the dict for the history.

Finally, with the last function history_functions(), it clears the screen using os.system('clear') and with a for loop outputs each item and value of the history_operation dict (if the dict isn't empty.)

In the while True loop, it displays the menu choices, and with some conditionals calls the functions based on what the user wants to do. That's about all, a pretty straightforward program.

Read the comments I inserted into the code for more! ;)

5) Links


Helpful Links

For more about the Pythagorean Theorem and about Pythagoras, you can visit the links above and here are a few more:

6) Closing

Well, that's about all. I hope you've learnt something cool and new about Pythagoras and the Pythagorean Theorem. This can help you in math class, so go amaze your classmates and teachers!

  • Also, you know, try to learn the real way of how to calculate the hypothenuse; this program was just for 'illustrating purposes', you probably shouldn't use or rely on this during a test lol. But have fun with it anyways! :)

And also this amazing video, I have no idea this works but it's super amazing. Check it out!

Until we meet again! 👋

PS: In case you missed it, run the repl as there is a program.

PPS: Extend the console to the maximum so you can see the most!! :)
OldWizard209 (1702)

Repl.It has turned into a math class....... @Bookie0 How is this related to programming.....

Bookie0 (6567)

@OldWizard209 well, there is a program..

And math is coding/programming in a way. I did this tutorial after the Fibonacci that teaches recursion in programming.

XanthusPettitt (10)

@Bookie0 there is? I just saw math lol

Bookie0 (6567)

run the program! :)@XanthusPettitt

OldWizard209 (1702)

You know what, I really love math and I already know this, but you put your time and effort into this so u. rilly deserve an updooot @Bookie0

CyberHacker101 (137)

"updoot" sounds like saying upvote but you burp in the vote part @OldWizard209

LegendaryWolf (572)

bruh we need to lern math to code,

OldWizard209 (1702)

yap, and that is why, most people quit programming even before they write "Hello World" @LegendaryWolf

FlaminHotValdez (750)

Orange...Person...I can't hold it in anymore...


CyberHacker101 (137)

I seriously dont understand why this post just has 6 cycles only man

XanthusPettitt (10)

@CyberHacker101 sorry im new but what are "cycles"

Bookie0 (6567)

@XanthusPettitt useless internet reputation points. You get 1 cycle when someone upvotes your post/comment, 5 cycles when they select your answer as correct (only on ask board though).

CyberHacker101 (137)

My brain after reading this

TsunamiOrSumth (540)

@CyberHacker101 idk man this concept is p simple

CyberHacker101 (137)

I know that but there is so many words. I DONT LIKE MANY WORDS!!!!!!!!!!!!! @TsunamiOrSumth

CodeLongAndPros (1645)

@CyberHacker101 radical a^2+b^2 or radical c^2-b^2

CyberHacker101 (137)

Yo bro if ya wanna talk to someone you have to put the
@( Persons username) to tag or ping them @LegendaryWolf

LegendaryWolf (572)

ik that, but forgot to...

LegendaryWolf (572)

mee too @CyberHacker101. Grade? Sorry of im disturbing you

BobTheTomatoPie (3362)

haha i remember learning this stuff

Bookie0 (6567)

@BobTheTomatoPie ahaha lol guess it just sticks with you! xD

iocoder (166)

@Bookie0 this is rly cool! even tho i hate math like almost everyone else
*gives updoot*

Kookiez (417)

good job!


he looks quite depressed

RainBowLion1 (6)

This is to much please get this removed off replit before more peoples brain explodes.

RainBowLion1 (6)

@Bookie0 Im not kidding my small middle school brain is not smart enought for this

Whippingdot (720)

I, speaking on the behalf of the council of 2-year-olds, are not pleased with this. We do not know what your...putting a number to the power of 2 is and we do not know what the '+' sign means. You should have explained in further detail...BTW I know what all of these terms mean, just the council told me to say this to you.

P.S. the one word that caught my attention

You should correct it, it is supposed to be, CORTANA

Bookie0 (6567)

@Whippingdot I, replying on behalf of over 13 year olds (because 2 year olds aren't allowed on repl.it according to the ToS), understand completely your opinion. However, you can tell your.. 2-year-olds that for example x to the power of 2 is x times x. So if x = 5, x to the power of 2, or rather x^2 = 5^2 = 25.

And the '+' sign means addition meaning that 1 + 1 = 11. That is understandably harder to understand.

Oh ok thx

Whippingdot (720)

Thank you for helping me and the council. They are very pleased. They will give you many choopies in return. Be prepared for the scene in harry potter movie/book one replaced from mail to choopies in your home. Have a good day @Bookie0

Bookie0 (6567)

@Whippingdot ..Ok same to you and the council

CyberHacker101 (137)

Ahhh it used to be 6 cycles now it is 44 cycles i still get pings from this bro @Bookie0

LegendaryWolf (572)

I'd do algebra, but there is a problm

CoolGuy27 (68)

LOL! I made a similar thing a while ago in C#, but it had no colors and was a lot lazier, with no decimal point asking or explanation. It just took inputs and spit out the answer.

elipie (356)

speak poems

Idk if its just me, or do i feel like anyone can speak poems if they have vocal chords? I would say something like recite poems meaning that he memorized them or wrote them...

just a suggestion tho

Bookie0 (6567)

@elipie hum yes, good suggestion I'll add it! thanks!

elipie (356)

:D also

EsPeCiAlLy WiTh tHiS sPeCiAl InAuGaRaTiOn AnD sPeCiAl LeAvInG oF tHiS sPeCiAl OrAnGe PeRsOn ThAt I wOn'T mEnTiOn HeRe!!!


(srry repl no politics im too happy)


ArchieMullin (10)

@Bookie0 i don't make games or anythin i usually study alot of code from different languages cus im bored

ArchieMullin (10)

hippity hopity this code is now my propert cntrl c + cntrl v go BRRRRRRRRRRRRRRRRRR

Bookie0 (6567)

@ArchieMullin okay, make sure to give some credit though lol!

CyberHacker101 (137)

Woooohooooooo the old orange man left the white cheesecake house.

CyberHacker101 (137)

also what do you mean by "spam"? @Kookiez

CyberHacker101 (137)

lol as you said " I wont be as active this month due to spam" cant ya just turn da ringer off or somethin @Kookiez

Kookiez (417)

@CyberHacker101 oh right because at least 25% of the new posts in share are spam, and i also have school too

CodeLongAndPros (1645)

Just btw, it should be AB with a line across the top, not A*B

Bookie0 (6567)

@CodeLongAndPros oh, by AB I meant like segment AB, not A times B

How would I add the line across the top?

LuckyOreos (203)

@Bookie0 You’ve made Repl.it history with presenting us with a completely new subject, Math!!! Lol

Bookie0 (6567)

@DarthVader29 haha lol, this is kinda a sequel to a few other math tutorials I've made (Monty Hall Problem and Fibonacci, if you're interested xD)