Introduction: How to Make Python Do Your Math

Having a computer do math for you can be really useful, especially if you need to do the same type of problem over and over again. Python is a relatively easy and free programming language which can be a good place to do a simple task like this. This project will be very simple, but Python can be used in much more complicated applications. For example, YouTube and Google were originally written using Python. Also, a quick disclaimer that I am definitely not an expert with programming or python, but I am sure that this Instructable can be useful if you are a beginner.

All you will need is a computer and a brain and you're ready to go!




Step 1: Sign Up for Pythonanywhere (It's Free!)

First you need to get python to run your code. You can actually download python for free, without signing up for pythonanywhere, but pythonanywhere is really accessible and easy for beginners. There are also payed versions of pythonanywhere, but these aren't necessary.

Start by going to and click the tab in the upper right hand corner that says "Pricing & signup", next click the button that says "create a Beginner account".

Follow the steps to create an account and you can take the tour that they provide to better understand the layout if you like.

Step 2: Make a New File

The next step is to make a new file to run your code. From the home screen, which is where you should be already, click the tab in the upper right that says "Files". Next create a file name, and using a descriptive name can be useful in the future.

Important: Make sure to add ."py" at the end of your file name, otherwise your code won't run.

After you've made a file name, click the button to the right that says "New File". You are now ready to write your code.

Step 3: Determine What Math You Want Python to Do

You may already know what you want your code to do, but now is the time to think of some kind of math problem that you want a computer to solve. Knowing the types of problems and the ways to code them will come with more experience, but here are a few examples of simple problems that you could do.

  • Calculate the area and perimeter of a circle if you type the radius or diameter
  • A quadratic formula calculator
  • Convert Fahrenheit to Celsius (or any other conversion)
  • Do a cross product of vectors (this is tedious by hand, but perfect for a computer)

Or if you ever have to calculate something in your job, you could tailor it to that. For example, I need to calculate what's called "Watt density" for my work a lot. So I could make a calculator that I input the heater Wattage and the length of the heating element, and it will spit out the answer.

For this Instructable, I'll make a code that tells me if something is a right triangle. A right triangle is where one of the angles is 90 degrees. I'll type in 3 side lengths and the program will do all of the hard work and say if it's a right triangle or not.

Step 4: Write the Code: Basics

You should already have your file open and ready from steps 1-2. I'll mention some coding basics first and then some python specific things that you might need to know.


  • A computer will read from the top to the bottom, line by line of your program, and it only "knows" what it's already read. So if you tell it that x = 3 on line 10, but you ask it what x is equal to on line 4, it will cause an error.
  • All programming languages (like python) have what's called syntax. This is sort of like the "grammar" of the language, and you need to learn some of the specific syntax for whatever language you're using in order for you to successfully make a program. Luckily the internet can help you out with this.
  • I'll be using something called a "while loop". This will loop through a few lines of code until some condition is met. An example might be for a program to keep adding 1 until it gets to 10 and counting how many times it does that.
  • You can make things called variables. These usually have "English" names, but can be assigned a numerical value. So you can make a variable called 'side_1' but set that variable name equal to 3 ('side_1 = 3'). Now if you did an operation in your code of '2 + side_1' the output would be 5.
  • Making comments are really useful for you and for anyone that uses your code. Comments are lines that explain what your code is, or what a specific line of code is doing. The computer doesn't read the comments, it's only for humans to understand what the code means. A comment in python starts with an '#', and everything after that in the line will be ignored by the computer.
    • e.g. #This line of code is multiplying a^2 + b^2


  • When using pythonanywhere, you can write your program in an area near the top of the file page. If you make a program where it asks the user to input numbers, like my program will do, then the user can do this in an area below. I will write the code above, then click "Run" to run the code, then enter my inputs in the black area below.
  • Python can be picky about how you indent your lines of code, this is part of the syntax. If you follow how I type it, then you should be fine.
  • Part of the syntax of python that is important for my code is that a power is denoted by two stars '**'. So if you wanted to say x = 3^2 (3 squared), then you would need to type, x = 3**2. Or x = 3^3 is x = 3**3.

Step 5: Write the Code Continued

I will include my code in two links below, which you can access with a pythonanywhere account so you can copy and paste it in to your program, but I'll also explain what is happening here. One link will have my comments, and the other won't. Also, if you are making your own calculator or math program, this is where you will need to use your brain if you haven't been already. You need to think of how a computer could solve your problem, and as I said before, the knowledge and intuition for this will come with experience.

My program will tell me if a triangle is a right triangle after I give it 3 side lengths. Luckily, Pythagoras thought about this hard enough 2500 years ago that we have a formula to use. If you name the three sides of a triangle a, b, and c, then this formula will be true for all right triangles: a^2 + b^2 = c^2. So my program will do the following in this order:

  • Ask the user to input 3 side lengths
  • Calculate all of the combinations of "one side squared + another side squared" and check if that equals the third side squared.
  • If any of these combinations are true, the program will print (type out) that it is a right triangle. Otherwise, it will print that it isn't a right triangle.

Keep in mind that whenever you see an '#', everything after that on the line is just a comment, meant to explain what is happening. The computer doesn't read it. Also, a single '=' means that I am setting a variable equal to a number, like x=3. However, a double '==' is checking to see of two things are equal. x == 3 means, "Is x equal to 3?", not, "make x equal to 3".

If you are a beginner, then look at the code with comments first. The code without comments is provided so you can see what is actually being read by the computer better.

Code with comments

Code without comments

Step 6: Run the Code

This step is easy--simply run your code! Make sure to save the file first, then click the "Run" button in the top right. Also, if you have too many consoles open in the free version of python anywhere, then it will ask you to "kill" one or more so that you can free up space to run the new one. This won't be an issue if this is your only file.

My code asks the user for inputs, so if you run it, then type a number then hit enter. Do this for all of the side lengths and then it will tell you the output. If you want to run it again, then click "Run" again. You might need to click your cursor back in the black box again.

Step 7: Correct Any Errors

If you have any errors, then the line that is erroneous will have a red x by it. Also, there will be a description of what is wrong in the command box. In the case above, I made a syntax error because I forgot to add an apostrophe. Keep fixing your errors, and save each time after you make a change until your code is correct and works.

Step 8: Test the Code

Now that you fixed your errors, your code should run. However, that doesn't necessarily mean that it works correctly. It might be giving an output, but one that is incorrect. For doing math, you can test your code in a simple case where you already know the answer. If you get a different answer, then the logic of your code might be wrong or you made a math error. You also should test an answer that you know will be wrong to make sure your code still agrees.

Step 9: Keep Testing Your Code

You want a program that will work whenever you need it and for any case that is possible, so keep testing your code with different parameters. Think a little outside the box to try to "break" your code. Maybe use negative numbers if applicable, or numbers with decimals. If you change your parameters and your code breaks, then you might need to add something to your code that takes care of that scenario.

Step 10: Use It!

Maybe you wanted to do this project to learn some programming, but ideally you want to make something that you can use. Maybe you wrote a code that does the quadratic formula for you so you never have to solve for the roots by hand again, or you made one that does a task for you at work. Maybe you made one that does your taxes for you. The point is to make something useful, so go ahead and use your creation! Coding is a powerful skill to have, and it probably will be more and more in the future, especially in STEM fields. So doing math with a computer is actually a really practical and useful skill to practice. Learn more and have fun!