You are on page 1of 7

[MUSIC]. Well, welcome to class. today we're going to start you on your journey of learning how to program in Python.

Today's lesson is fairly straight forward. you've probably seen some of this in Middle School and High School algebra. I'm going to tell you about how numbers represented in Python, and then we'll discuss how to build arithmetic expressions by combining these numbers. I don't think it's anything that's too mysterious here. Kind of what you should, the way you should think about today's lecture is that Python at it's core is just like an overgrown calculator. We're going to give it some data, in this case numbers, and we're going to ask it to do some operations. And it's going to, when it's finished, give us an answer. And so, that'll kind of consume most of today's lecture. Then, I'll go on and we'll talk about ways to build variables, and functions, and things like that later on. Okay, let's go to class. Before we jump into the details of our lecture on arithmetic expressions, let me show you about how we're going to handle examples that we're going to consider during lecture. So, here I have a CodeSkulptor window setting, and it's got the example we're going to consider. This is actually the completed example. I'm going to walk through it and fill in all the Python expressions dynamically, so we can see how I, how I do it. But, the critical thing is, we're going to make that file available to you. It's going to, in the break, between this segment of video and the next, we're going to pop up a URL that you can click on. And it will take you to this file inside CodeSkulptor and you can kind of see up here there's actually a naming invention we have. It's always codesculptor.org we put a hash mark with examples and then afterwards we'll have the lecturename.py. You don't have to remember that. You can click on it, pull it up, and play around with it either during the video or after the video is over.

So, let's talk about numbers and arithmetic expressions in Pythons. So, there are two types, two kinds of numerical data types in Python. They're integers. Think of kind of signed whole numbers, and there are floats. Think about those as kind of a way to represent decimal numbers with some finite precision. So, we can just type in examples of this very easily into Python. So we could say, print 3 or minus 1, or maybe 3.14159 or minus 2.8. And if we click Run, well, out comes these numbers printed out in the console. There are two Python functions that allow us to convert into these data types. There's a function int that takes a piece of data and converts it into an integer, this will work on floats. And you'll see it'll work on strings and other things later on. There's a function float that will take a piece of data and try to convert it into a floating point number. let's just focus on int for now because it's kind of interesting. So, we could try to see what it does when we apply int to various floating point numbers. So, let's apply it to 3.1 and maybe take the int of 3.7, and the int of minus 3.1, and int of minus 3.7. The reason I'm doing this is because I want to figure out kind of what whole number float converts a decimal number into. If you see in this example here, all these numbers, when we apply int to them, what we really do is we just take everything to the left of the decimal point. And we throw away, throw away everything to the right of the decimal point. So, for example, this doesn't round. It's just kind of strips off everything on the left-hand side of the decimal point. if we take the float of an integer, it's actually boring. It's we just say float of maybe 3 or float to minus 7. And what we can see is that it's about what we expect, 3 and minus 7. Now, you probably worked with integers before. But you, you may not have worked with floating point numbers. And the key, key distinction here is that

a floating point number doesn't always represent a decimal number exactly. there's kind of a scene, if you're a fan of the old time original Star Trek, where Dr. Spock terribly confuses the ship's computer by commanding it to compute pi to the last decimal digit. Well, pi doesn't have a finite representation as a decimal, and so the computer went merrily off, trying to compute the last digit of pi, and got in a loop and eventually I think gave up control of the ship and Spock saved the day. I'm going to show you a demonstration to kind of to get you going here to show you a little bit about how the limitation of Python representing a really high precision decimal numbers as a float. So, what I've got done here is I've gone out and looked up on the web a 50-digit approximation of pi and a 50 digit approximation of the square root of 2. So, I'm just going to put in a print statement from each of these and, and try to ask it to print out what it thinks this 50-digit approximation really is as a floating point number. So, we're going to say print and I'm going to copy here. This is my representation for pi. And I can say print, and I'll copy this long representation for square root of 2. And we'll run that. And what you see is, over here, that in the console, Python printed out about a 15 digit approximation to both of those numbers. So, it didn't take all 50 digits and store them. It stored about 15 decimal digits for each of these numbers. So, when Python is doing calculations using floating point numbers, it, it doesn't store kind of an infinite representation, it stores a finite representation. And for most of time in this clash/g, you'll never notice the difference. There'll be a few times as you progress on in your programming career, this'll start to become important. In fact, there actually, as you get high enough, there are actually kind of whole sections of class devoted to this issue. For now, all I want to do want you to do is just be aware of this particular issue. So now, we have numbers, we have ints, we

