1 00:00:00,154 --> 00:00:07,539 [MUSIC]. 2 00:00:07,539 --> 00:00:10,548 Well welcome to class today we're going to start you on your journey of 3 00:00:10,548 --> 00:00:15,767 learning how to program the Python. Today's lesson is fairly straight 4 00:00:15,767 --> 00:00:19,925 forward, you've probably seen some of this in middle school and high school 5 00:00:19,925 --> 00:00:23,376 algebra. I'm going to tell you how numbers are 6 00:00:23,376 --> 00:00:26,960 represented in Python and we will discuss how to do arithmetic expressons by 7 00:00:26,960 --> 00:00:31,855 combining these numbers. I don't think its anything that's too 8 00:00:31,855 --> 00:00:35,149 mysterious here. Kind of what you should, what you should 9 00:00:35,149 --> 00:00:39,310 think about today's lecture is Python is, is an over grown calculator. 10 00:00:39,310 --> 00:00:41,933 We're going to give it some data, in this case, numbers and we're going to ask it 11 00:00:41,933 --> 00:00:45,158 to do some operations. And its going to, when its finished, give 12 00:00:45,158 --> 00:00:47,542 us an answer. So that will kind of consume most of 13 00:00:47,542 --> 00:00:50,053 today's lecture. And I'll go on about ways to build 14 00:00:50,053 --> 00:00:53,144 variables and functions and things like that later on. 15 00:00:53,144 --> 00:00:59,429 Okay let's go to class. Before we jump into the details of our 16 00:00:59,429 --> 00:01:03,659 lecture on Arithmetic Expressions. Let me show you about how we're going to 17 00:01:03,659 --> 00:01:07,580 handle examples that we're going to consider during lecture. 18 00:01:09,150 --> 00:01:12,363 So here I have a code sculptor window setting, and it's got the example we're 19 00:01:12,363 --> 00:01:15,570 going to consider here. This is actually a completed example. 20 00:01:15,570 --> 00:01:18,738 I'm going to walk through it and fill in all the Python expressions dynamically so 21 00:01:18,738 --> 00:01:22,866 you can see how I, how I do it. But the critical thing is we're going to 22 00:01:22,866 --> 00:01:25,746 make that file available to you. It's going to, in the break between this 23 00:01:25,746 --> 00:01:28,598 segment of the video and the next, we're going to popup a URL that you can click 24 00:01:28,598 --> 00:01:32,112 on. And it will take you to this file inside 25 00:01:32,112 --> 00:01:36,504 code sculptor, and you can kind of see up here there's actually a naming convention 26 00:01:36,504 --> 00:01:40,940 we have. It's always codesculptor.org. 27 00:01:40,940 --> 00:01:44,234 We put a hash mark with examples, and then afterwards we'll have a lecture 28 00:01:44,234 --> 00:01:47,048 called .py. And you don't have to remember that, the 29 00:01:47,048 --> 00:01:49,697 link will be here, you can click on it to pull it up. 30 00:01:49,697 --> 00:01:52,301 And you can play around with it either during the video or after the video is 31 00:01:52,301 --> 00:01:55,405 over. All right, let's talk about numbers and 32 00:01:55,405 --> 00:01:59,697 their arithmetic expressions. So to begin, let's just dive right in and 33 00:01:59,697 --> 00:02:05,968 have Python print out some numbers. So let's print 3 and -1 and 3.14159 and 34 00:02:05,968 --> 00:02:11,760 maybe -2.8. So if I hit Run here, sure enough out 35 00:02:11,760 --> 00:02:16,464 come those numbers. Now the thing to note is that in Python 36 00:02:16,464 --> 00:02:21,500 there are actually two types of numbers. There's kind of sign whole numbers, 37 00:02:21,500 --> 00:02:25,354 things like 3 and -1. And then there are decimal numbers that 38 00:02:25,354 --> 00:02:29,500 kind of have fractional parts and always have a decimal point. 39 00:02:29,500 --> 00:02:33,064 And in Python those sign home numbers are called ints or integers and kind of the 40 00:02:33,064 --> 00:02:37,326 decimal numbers are called floats or floating point numbers. 41 00:02:37,326 --> 00:02:40,400 And if you are ever in any doubt about kind of what kind of number you are 42 00:02:40,400 --> 00:02:43,607 working with. There is a function in python which can 43 00:02:43,607 --> 00:02:47,648 actually tell you what type of number you are working with is called type. 44 00:02:47,648 --> 00:02:59,018 So I could say type 3, or maybe type 3.14159. 45 00:02:59,018 --> 00:03:03,162 If I run that, well, I'm going to guess this is an int and this is a float. 46 00:03:03,162 --> 00:03:06,830 Let's run it, sure enough, it's an int, this is a float. 47 00:03:06,830 --> 00:03:13,010 Now here's a little trickier test. What happens if we ask for the type of 48 00:03:13,010 --> 00:03:16,235 3.0? Now if you look at it, really it's 49 00:03:16,235 --> 00:03:19,755 kind of it's really a whole number, but somehow its kind of got this 0 fractional 50 00:03:19,755 --> 00:03:22,924 part. So in Python this is going to be a float 51 00:03:22,924 --> 00:03:26,002 in fact the way we are going to be working with float is we always have a 52 00:03:26,002 --> 00:03:31,450 decimal point here, .0. Now there's some functions that we can 53 00:03:31,450 --> 00:03:34,585 use to move back and forth between ints and floats and they're not too 54 00:03:34,585 --> 00:03:37,746 complicated. in fact, they're just a function int and 55 00:03:37,746 --> 00:03:41,392 float. So, I could convert something that is a 56 00:03:41,392 --> 00:03:48,204 floating point number into an integer in the following manner. 57 00:03:48,204 --> 00:03:50,838 [SOUND]. And so, these are going to return back 58 00:03:50,838 --> 00:03:55,820 some integers, and kind of what integer are we always going to get? 59 00:03:55,820 --> 00:03:58,687 Well, the rule is we're going to get a, the whole part of this decimal number, 60 00:03:58,687 --> 00:04:00,946 here. So, we're just going to throw away the 61 00:04:00,946 --> 00:04:06,506 part to the right of the decimal point. we can also go through and convert, an 62 00:04:06,506 --> 00:04:13,620 integer into a float. And that again seems kind of crazy, 63 00:04:13,620 --> 00:04:17,280 because, well, an integer kind of is a decimal number but what you're going to 64 00:04:17,280 --> 00:04:24,070 see is the way Python represents this is. We're always going to put .0 here. 65 00:04:24,070 --> 00:04:29,631 Okay, so when you see 3.0, that's really telling you this is a float, -1.0, that's 66 00:04:29,631 --> 00:04:34,238 a float. Now before we move on to talk about 67 00:04:34,238 --> 00:04:38,264 arithmetic operations, let me give you a few words of wisdom about dealing with 68 00:04:38,264 --> 00:04:43,844 floating point numnbers. floating point numbers are only an 69 00:04:43,844 --> 00:04:50,160 approximation to a decimal number. For example, when you do 1 divided by 3, 70 00:04:50,160 --> 00:04:54,580 you expect the decimal representation of something like 0.33333333 with 3 peats 71 00:04:54,580 --> 00:04:58,821 forever. Computers are not really graded 72 00:04:58,821 --> 00:05:05,009 representing that kind of information. In fact a number like pi doesn't even 73 00:05:05,009 --> 00:05:09,780 have kind of,1 even a nice repeating representation. 74 00:05:09,780 --> 00:05:14,395 There's a famous episode of Star Trek where Mister Spock calms rebellious 75 00:05:14,395 --> 00:05:18,188 computer. By asking the computer to compute the 76 00:05:18,188 --> 00:05:22,961 value of pi to the last decimal digit. And the computer whirs off and goes into 77 00:05:22,961 --> 00:05:26,830 an involute, desperately trying to compute the last digit of pi. 78 00:05:26,830 --> 00:05:29,170 Now, if you'd, the computer people who have built the computer have taken this 79 00:05:29,170 --> 00:05:31,198 class. They've no just have a time-out here or 80 00:05:31,198 --> 00:05:34,087 there. But the critical question is kind of what 81 00:05:34,087 --> 00:05:39,820 goes on inside Python whenever you give it a number that has lots of digits. 82 00:05:39,820 --> 00:05:42,460 What does Python do? So let my show you, I had two examples 83 00:05:42,460 --> 00:05:47,229 real quick here to help you understand. So what I've gone out to done is I've 84 00:05:47,229 --> 00:05:51,072 actually grabbed two really good approximations for pi and the square root 85 00:05:51,072 --> 00:05:55,822 of 2. So here I have, this is kind of a 50 86 00:05:55,822 --> 00:06:02,782 digit approximation of pi, and then I've also gone and grabbed a 50 digit 87 00:06:02,782 --> 00:06:12,056 approximation to the square root of 2. So I'm going to actually ask Python now 88 00:06:12,056 --> 00:06:15,112 to print these out. And when you do that, notice that I lost 89 00:06:15,112 --> 00:06:17,818 a lot of digits here. All these digits here kind of got trimmed 90 00:06:17,818 --> 00:06:21,430 down. All these digits here got trimmed down. 91 00:06:21,430 --> 00:06:25,005 So what happens inside Python is they, they represent floating point numbers 92 00:06:25,005 --> 00:06:31,530 with about 15 decimal digits of accuracy. So anything beyond that gets thrown away. 93 00:06:31,530 --> 00:06:34,726 So, in particular, occasionally when you're doing arithmetic operations using 94 00:06:34,726 --> 00:06:37,959 floating-point numbers. You're going to get answers of the form 95 00:06:37,959 --> 00:06:41,978 four point and a bunch of zeroes and then maybe a three at the end. 96 00:06:41,978 --> 00:06:45,203 What you're seeing here is called, something called floating point error. 97 00:06:45,203 --> 00:06:50,393 Whereas Python is doing the computation. It can't do the exact, precise operation 98 00:06:50,393 --> 00:06:54,406 which you're specifying. It can't compute pi to the last decimal 99 00:06:54,406 --> 00:06:58,190 point, or one-third to the last decimal point. 100 00:06:58,190 --> 00:07:02,438 So, it has to do some approximation. So, you're seeing that approximation 101 00:07:02,438 --> 00:07:11,474 error inside that computation. Alright, we've talked about numbers in 102 00:07:11,474 --> 00:07:15,186 Python lets take about the arithmatic operators you have available to do 103 00:07:15,186 --> 00:07:20,082 computations in Python. here's a list of, kind of, basic 104 00:07:20,082 --> 00:07:25,975 arithmetic operators, we have plus, minus, times, division, power, it's 105 00:07:25,975 --> 00:07:33,016 fairly straight forward. You just take two numbers and apply the 106 00:07:33,016 --> 00:07:38,887 operator to them, so we could say something like, 1 plus 2, or 3 minus 4 107 00:07:38,887 --> 00:07:49,507 are 5 times 6 are 2 to the 5th power. And if we run that, out comes 3 minus 1, 108 00:07:49,507 --> 00:07:56,225 30 and to the 5th is 32. one operator that you should pay 109 00:07:56,225 --> 00:08:01,193 attention to in Python is division. So, the, the way the division operator 110 00:08:01,193 --> 00:08:06,040 works in Python 2 is different in the way it works in Python 3. 111 00:08:06,040 --> 00:08:10,261 In Python 2, if one of the operators is a floating point number, then the result of 112 00:08:10,261 --> 00:08:16,493 division is also a floating point number. And it approximates the actual division. 113 00:08:16,493 --> 00:08:23,333 So, for example, I could say print 1.0 divided by 3 or 5.0 divided by 2.0 or 114 00:08:23,333 --> 00:08:32,227 minus 7 divided by 3.0. And what would come out when I run that 115 00:08:32,227 --> 00:08:40,345 is what I'd expect kind of a decimal approximation of 1 3rd, 5 halves and kind 116 00:08:40,345 --> 00:08:47,772 of minus 7 thirds. notice at 5, both the operators are 117 00:08:47,772 --> 00:08:52,577 actually integers. Well, Python returns kind of the integer 118 00:08:52,577 --> 00:08:56,848 version of the answer. That particular case is the kind of the 119 00:08:56,848 --> 00:08:59,860 next lowest integer after you do the exact division. 120 00:08:59,860 --> 00:09:08,980 So for example if I say print 1 divided by 3 or 5 divided by 2 or -7 divided by 121 00:09:08,980 --> 00:09:15,646 3. That, the answer comes out to be, well, 1 122 00:09:15,646 --> 00:09:21,369 integer divided by 3 is 0, we kind of round it down, 2.5, then let's see, 5 123 00:09:21,369 --> 00:09:28,642 divided by 2. Integer answer would be 2 here, we kind 124 00:09:28,642 --> 00:09:33,240 of rounded 2.5 down. We do -7 divided 3, well, integer 125 00:09:33,240 --> 00:09:37,271 division is actually minus 1 3rd. And we'll actually talk about division 126 00:09:37,271 --> 00:09:40,060 more later on, when we talk about remainders. 127 00:09:40,060 --> 00:09:43,128 And we'll have the second operator called, slash, slash is explicitly 128 00:09:43,128 --> 00:09:48,956 integer division. All right, we know about numbers, we know 129 00:09:48,956 --> 00:09:55,001 about arithmetic operators. Now, we're ready to build arithmetic 130 00:09:55,001 --> 00:09:58,810 expressions. So, the idea is fairly simple. 131 00:09:58,810 --> 00:10:03,094 An arithmetic expression is either a number or it's an arithmetic operator 132 00:10:03,094 --> 00:10:08,484 applied to two arithmetic expressions. This is kind of our first example of kind 133 00:10:08,484 --> 00:10:12,680 of recursive definition. So for example, 2 it's an arithmetic 134 00:10:12,680 --> 00:10:17,115 expression, 2 plus 3, it's an arithmetic expression. 135 00:10:17,115 --> 00:10:23,027 Because we've applied the plus operator to two arithmetic expressions, 2 and 3. 136 00:10:23,027 --> 00:10:26,524 Now in practice, you don't really need to understand that definition. 137 00:10:26,524 --> 00:10:29,824 You can just simply go through and type in expressions that you're used to using 138 00:10:29,824 --> 00:10:38,195 from say, middle school algebra. So, I could say 1 plus 2 times 3 or 4.0 139 00:10:38,195 --> 00:10:51,980 minus 5.0, divided by 6.0, or 7 times 8 plus 9 times 10. 140 00:10:51,980 --> 00:10:56,908 And if I hit Run, what comes out is exactly what I'd expect. 141 00:10:56,908 --> 00:11:00,289 now, you might kind of say well how did I go from this definition kind of involving 142 00:11:00,289 --> 00:11:04,203 an arithmetic expression. Is an arithmetic expression combined with 143 00:11:04,203 --> 00:11:07,143 operators to this thing that I just typed in that's just kind of this flat 144 00:11:07,143 --> 00:11:12,450 expression. I typed in 1 plus 2 times 3. 145 00:11:12,450 --> 00:11:19,000 So somehow Python knew that this is really 1 plus, then 2 times 3. 146 00:11:19,000 --> 00:11:20,800 Turns out there's this notion of precedence. 147 00:11:20,800 --> 00:11:23,855 You probably studied this in middle school algebra, and in fact, you probably 148 00:11:23,855 --> 00:11:28,020 have a mnemonic you learned at one point, maybe remember, which was. 149 00:11:28,020 --> 00:11:31,839 Please excuse my dear aunt sallie. So the first letter of that phrase, of 150 00:11:31,839 --> 00:11:34,691 each word of that phrase gives us the order. 151 00:11:34,691 --> 00:11:38,309 In which we should do operations when we are taking kind this linear version of an 152 00:11:38,309 --> 00:11:43,910 expression trying to think about in terms this kind of recursive definition. 153 00:11:43,910 --> 00:11:46,562 So it says that we are value an expression, we should always do 154 00:11:46,562 --> 00:11:52,130 parenthesis first and then after that we should always do exponentiation next. 155 00:11:52,130 --> 00:11:54,930 Then we should go through and do multiplication and division, M and D. 156 00:11:54,930 --> 00:11:58,347 And in fact, those have equal precedence. So if we have a sequence of those, we 157 00:11:58,347 --> 00:12:01,917 just do em from left to right. And then finally we do addition and 158 00:12:01,917 --> 00:12:08,238 subtraction last. So, for example, let's type in a 159 00:12:08,238 --> 00:12:18,610 expression here say print, 1 times 2, 1 times 2 plus 3 times 4. 160 00:12:18,610 --> 00:12:23,398 And if you look at that, the rules of Preston say we should do multiplication 161 00:12:23,398 --> 00:12:29,370 before addition, so this is really the same thing as 2 plus 12. 162 00:12:29,370 --> 00:12:33,397 So if I run that, sure enough I get 14 back for both of them. 163 00:12:33,397 --> 00:12:36,799 And notice here's kind of the real rule, which is if you're in doubt about the 164 00:12:36,799 --> 00:12:41,447 order in which operations take place. You can always just go through and use 165 00:12:41,447 --> 00:12:46,955 parenthesis. For example, if I wanted to do that 2 166 00:12:46,955 --> 00:12:56,317 plus 3 first, I can say 1 plus 1 times, 2 plus 3 times 4, that should be the same 167 00:12:56,317 --> 00:13:07,646 thing as 1 times 5, times 4. And sure enough these both evaluate to 168 00:13:07,646 --> 00:13:10,832 20. So in practice working with arithmetic in 169 00:13:10,832 --> 00:13:14,220 expressions inside Python is very intuitive. 170 00:13:14,220 --> 00:13:17,020 It's really what you, what you learn in middle school Algebra. 171 00:13:17,020 --> 00:13:20,000 So I don't think you'll find anything real too tricky here. 172 00:13:20,000 --> 00:13:24,013 So, go ahead and take a shot at it.