1 00:00:01,010 --> 00:00:06,495 Welcome back. So, we've seen how to assign variables to different values, 2 00:00:06,495 --> 00:00:08,595 for example, on line one here, 3 00:00:08,595 --> 00:00:12,825 we have code that assigns the variable x to the value six. 4 00:00:12,825 --> 00:00:14,880 What that does is, 5 00:00:14,880 --> 00:00:19,300 you can imagine a variable's values table, 6 00:00:19,520 --> 00:00:25,110 that says x now points to six. 7 00:00:25,110 --> 00:00:27,375 So, what that means is that, 8 00:00:27,375 --> 00:00:30,075 after we've assign x to six on line one, 9 00:00:30,075 --> 00:00:33,270 if we print out the value of x on line two, 10 00:00:33,270 --> 00:00:35,445 then that should print out six, 11 00:00:35,445 --> 00:00:37,845 because Python asks what's the value of x? 12 00:00:37,845 --> 00:00:39,720 It looks in this variable's values table, 13 00:00:39,720 --> 00:00:42,975 it sees that x points at six and it gets six. 14 00:00:42,975 --> 00:00:46,835 Now, what happens if we say something like on line three, 15 00:00:46,835 --> 00:00:49,120 x equals x plus one? 16 00:00:49,120 --> 00:00:50,790 Now, on first blush, 17 00:00:50,790 --> 00:00:53,465 this might look almost like a contradiction, 18 00:00:53,465 --> 00:00:56,990 but the way that Python evaluates this expression is, 19 00:00:56,990 --> 00:01:01,900 it first computes the value of this expression 20 00:01:01,900 --> 00:01:08,000 and then it takes the value of this expression and assigns it to the new value for x. 21 00:01:08,000 --> 00:01:11,420 So, in other words, what Python is first going to do is, 22 00:01:11,420 --> 00:01:14,585 it's going to ask what's the value of x? 23 00:01:14,585 --> 00:01:17,520 Python is going to get six, 24 00:01:17,520 --> 00:01:20,570 and then it takes that and adds one to it. 25 00:01:20,570 --> 00:01:22,685 So, the value of this expression, 26 00:01:22,685 --> 00:01:26,225 x plus one is going to be seven. 27 00:01:26,225 --> 00:01:28,085 So, all Python does, 28 00:01:28,085 --> 00:01:31,250 is it computes the value of x plus one to which we get 29 00:01:31,250 --> 00:01:35,600 seven and then it puts that as the new value for x. 30 00:01:35,600 --> 00:01:37,850 So, x is no longer six, 31 00:01:37,850 --> 00:01:40,430 x is now seven. 32 00:01:40,430 --> 00:01:43,355 So, now when we print x on line four, 33 00:01:43,355 --> 00:01:45,265 then this prints out seven. 34 00:01:45,265 --> 00:01:47,070 So, if we run our code, 35 00:01:47,070 --> 00:01:53,420 you will see that when we print out the value of x on line two we get six, but then, 36 00:01:53,420 --> 00:01:57,080 after reassigning x to be x plus one on line three, 37 00:01:57,080 --> 00:01:59,845 then when we print out x on line four, 38 00:01:59,845 --> 00:02:02,250 then we print out seven. 39 00:02:02,250 --> 00:02:07,205 Now, this kind of operation where we say x equals x plus one 40 00:02:07,205 --> 00:02:12,090 or whatever x's previous value was, 41 00:02:12,090 --> 00:02:14,950 we want to add one to it and reassign that to x. 42 00:02:14,950 --> 00:02:17,900 That's actually really common and for that reason, 43 00:02:17,900 --> 00:02:22,220 Python includes a shortcut for incrementing and decrementing like that. 44 00:02:22,220 --> 00:02:24,295 So, let's run through this code. 45 00:02:24,295 --> 00:02:25,645 So, on line one, 46 00:02:25,645 --> 00:02:29,450 we assign x to have the value six and then we 47 00:02:29,450 --> 00:02:33,680 print out the value of x which is going to print out six. 48 00:02:33,680 --> 00:02:40,580 On line three, we have this special syntax where we say x plus equals three. 49 00:02:40,580 --> 00:02:46,360 X plus equals three is a shortcut for saying x equals x plus three. 50 00:02:46,360 --> 00:02:51,090 Now, it allows us to just say the name of the variable x only once here. 51 00:02:51,090 --> 00:02:53,090 So, it's a little bit shorter in 52 00:02:53,090 --> 00:02:56,710 less variables especially if we have much longer variable names. 53 00:02:56,710 --> 00:03:00,960 So, again, this increments x by three and reassigns it. 54 00:03:00,960 --> 00:03:07,190 So, if we have our variables values table here. 55 00:03:07,190 --> 00:03:13,125 On line one we have assigned x to be six and then we print out that. 56 00:03:13,125 --> 00:03:18,370 On line three, we say x equals x plus three by saying x plus equals three. 57 00:03:18,370 --> 00:03:22,745 So, x is no longer six x instead becomes nine. 58 00:03:22,745 --> 00:03:26,515 Then, when we print out x this prints out nine, 59 00:03:26,515 --> 00:03:29,195 then we can do the same thing with subtraction. 60 00:03:29,195 --> 00:03:36,020 So, we can say x minus equals one as a shortcut for saying x equals x minus one, 61 00:03:36,020 --> 00:03:40,760 and so that's going to take x from nine and it's going to 62 00:03:40,760 --> 00:03:45,680 reassign it to the value eight and now when we print out the value of x, 63 00:03:45,680 --> 00:03:48,170 then we are going to print out eight. 64 00:03:48,170 --> 00:03:52,590 So, you can see x goes from six to nine to eight. 65 00:03:52,970 --> 00:03:57,260 So, we can do reassignment as many times as we want. 66 00:03:57,260 --> 00:03:59,195 So, we can say s equals one, 67 00:03:59,195 --> 00:04:00,530 and then we can add two to it, 68 00:04:00,530 --> 00:04:01,865 then we can add three to that, 69 00:04:01,865 --> 00:04:03,755 and four to that etc. 70 00:04:03,755 --> 00:04:08,150 If I run this code, you'll see the different values of s. So, 71 00:04:08,150 --> 00:04:09,230 s starts out as one, 72 00:04:09,230 --> 00:04:11,180 when we add two to that it becomes three, 73 00:04:11,180 --> 00:04:13,070 when we add three to that it becomes six, 74 00:04:13,070 --> 00:04:16,205 when we add four to that it becomes ten and so on. 75 00:04:16,205 --> 00:04:17,710 Later on in this course, 76 00:04:17,710 --> 00:04:23,070 you will find a much shorter way to actually do something like this. 77 00:04:23,520 --> 00:04:27,110 Now, let's run through some multiple choice questions, 78 00:04:27,110 --> 00:04:30,215 what gets printed out when the following statements execute? 79 00:04:30,215 --> 00:04:32,405 So, here, we assign x to be 12. 80 00:04:32,405 --> 00:04:37,910 So, x has the value 12 and then we say x equals x minus one. 81 00:04:37,910 --> 00:04:44,480 So, the value of this expression we replace x with 12 and we 82 00:04:44,480 --> 00:04:51,545 subtract one from 12 to get 11 and we take 11 and that's x's new value. 83 00:04:51,545 --> 00:04:53,210 So, x is no longer 12, 84 00:04:53,210 --> 00:04:54,965 x is now 11. 85 00:04:54,965 --> 00:05:04,565 So, when we print out x we are going to print out 11 or C. Next question, 86 00:05:04,565 --> 00:05:07,145 what gets printed when the following statements execute? 87 00:05:07,145 --> 00:05:11,870 So, here we first assign x to be 12. 88 00:05:11,870 --> 00:05:15,410 Then we say, x equals it's previous value minus three, 89 00:05:15,410 --> 00:05:18,215 sin x goes from 12 to nine. 90 00:05:18,215 --> 00:05:20,945 Then we say it's previous value plus five. 91 00:05:20,945 --> 00:05:24,530 So, x goes from nine to 14. 92 00:05:24,530 --> 00:05:27,450 So, then we say x equals x plus one, 93 00:05:27,450 --> 00:05:30,735 so it goes from 14 to 15. 94 00:05:30,735 --> 00:05:41,680 So, x ends up with the value 15 or C. Next question, 95 00:05:41,890 --> 00:05:48,170 construct code that will result in the value 134 being printed. 96 00:05:48,170 --> 00:05:51,005 So, the first thing that we want to do, 97 00:05:51,005 --> 00:05:54,445 is we want to assign an initial value for bank balance. 98 00:05:54,445 --> 00:05:56,265 If I try to put this first, 99 00:05:56,265 --> 00:05:59,180 then Python when trying to evaluate the value of 100 00:05:59,180 --> 00:06:03,650 this expression would say that it didn't find a variable named mybankbalance. 101 00:06:03,650 --> 00:06:06,575 So, I know that this line can't come first. 102 00:06:06,575 --> 00:06:10,050 Here, I can do the assignment first, 103 00:06:10,050 --> 00:06:14,930 so I can assign my bank balance to 100 and then I can reassign it like I do here. 104 00:06:14,930 --> 00:06:19,145 So, this looks valid and by the end of running these two lines, 105 00:06:19,145 --> 00:06:23,125 then mybankbalance is going to have the value 134, 106 00:06:23,125 --> 00:06:28,055 and then if we print out the value of my bank balance on line three, 107 00:06:28,055 --> 00:06:31,560 then we're going to print out 134. 108 00:06:33,310 --> 00:06:38,305 Next question, which of the following statements are equivalent? 109 00:06:38,305 --> 00:06:44,390 So, I see this statement reassigns x to it's previous value plus y. 110 00:06:44,390 --> 00:06:47,765 This statement assigns y instead of x. 111 00:06:47,765 --> 00:06:50,585 So, I know that this can't be equivalent to 112 00:06:50,585 --> 00:06:56,960 statement A. X plus equals x plus y looks like it's similar to statement A, 113 00:06:56,960 --> 00:06:59,990 but what that's really saying because we have plus equals here, 114 00:06:59,990 --> 00:07:04,600 is x equals x plus x plus y. 115 00:07:04,600 --> 00:07:07,245 So, here we have that extra x plus, 116 00:07:07,245 --> 00:07:10,455 so that's not equivalent to statement A, 117 00:07:10,455 --> 00:07:13,505 but statement D is. 118 00:07:13,505 --> 00:07:15,800 Then statement E just won't work. 119 00:07:15,800 --> 00:07:19,740 So, statement's A and D are equivalent. 120 00:07:19,880 --> 00:07:23,710 That's all for now until next time.