have floats. And, if that was another thing that Python could do, it'd be kind of boring. So, we're next going to talk about arithemetic operations that we can apply to numbers to make more complicated expressions. This is a very similar, what you probably learned to do in Middle School with a calculator. the only difference here is we're going to actually just type these expressions in this text. there're five operators we're going to look at. We're going to look at plus, minus, times, division, and power. Those correspond to addition, subtraction, multiplication, division, and exponentiation. let's just do a couple of examples real quick. So, I could say 1 plus 2 or minus 4, and 5 times 6, and then maybe like, let's go to the 5th power here. So, we can run that. And what we see come out, comes out, is 1 plus 2 is 3, 3 minus 4 is minus 1, 5 times 6 is 30, and 2 to the 5th power is 32. Now, the only operator that's a little tricky here is division. So, division takes two integers and this is an automatically returns back an integer. and in fact, we're going to, the version we're going to use is going to return back a, a floating point approximation to kind of the exact answer. And so, let's just do that real quick. Let's do, let's say print maybe 1 divided by 3, or 5 divided by 2 maybe minus 7 divided by 3. So if we run that, sure enough, what comes out is what I just discussed. There are floats that approximate the exact answer. So, I want to point out something that you should keep in mind here. That this implementation of division is not consistent with Python 2.6. It's the one place in CodeSkulptor where we don't make a design choice based on Python 2.6. We instead replicate what JavaScript does and what Python 3 does. Python 3 uses the slashes division to do floating point division. if you want to use integer division, which is what was used in Python 2.6, you

can use the slash, slash operator. And that will return an integer as a result of doing division. So, just print that out and I'll explain kind of how it works. So, we do 1 integer divided by 3, and 5 and integer divided by 2, and minus 7 divided by 3. And the answers that are going to come out are going to be integers now. And it's 0, it's 2, and it's minus 3. So for positive numbers, you can always just think it's kind of the, the, the largest whole number less than the answer. For negative numbers, you still kind of go to the, the largest whole number less than the answer. You might ask, well, why don't we do it the same way as kind of the end operation and just take the whole part? Well, we're going to look at the remainder operator in a couple of lectures. And it turns out that this is a nice way to do integer division for negative numbers because it'll be an important relationship that'll always hold with integer division, and the remainder operator. And I'll talk about that in a few lectures. so in general, just use slash for division and I think everything will be fine. So, to finish off the lecture, note that we've got the ingredients to do something interesting now. We have numbers and we have operations, and so can combine these numbers and operations and make more complicated expressions. So, what's an expression? Well, it's either a number, or it could be two expressions combined with some kind of binary operator, operator that takes two inputs. Something like times or plus. And to be technically correct, you can actually build expressions using unary operators. The only one that you know about is minus. You could just take the negative of an expression. So, let me show you some examples of building the more complex expressions using binary operators. So, I could print out, let's see, 1 plus 2 to the 3rd, or 4 minus 5 divided by 6,

or maybe 7 times 8 plus 9 times 10. So if we run that, and what do we see over here? Well, let's see. 1 plus 2 to the 3rd, that's 1 plus 8, that's 9. 4 minus 5 6th, that's let's see. 5 6th is point 833333 so it's 3.1666. Let's see 7 times 8 is 72 plus 90. Actually, 7 times 8 is 56 plus 9 times 10 is 90, so it's 146. Now, you might notice when I did these expressions up here, that I, I kind of had like a linear sequence of numbers and operations. And I somehow interpreted them in terms of this idea of having kind of an operator collecting two expressions. So, there's some rules you're probably really aware of that you can use to take this kind of, linear sequence of numbers and operators, and turn it into an expression. It's called Operator Presidence. And you probably saw this in Middle School when they told you a pneumatic of the form, please, excuse my dear Aunt Sally. What that says is that when you have this linear sequence of you know, this linear expression, what you're going to do is you're going to go through and do parentheses first. Now, if you've done parentheses, then you're going to do E for exponentiation. Then after that, you're going to do multiplication and division. And then after, you're going to do addition and subtraction. That's what the A and the S stands for in Aunt Sally. And for multiplication and division, they really have equal precedence, so you do them from left to right. And for addition and subtraction, same thing. They have equal precedence, you go from left to right. So, for example, we could go through and we could say you could say, print 1 times 2 plus 3 times 4, and or the rules of precedence say that multiplication comes before division comes before addition. So, we can say 1 times 2 is 2, plus 3 times 4 is 12. And, so run that, and sure enough, we get 14 back from both of them. note, that if you would like to overwrite this and do this in a different order, you can always use parentheseses.

So, we could say, print 1 times 2 plus 3 times 4. And now, there is a precedence would say, do the parenthesis first. So, that'd be the same thing as print 1 times 5 times 4, and we get 20 for both expressions. So, I think that's probably pretty clear. We're done at this, but you've done this before. So that's our lecture on arithmetic expressions. I hope you got a little bit of an idea about how to start doing things in Python. I'll see you back in a little bit.

You might also like