From the course: Introduction to Python: Learn How to Program Today with Python by Pearson

Write an area calculator

Okay, so the Python console and interactive mode that we looked at in the last sub-lesson is great for just testing out little things and seeing how Python works, maybe doing a bit of debugging, but you're going to be wanting to write most of your code in files so that you can write, edit, run the code and not have to type everything in over and over and over again. Okay, so let's start working in files. We're going to, I'll close the Python console for now or hide it, and we're going to work on our first problem, which is problem one, area calculator. Okay, so in this problem, it's just a really basic calculation, just getting you used to working in files, and the area of a right angle triangle is defined by this equation here, a times b divided by 2. So you can open up problems, problem 1, area calculator. Okay, and before we start working on it, you'll notice that there are some things in here that are all grayed out, and these are called comments. So comments are for documenting what some piece of code does. It helps communicate to the humans who are reading the code, and also has Python ignore that code. So the Python interpreter will just ignore it and not try to run it as code. So here we have kind of three kinds of comments that you can see. At the top is what's called a doc string, and you can use triple quotes. This is also how you would create multi-line strings if you were to save it to a variable. But if it's at the top, it's a doc string, and that's generally for describing what a file is doing. You could also have some example code in there, or how to use the file, or author information, or license, and copyright information, links, and stuff. Okay, and that goes at the very top of the file in these triple quotes So I've got those on all of my files here so that you can tell what you're supposed to be doing in it Now there are single line comments They can either be on their own line or they can be in line and those start with a pound sign or a hashtag Whatever you want to call it Okay, and pycharm is nice and it grays it out and we can tell that pycharm is not or the Python interpreter is not going to try to run it. Okay you it is actually not great practice to comment every single line when it's kind of self-evident so we don't technically need these comments here assuming we know that A and B are just the two sides of a triangle and then I want you to fill in the area here and print it out. So right now if I right-click and run this problem it'll just print none but But then you can fill it in with the correct calculation. Once you've done that, you can also remove this to-do comment because that won't be necessary anymore. Okay, so you could just keep following along and see me go through it. But for extra practice, I think it's probably a good idea to pause the program or pause the video. to do it on your own, see how far you get, and then play again and see how I solved the problem. Okay, so I'll give you time to work on that now and pause. All right, so let's see how to solve this problem. You know, this is a pretty simple calculation. We'll do a more complicated one in a second. We just have A times B and divided by 2. So according to bed mass, it will, this is fine, you don't need to change anything about it. If you wanted to, you could add parentheses for readability wherever you want to. And then because we've done that, we can remove the to do comment there. And I can run this file, it should give me 6.0, which is correct. And let's see if I add some decimal places to there. I've got another answer. And it's showing up with these like a very, very slight rounding error at the very end. That's because when you have decimal places in Python and in many other languages as well, These are called floats. Based on the way that Python stores numbers, and they're stored in binary, sometimes numbers are not able to be stored super precisely, or especially when you do a calculation on them, there's a little bit of a rounding error. So that's a pretty common thing when you're working with floats. So we'll ignore that for now, and I'll show you how to round them or display them to a certain decimal precision later on. Okay, so that was our first problem, and also the solutions to the various problems are going to be in the solutions folder here. Okay, now we'll do a more complicated calculation, and so this is going to be challenge number one. We're going to look at the area of a triangle, not of a right angle triangle, but just any old triangle. This is called Heron's formula. And you can open up challenges, challenge one, triangle area here. Okay, so I have, there's now variables for A, B, and C. You have to do two calculations. One is to calculate what's called the super perimeter, and then use that to calculate the area. So I'll pause to give you time to stop and and work on it okay now let's go through this and I'll get rid of that comment to calculate s we need to do a plus b plus c and then all of that has to be divided by 2. So I'm going to put that in parentheses here and divide it by 2. Next, to get the area, we need the square root function, which we can get from math. So let's import math. Our imports should be at the top of the file after the doc strings. then we can say math.squareRoot and fill that in. So this is going to be square root of s times s minus a times s minus c, and print the area. And I'm gonna use some initial numbers just to test that it's correct. Oh yeah, it's not correct, okay? Because this is three, four and five is actually a right angle triangle. So this should be three times four divided by two. So the result should be six. I can see what I did. I missed out S minus B. Oh, and then add a star to multiply. Okay, so that gives me 6.0. So that's great. It's actually a really good idea eventually to have some tests so that you know that your equations are working. But right now we can just test manually by using numbers that we know. So this should turn out to be six because it's a right angle triangle with sides 3 and 4, and 5 would be the hypotenuse. So we know that that works. Let's see what it would look like if I forgot to put parentheses around there. Well, we just get a different number. Okay. And if you wanted to round this number, let's say to one decimal place, we can do that. There is a built-in function called round. So we can say area equals round, pass in the area, and then it accepts a number of digits. If we don't pass a number of digits in, it'll round to the nearest whole number. But if we do, it'll round to that many digits. So let's say the nearest two. And let's try it with a different number here. And you can see it's rounding to decimal places. Eventually, you'll be comfortable nesting these functions. So you don't actually have to specify it on a new line here. We can just put round inside of print. And then you'll get used to working with nested functions like this, where we've got, or nested function calls, should say, where it'll do the inner ones first, evaluate that first, and then pass that result into the outer function. Okay, and that is our area calculation. Next, I'll talk about what programming is and how Python fits into the landscape.

Contents