WEBVTT 1 00:00:02.160 --> 00:00:03.040 So in the previous video, 2 00:00:03.040 --> 00:00:06.880 we stored some strings in variables but we didn't really explain what was going on. 3 00:00:06.880 --> 00:00:07.790 So we wanna go through and 4 00:00:07.790 --> 00:00:11.540 actually explain what is happening a little bit more now behind the scenes. 5 00:00:11.540 --> 00:00:13.360 So when the Python programming is running, 6 00:00:13.360 --> 00:00:17.200 everything the program needs ends up being stored somewhere in the computer's memory. 7 00:00:17.200 --> 00:00:19.870 You don't actually need to know where that is because the computer 8 00:00:19.870 --> 00:00:20.730 actually tracks that. 9 00:00:20.730 --> 00:00:24.617 For the program code itself, in other words the instructions that you type in, 10 00:00:24.617 --> 00:00:28.503 will be stored in one area of memory, but also the data that it's gonna work on, 11 00:00:28.503 --> 00:00:33.000 the variables that you've typed in etc., will be stored somewhere else in memory. 12 00:00:33.000 --> 00:00:33.990 So the variable, 13 00:00:33.990 --> 00:00:37.760 the name that we assigned is just really a way to give a meaningful name to 14 00:00:37.760 --> 00:00:42.270 a area of memory into which we can place certain values like strings and so forth. 15 00:00:42.270 --> 00:00:46.915 So when we create a variable called greeting like greeting = Bruce. 16 00:00:48.205 --> 00:00:52.085 Python's actually allocating an area of computer memory for us, and 17 00:00:52.085 --> 00:00:55.765 it knows to refer to that area whenever we talk about the variable greeting. 18 00:00:55.765 --> 00:01:00.095 So think of it like a box in memory that's storing the values, Bruce in this case, 19 00:01:00.095 --> 00:01:03.335 and the box has got a name on, a sticker on it that says greeting. 20 00:01:03.335 --> 00:01:06.305 And when we type in greeting, the computer knows to go and 21 00:01:06.305 --> 00:01:08.625 find the value that's in that particular box. 22 00:01:08.625 --> 00:01:11.260 So that all happens automatically behind the scenes. 23 00:01:11.260 --> 00:01:14.470 Just in terms of creating these variable names, 24 00:01:14.470 --> 00:01:17.610 there are a few rules to keep in mind when you actually created them. 25 00:01:17.610 --> 00:01:21.740 Firstly, the actual name has to start with either a letter, it could be upper or 26 00:01:21.740 --> 00:01:24.020 lower case, or an underscore. 27 00:01:24.020 --> 00:01:30.135 So in other words, we can type _myName = Tim, that's valid. 28 00:01:30.135 --> 00:01:33.012 So a variable name can start with either the letter or an underscore but 29 00:01:33.012 --> 00:01:34.298 we can't start with a number. 30 00:01:37.392 --> 00:01:38.560 Actually get an error. 31 00:01:38.560 --> 00:01:41.730 But we can actually also use letters. 32 00:01:41.730 --> 00:01:44.670 We can also use numbers in our variable names, just not as the first letter. 33 00:01:44.670 --> 00:01:47.370 So, we can put something like Tim45 = Good. 34 00:01:47.370 --> 00:01:50.517 That's quite valid and we could put a combination. 35 00:01:50.517 --> 00:01:55.975 So we could put Tim_Was_57 = Hello and 36 00:01:55.975 --> 00:01:59.770 that's quite valid as well. 37 00:01:59.770 --> 00:02:02.640 Now the other important thing to consider here is that I could also 38 00:02:02.640 --> 00:02:05.610 type a variable name called Greeting, with uppercase G. 39 00:02:05.610 --> 00:02:10.420 And that's different and distinct to this variable name. 40 00:02:10.420 --> 00:02:13.730 So that one's all in lowercase, this one's got an uppercase first letter. 41 00:02:13.730 --> 00:02:15.400 Even though the actual word is the same, 42 00:02:15.400 --> 00:02:20.490 they are treated as different variable names as far as Python is concerned. 43 00:02:20.490 --> 00:02:22.360 Now in terms of the allocation of memory, 44 00:02:22.360 --> 00:02:25.130 it actually happens at the point that you've initialized it. 45 00:02:25.130 --> 00:02:28.717 So in other words once you've talked in the variable name and put an equal and 46 00:02:28.717 --> 00:02:31.743 then the value, at that point python will allocate the space for 47 00:02:31.743 --> 00:02:33.650 that in the computers memory. 48 00:02:33.650 --> 00:02:36.530 In terms of using variables we saw that we could 49 00:02:36.530 --> 00:02:38.070 use the prints statement to print some out. 50 00:02:38.070 --> 00:02:43.316 We could do something like TIm was here, or 51 00:02:43.316 --> 00:02:49.900 Tim was 57 + a single-quote space plus greeting. 52 00:02:49.900 --> 00:02:53.420 We could run thatm, Hello Bruce. 53 00:02:53.420 --> 00:02:56.730 Obviously it's taking the value of Hello from Tim_Was_57 and 54 00:02:56.730 --> 00:02:58.630 Bruce from the variable greeting and 55 00:02:58.630 --> 00:03:02.970 actually printing that on the screen with a space between the two there. 56 00:03:02.970 --> 00:03:06.580 We could do something like age equals 24, print age. 57 00:03:08.480 --> 00:03:13.300 We can also do something like greeting plus age, since we're on this program. 58 00:03:14.640 --> 00:03:16.500 Now this is where we actually get an error. 59 00:03:16.500 --> 00:03:19.270 So you can see it should the first Hello Bruce which we saw before, 60 00:03:19.270 --> 00:03:20.680 which was this first line. 61 00:03:20.680 --> 00:03:24.350 We then assigned a value of 24 to the variable age, and we printed the age, 62 00:03:24.350 --> 00:03:24.850 which 24 worked. 63 00:03:24.850 --> 00:03:29.200 We're going to try to do this next line, print greeting plus age. 64 00:03:29.200 --> 00:03:30.620 And we actually got an error in red. 65 00:03:30.620 --> 00:03:34.790 And you can see that Python's actually trying to help you here by actually sort 66 00:03:34.790 --> 00:03:36.850 of saying, look I've got a problem with what you've typed. 67 00:03:36.850 --> 00:03:41.200 It's actually come up and actually said, I can't convert int object to a string. 68 00:03:41.200 --> 00:03:44.470 Str is the abbreviation of string implicitly. 69 00:03:44.470 --> 00:03:47.595 So what we've tried to do is Get it to do something that Python can't work with, and 70 00:03:47.595 --> 00:03:50.165 this is what happens when you get an error. 71 00:03:50.165 --> 00:03:53.865 So Python will hopefully show you some information which hopefully leads to you 72 00:03:53.865 --> 00:03:57.875 figuring out what the problem was, and it actually shows also the actual lines. 73 00:03:57.875 --> 00:04:02.000 So if we move over a little bit you can see it's saying the name of the Python 74 00:04:02.000 --> 00:04:06.548 file which was one that I created for this video varibles.py py. 75 00:04:06.548 --> 00:04:10.120 And it says that line 12, which equates to line 12 over here. 76 00:04:10.120 --> 00:04:13.210 So it's very handy to look at these when you trying to figure out whether there's 77 00:04:13.210 --> 00:04:17.239 a problem, and you can go back to line 12, and know this was where the problem was. 78 00:04:18.360 --> 00:04:21.310 Now the other thing is that IntelliJ actually does a lot of this for 79 00:04:21.310 --> 00:04:24.950 you automatically as well, it tries to help you out with hints as well. 80 00:04:24.950 --> 00:04:27.630 So, over here we can see these little dashes. 81 00:04:27.630 --> 00:04:29.240 If I actually hover over there with my mouse, 82 00:04:29.240 --> 00:04:31.175 we actually get a helpful error message. 83 00:04:31.175 --> 00:04:34.465 Expected type string str got int instead. 84 00:04:34.465 --> 00:04:37.295 So that's IntelliJ, trying to give you some information to say, 85 00:04:37.295 --> 00:04:38.855 you've got this wrong, basically. 86 00:04:38.855 --> 00:04:43.115 And so it can be really useful when you're creating Python programs to keep 87 00:04:43.115 --> 00:04:46.035 checking these hints out to see whether there actually is an issue. 88 00:04:46.035 --> 00:04:47.725 Notice also there's a keyboard shortcut, 89 00:04:47.725 --> 00:04:50.025 if you want to expand this to get some more information. 90 00:04:50.025 --> 00:04:55.250 So on a Mac, I can actually type Cmd + F1, I get more information like so. 91 00:04:55.250 --> 00:04:58.780 And press Ctrl + F1 on Windows machine or a Linux machine and 92 00:04:58.780 --> 00:04:59.818 you can get the full message. 93 00:04:59.818 --> 00:05:02.910 So that's very handy to refer to that all the time, and 94 00:05:02.910 --> 00:05:05.800 later in the course we'll be looking at handling these errors. 95 00:05:05.800 --> 00:05:08.620 In other words, if you're program crashes or goes to crash, 96 00:05:08.620 --> 00:05:10.740 we can handle that more gracefully. 97 00:05:10.740 --> 00:05:14.220 But for now, think of those error messages that come up as your friend because what 98 00:05:14.220 --> 00:05:17.320 it's really doing, it's telling you that your code is not gonna work and 99 00:05:17.320 --> 00:05:20.040 providing as much information as possible to you to help you try and 100 00:05:20.040 --> 00:05:21.380 figure out what the solution is. 101 00:05:21.380 --> 00:05:24.410 So what can be frustrating, you really did learn a lot from errors like these, 102 00:05:24.410 --> 00:05:26.820 because you can figure out something that Python doesn't like, and 103 00:05:26.820 --> 00:05:29.910 how to work around that to come up with actually a solution. 104 00:05:29.910 --> 00:05:31.020 Okay, so moving on, 105 00:05:31.020 --> 00:05:34.240 and later in the course we'll be talking about these conversions. 106 00:05:34.240 --> 00:05:37.490 So, in terms of this particular error message, what's actually happening was 107 00:05:37.490 --> 00:05:42.810 again we determined that Python wasn't happy because we're trying to add the age, 108 00:05:42.810 --> 00:05:46.680 the numeric variable age to a number, and you can't actually do that. 109 00:05:46.680 --> 00:05:48.310 It doesn't actually work like that. 110 00:05:48.310 --> 00:05:50.280 So Python doesn't know what to do in that scenario. 111 00:05:50.280 --> 00:05:52.890 It knows what to do in the situation like this, 112 00:05:52.890 --> 00:05:54.262 we're actually adding strings together. 113 00:05:54.262 --> 00:05:56.690 Because Tim_Was_57 is a string, and 114 00:05:56.690 --> 00:06:00.230 greeting was a string and it knows to concatenate those. 115 00:06:00.230 --> 00:06:03.100 Likewise, if we tried it to do something with two numbers, 116 00:06:03.100 --> 00:06:05.320 it knows to add to some of those two numbers. 117 00:06:05.320 --> 00:06:07.340 But in this scenario where we've got a string and 118 00:06:07.340 --> 00:06:09.580 a number, Python doesn't actually know how to do that. 119 00:06:09.580 --> 00:06:12.370 Not know how to handle that and that's why we actually got the message. 120 00:06:12.370 --> 00:06:14.880 So what we dealing more with actually how to get around that. 121 00:06:14.880 --> 00:06:18.310 And do note that some other programming languages, Java being one of them, 122 00:06:18.310 --> 00:06:20.900 does actually allow you to actually do something like that. 123 00:06:20.900 --> 00:06:24.790 And in that scenario what would happen is it would actually automatically convert 124 00:06:24.790 --> 00:06:25.600 what you're trying to do. 125 00:06:25.600 --> 00:06:29.480 In this case, it would append the value of 24 to the string age. 126 00:06:29.480 --> 00:06:32.560 But Python doesn't do that, it makes you get it right at the start. 127 00:06:32.560 --> 00:06:35.250 And that's actually a good thing in many ways because it means you're not 128 00:06:35.250 --> 00:06:37.960 going to be getting any weird little bugs that crept about 129 00:06:37.960 --> 00:06:39.960 because of automatic type conversion. 130 00:06:39.960 --> 00:06:41.570 That you may get in other programming languages. 131 00:06:41.570 --> 00:06:44.150 So the bottom line is, you need to get that right first, and 132 00:06:44.150 --> 00:06:47.440 really it's just a matter of checking out the error message and fixing it. 133 00:06:47.440 --> 00:06:50.470 And we're talking more about this type of thing as the course progresses anyway. 134 00:06:50.470 --> 00:06:53.390 So moving on, let's talk now a little bit about variable types. 135 00:06:53.390 --> 00:06:55.940 So Python has got several built-in data types. 136 00:06:55.940 --> 00:06:58.440 We've got a heap of them and we'll be dealing with those in detail. 137 00:06:58.440 --> 00:07:03.130 Data types can be classified as numerics, sequences, mappings, files, 138 00:07:03.130 --> 00:07:05.450 classes, instances, and exceptions. 139 00:07:05.450 --> 00:07:08.673 And I [LAUGH] know that sounds like a lot and it probably is but 140 00:07:08.673 --> 00:07:11.750 you'll be introduced to all of these as the course progresses. 141 00:07:11.750 --> 00:07:14.510 But for now we're gonna look at numeric dial tops and 142 00:07:14.510 --> 00:07:16.480 one of the sequence stops which you've already seen. 143 00:07:16.480 --> 00:07:19.318 String being a sequence stop, which we've actually looked at before. 144 00:07:19.318 --> 00:07:23.820 Now Python 2, the older version of python which as I mention is no longer 145 00:07:23.820 --> 00:07:25.720 being used and we're not using it in this course. 146 00:07:25.720 --> 00:07:30.720 That actually had various dial tops such as int, long, float, and complex, 147 00:07:30.720 --> 00:07:34.840 but in Python 3 the change they've made is there's no longer a long type, and 148 00:07:34.840 --> 00:07:37.000 the int never has like a long used to. 149 00:07:37.000 --> 00:07:40.020 So they've really consolidated the amount of data tops. 150 00:07:40.020 --> 00:07:42.670 Now we're not going to discuss complex numbers 151 00:07:42.670 --> 00:07:44.040 without going into too much detail. 152 00:07:44.040 --> 00:07:46.850 A complex number contains a real and 153 00:07:46.850 --> 00:07:49.460 an imaginary part based on the square root of minus one. 154 00:07:49.460 --> 00:07:53.030 Now, if you understand a complex number and wanna use Phyton to manipulate them, 155 00:07:53.030 --> 00:07:56.860 then by the end of the course you'll understand Phyton well enough to do so. 156 00:07:56.860 --> 00:08:01.180 But really the complex number theories in advance branch of mathematics slash 157 00:08:01.180 --> 00:08:04.500 engineering, it's not really appropriate to go into too much detail with that. 158 00:08:04.500 --> 00:08:07.100 But let's look at the basic data types, and 159 00:08:07.100 --> 00:08:09.660 a basic data type we're going to start with an integer. 160 00:08:09.660 --> 00:08:13.530 An integer being a whole number so a number that hasn't got any decimal points. 161 00:08:13.530 --> 00:08:15.930 First, you can see on the screen I have typed in an integer. 162 00:08:15.930 --> 00:08:17.480 So an integer is a just whole number. 163 00:08:17.480 --> 00:08:21.010 It's a number having no fractional part where as a float 164 00:08:21.010 --> 00:08:24.190 in the other example is another name for a real number. 165 00:08:24.190 --> 00:08:27.810 That is a number having a fractional part after the decimal point. 166 00:08:27.810 --> 00:08:32.015 There is a very small number of computer languages that make no distinction between 167 00:08:32.015 --> 00:08:33.720 real numbers and integers. 168 00:08:33.720 --> 00:08:36.490 I guess you could say that integers can be considered 169 00:08:36.490 --> 00:08:38.210 just special cases of real numbers. 170 00:08:38.210 --> 00:08:41.380 But when represented in a computer, computations using integers, 171 00:08:41.380 --> 00:08:45.250 these whole numbers, are significantly faster than using floating point numbers. 172 00:08:45.250 --> 00:08:48.280 So that's one of the main reasons that we actually distinguish in programming 173 00:08:48.280 --> 00:08:50.710 an integer as a whole number and a real number, 174 00:08:50.710 --> 00:08:53.820 a floating point number and that's because it's a lot faster to process. 175 00:08:53.820 --> 00:08:57.560 So anything that doesn't need, as a general rule, a floating point number for 176 00:08:57.560 --> 00:09:00.250 calculation purposes, you shouldn't actually be using it. 177 00:09:00.250 --> 00:09:03.630 Because you'll find that your programs will operate a lot faster because of it. 178 00:09:03.630 --> 00:09:07.670 There are pros and cons, because of the way that integers are stored in 179 00:09:07.670 --> 00:09:10.870 the computer's memory, there's actually a limit to the size of an int. 180 00:09:10.870 --> 00:09:13.900 It's about 9 trillion in European terms. 181 00:09:13.900 --> 00:09:16.850 I think it's nine quintillion, in American format. 182 00:09:16.850 --> 00:09:20.254 But effectively the Python 2 long data type the Python 3, 183 00:09:20.254 --> 00:09:22.860 effectively have no real maximum size. 184 00:09:22.860 --> 00:09:24.540 Now floating point numbers on the other hand, 185 00:09:24.540 --> 00:09:28.140 that's floats, they're used to represent numbers having a fractional part. 186 00:09:28.140 --> 00:09:32.610 So the maximum float value on a 64 bit computer is, well that's, it's just huge, 187 00:09:32.610 --> 00:09:34.250 we'll go through it, but it's quite large. 188 00:09:34.250 --> 00:09:38.590 Basically a number that actually moves the decimal point 308 places to the right. 189 00:09:38.590 --> 00:09:42.800 And the smallest float is a negative number which has 307 zeros before 190 00:09:42.800 --> 00:09:44.250 the decimal point. 191 00:09:44.250 --> 00:09:47.985 So, it's obviously a huge variance and 192 00:09:47.985 --> 00:09:50.695 a huge number can be stored in that floating point number. 193 00:09:50.695 --> 00:09:54.145 Floating points have 52 digits of precision, which should be adequate for 194 00:09:54.145 --> 00:09:57.465 most purposes from financial calculations where you're needing decimals. 195 00:09:57.465 --> 00:10:00.940 And because Python doesn't really have variable declarations, 196 00:10:00.940 --> 00:10:04.810 that's where you specify the type of a variable before you can use it, and 197 00:10:04.810 --> 00:10:07.030 lots of other computer languages do that. 198 00:10:07.030 --> 00:10:10.830 It's probably tempting to think that you don't need to understand the difference 199 00:10:10.830 --> 00:10:11.870 between an int, a long, and a float type, but 200 00:10:11.870 --> 00:10:14.980 really it is something you really need to consider when running your programs. 201 00:10:14.980 --> 00:10:17.240 This makes more sense when we look at some of the operators that can be 202 00:10:17.240 --> 00:10:18.100 performed on numbers. 203 00:10:18.100 --> 00:10:19.900 So let's just go through the first few scenarios. 204 00:10:19.900 --> 00:10:23.340 So I typed a variable called a with an integer value of 12. 205 00:10:23.340 --> 00:10:26.310 Let's do another one, b = 3. 206 00:10:26.310 --> 00:10:28.530 And let's perform some calculations. 207 00:10:28.530 --> 00:10:36.317 We'll do print, we'll do a plus b, we should get the value of 15, 208 00:10:36.317 --> 00:10:41.780 a take b, we should get the value of nine. 209 00:10:41.780 --> 00:10:47.258 Print, a times b, the value of 36, we should get and we'll test this shortly. 210 00:10:47.258 --> 00:10:50.390 a divided by b, we should get the answer four, but 211 00:10:50.390 --> 00:10:52.980 there's a special rule there about that which I will talk about shortly. 212 00:10:52.980 --> 00:10:58.500 And we're going to introduce another operator, //b and 213 00:10:58.500 --> 00:11:01.545 I'll explain that shortly, and last one we're going to use is the remainder, 214 00:11:01.545 --> 00:11:06.190 remainder of a divided by b effectively. 215 00:11:06.190 --> 00:11:10.620 Otherwise dividing 12 by 3, how much remainder is left after that. 216 00:11:10.620 --> 00:11:13.180 So let's just run this, and then we'll just go through it. 217 00:11:13.180 --> 00:11:14.610 So the first one is 15. 218 00:11:14.610 --> 00:11:15.300 The second one is 9. 219 00:11:15.300 --> 00:11:16.940 And the third value is 36. 220 00:11:16.940 --> 00:11:19.470 And there's the fourth one, a divided by b. 221 00:11:19.470 --> 00:11:24.220 The actual answer is returned as a float, with a decimal point, .0. 222 00:11:24.220 --> 00:11:27.350 And that's the thing to keep in mind, with a divide, 223 00:11:27.350 --> 00:11:32.040 a division operation, in Python, by default it returns as a float. 224 00:11:32.040 --> 00:11:34.740 If you want the result to return as an integer, 225 00:11:34.740 --> 00:11:37.850 a whole number, you need to use the two slashes in this case. 226 00:11:37.850 --> 00:11:41.220 So a, slash slash b, returns four as a whole number. 227 00:11:41.220 --> 00:11:41.980 And lastly, 228 00:11:41.980 --> 00:11:47.160 the remainder of a divided by b was zero because b goes into a four times. 229 00:11:47.160 --> 00:11:49.060 Four times three is twelve and there would be no remainder. 230 00:11:49.060 --> 00:11:52.380 So that's how you do some basic calculations in Python. 231 00:11:52.380 --> 00:11:55.690 Now this slash and slash slash can really trip you up, and 232 00:11:55.690 --> 00:11:57.840 we're gonna be talking about loops later. 233 00:11:57.840 --> 00:12:00.890 But I just want to give you an example of typing in a loop where you could get into 234 00:12:00.890 --> 00:12:02.950 difficulty without using that slash or 235 00:12:02.950 --> 00:12:06.460 slash slash because it can be a common source of problems. 236 00:12:06.460 --> 00:12:07.560 So let's at an example here. 237 00:12:07.560 --> 00:12:10.634 We're gonna type in some code, and again we'll talk more about this for, and 238 00:12:10.634 --> 00:12:11.765 the concept of loops later. 239 00:12:11.765 --> 00:12:16.797 But for now we'll just type in for 240 00:12:16.797 --> 00:12:21.842 i in range, 1, 4, : print i. 241 00:12:21.842 --> 00:12:25.250 So if we run this, You get the values 1, 2, 3. 242 00:12:25.250 --> 00:12:30.810 We've actually changed this here and put a divided by b, cuz instead 243 00:12:30.810 --> 00:12:34.120 of putting a constant number, a literal, we can actually put an expression like so. 244 00:12:34.120 --> 00:12:37.610 If we run that, we actually get an error. 245 00:12:37.610 --> 00:12:40.610 And that's because float object cannot be interpreted as an integer, 246 00:12:40.610 --> 00:12:41.880 and that's because tha answer, 247 00:12:41.880 --> 00:12:44.850 if you remember, with division by default is a floating point number. 248 00:12:44.850 --> 00:12:46.400 The range actually needs an integer, so 249 00:12:46.400 --> 00:12:48.780 we'd actually put two slashes in that scenario. 250 00:12:48.780 --> 00:12:52.760 Run the code again, it actually correctly returns the results one, two, and three. 251 00:12:52.760 --> 00:12:55.360 As we progress through the course, you'll see other ways of actually handling this, 252 00:12:55.360 --> 00:12:56.680 but I just wanted to point that out. 253 00:12:56.680 --> 00:12:59.500 At this point in time, that's a common source of problems when you're 254 00:12:59.500 --> 00:13:00.820 looking at Python for the first time. 255 00:13:00.820 --> 00:13:03.570 And again, don't be too worried about the concepts of what this is doing, 256 00:13:03.570 --> 00:13:06.970 we'll be covering loops, which is what this actually is In future videos. 257 00:13:06.970 --> 00:13:10.500 So the code something like a plus b, that's actually an expression and 258 00:13:10.500 --> 00:13:11.700 I mentioned expressions. 259 00:13:11.700 --> 00:13:15.890 In Python an expression is anything that can be calculated to return a value. 260 00:13:15.890 --> 00:13:19.090 So we've really only used simple expressions to date like a plus b or 261 00:13:19.090 --> 00:13:21.410 a take b but we can get a lot more complex. 262 00:13:21.410 --> 00:13:23.510 We could do something, just let me clear some space here. 263 00:13:24.590 --> 00:13:28.158 So a lot more complex if we want to, 264 00:13:28.158 --> 00:13:34.186 we can do something like print (a + b / 3- 4 * 12), 265 00:13:34.186 --> 00:13:38.670 and that's quite a valid expression. 266 00:13:38.670 --> 00:13:42.460 So if we actually run this, you might be surprised to look at that and think, 267 00:13:42.460 --> 00:13:44.630 why is that come up with a value of 35? 268 00:13:44.630 --> 00:13:47.966 You'd expect looking at that, that the answer should actually be 12. 269 00:13:47.966 --> 00:13:54.849 It should be a plus b is 15 divided by 3 is 5, take 4 is 1, times 12 is 12. 270 00:13:54.849 --> 00:13:56.645 So why have we actually got 35 here instead of 12? 271 00:13:56.645 --> 00:14:00.030 [LAUGH] Now don't worry, your computer's not broken. 272 00:14:00.030 --> 00:14:02.790 And you can reliably perform arithmetic in Python. 273 00:14:02.790 --> 00:14:05.225 But, as is the case in all programming languages, 274 00:14:05.225 --> 00:14:09.145 you have to understand some of the basic rules of the arithmetic operators. 275 00:14:09.145 --> 00:14:11.425 Out problem here is operator precedence. 276 00:14:11.425 --> 00:14:14.120 Now operator precedence is a fancy term for 277 00:14:14.120 --> 00:14:16.750 the relative importance given to each of the operators. 278 00:14:16.750 --> 00:14:20.673 If all operators were equal, we'll get the expression how I read it earlier. 279 00:14:20.673 --> 00:14:28.048 By calculating a plus b is 15 divided by three is 5 take 4 is 1 time 12 is 12. 280 00:14:28.048 --> 00:14:30.435 But the thing to remember is multiplication and 281 00:14:30.435 --> 00:14:34.263 division have higher operator precedence than addition and subtraction. 282 00:14:34.263 --> 00:14:36.480 So those operators are performed first. 283 00:14:36.480 --> 00:14:38.440 And most languages you'll find working the same way so 284 00:14:38.440 --> 00:14:39.830 this is not just peculiar to Python. 285 00:14:39.830 --> 00:14:44.300 So what's actually happening is b divided by 3, this is actually happening first. 286 00:14:44.300 --> 00:14:48.650 So, b divided by 3 is happening first, which is 1. 287 00:14:48.650 --> 00:14:50.580 Then 4 times 12 is added, which is 48. 288 00:14:50.580 --> 00:14:54.810 So the expression is actually evaluated as 12 plus one is 13, take 48, 289 00:14:54.810 --> 00:14:56.120 which is negative 35. 290 00:14:56.120 --> 00:15:01.240 That's weird, but that's the way it actually happens by default 291 00:15:01.240 --> 00:15:05.200 because multiplication and division, as I mentioned, have got a higher precedence. 292 00:15:05.200 --> 00:15:08.620 So the case where you've actually got expressions that use just those operators, 293 00:15:08.620 --> 00:15:10.060 they will actually work from left to right. 294 00:15:10.060 --> 00:15:12.920 So we can type a divided by two times three, 295 00:15:12.920 --> 00:15:16.140 and that should give us the answer of four times three is 12. 296 00:15:16.140 --> 00:15:19.703 So we run that, we actually get the answer of twelve and 297 00:15:19.703 --> 00:15:23.270 even something like eight times three divided by two. 298 00:15:23.270 --> 00:15:27.200 24 divided by two is 12, we get the answer correct. 299 00:15:27.200 --> 00:15:29.370 Then the best way to actually overcome this, 300 00:15:29.370 --> 00:15:34.325 if you want your expressions to be clear and unambiguous to use parenthesis freely, 301 00:15:34.325 --> 00:15:36.165 even when Python doesn't specifically require them. 302 00:15:36.165 --> 00:15:39.554 So, in our earlier example, the one that didn't work, this first line here. 303 00:15:39.554 --> 00:15:44.576 So we could do something like, print, 304 00:15:44.576 --> 00:15:51.592 a plus b, divide it by three, take four, times 12. 305 00:15:51.592 --> 00:15:54.742 Now that's a lot of brackets there but what we're doing is we're actually putting 306 00:15:54.742 --> 00:15:57.764 the expressions in parentheses to make it clear what we're trying to perform. 307 00:15:57.764 --> 00:16:01.137 And when we actually run it we're actually correct, you now have the answer 12. 308 00:16:01.137 --> 00:16:05.340 So you can see we've got a plus b in its own set of brackets, that's gonna be 15. 309 00:16:05.340 --> 00:16:08.120 But then outside to that there's this bracket here, which covers that. 310 00:16:08.120 --> 00:16:12.695 So therefore, that calculation is now 15 divided by three, which is three. 311 00:16:12.695 --> 00:16:15.935 Then take four from that is one, then multiply by 12, is 12, 312 00:16:15.935 --> 00:16:17.675 and that's why we're getting the answer of 12. 313 00:16:17.675 --> 00:16:18.945 Okay, so I'm gonna stop the video here. 314 00:16:18.945 --> 00:16:22.575 In the next video we're gonna continue on talking about variables and 315 00:16:22.575 --> 00:16:23.775 operator precedence etc. 316 00:16:23.775 --> 00:16:25.185 So I'll see you in the next video.