1 00:00:00,150 --> 00:00:02,520 - Let's trace a program step by step. 2 00:00:02,520 --> 00:00:04,050 This is a common pattern we'll use 3 00:00:04,050 --> 00:00:06,750 to understand what the computer is doing under the hood 4 00:00:06,750 --> 00:00:08,490 when we press the Run button. 5 00:00:08,490 --> 00:00:10,710 Tracing program execution like this 6 00:00:10,710 --> 00:00:12,600 helps us better read and write programs 7 00:00:12,600 --> 00:00:14,400 because we can start to predict 8 00:00:14,400 --> 00:00:16,590 what the computer's going to do with each instruction 9 00:00:16,590 --> 00:00:17,640 without having to go through 10 00:00:17,640 --> 00:00:19,770 a long process of trial and error. 11 00:00:19,770 --> 00:00:21,660 For now, we'll write this all out together 12 00:00:21,660 --> 00:00:23,340 so we can make sense of what's happening here, 13 00:00:23,340 --> 00:00:26,010 but eventually, we'll get familiar enough with Python 14 00:00:26,010 --> 00:00:27,820 where we can trace small blocks of code like this 15 00:00:27,820 --> 00:00:29,013 in our heads. 16 00:00:30,360 --> 00:00:31,410 When you press Run, 17 00:00:31,410 --> 00:00:33,690 the computer looks at your program line by line. 18 00:00:33,690 --> 00:00:35,580 It isn't smart enough to zoom out 19 00:00:35,580 --> 00:00:38,130 and look at the big picture and try and understand 20 00:00:38,130 --> 00:00:40,590 what the program is trying to do as a whole. 21 00:00:40,590 --> 00:00:43,350 When I say line by line, I literally mean 22 00:00:43,350 --> 00:00:46,530 that the computer is going to load each line of your program 23 00:00:46,530 --> 00:00:48,960 into its working memory one by one. 24 00:00:48,960 --> 00:00:50,550 And then with one instruction 25 00:00:50,550 --> 00:00:51,840 loaded into its working memory, 26 00:00:51,840 --> 00:00:54,660 it's going to interpret that instruction in isolation, 27 00:00:54,660 --> 00:00:57,000 in total isolation. 28 00:00:57,000 --> 00:00:59,430 Now, remember that the computer is not a mind reader. 29 00:00:59,430 --> 00:01:01,710 So it doesn't understand any nuance 30 00:01:01,710 --> 00:01:03,870 or intention behind your instruction. 31 00:01:03,870 --> 00:01:06,660 It's only going to do literally what that instruction says 32 00:01:06,660 --> 00:01:09,354 according to whatever rules of Python. 33 00:01:09,354 --> 00:01:12,180 Let's see that in action by tracing this program. 34 00:01:12,180 --> 00:01:14,130 The computer takes the first line of the program 35 00:01:14,130 --> 00:01:16,140 and loads that into working memory. 36 00:01:16,140 --> 00:01:17,580 Then the first thing it's gonna do 37 00:01:17,580 --> 00:01:20,490 is look for any expressions to evaluate. 38 00:01:20,490 --> 00:01:22,157 Remember that evaluating an expression 39 00:01:22,157 --> 00:01:25,350 just simplifies it down to a single value. 40 00:01:25,350 --> 00:01:26,880 Here we have the expression, 41 00:01:26,880 --> 00:01:30,270 the integer four plus the float, 20.55. 42 00:01:30,270 --> 00:01:34,440 And that evaluates to the float 24.55. 43 00:01:34,440 --> 00:01:36,540 Now, all our expressions have been simplified, 44 00:01:36,540 --> 00:01:38,790 so the computer's gonna peek outside the parentheses 45 00:01:38,790 --> 00:01:41,790 and ask, "Well, what did you want me to do with this value?" 46 00:01:41,790 --> 00:01:44,340 The instruction print tells it to take the value 47 00:01:44,340 --> 00:01:47,610 inside the parentheses and go display that in the console. 48 00:01:47,610 --> 00:01:51,840 So it goes over here and it prints 24.55. 49 00:01:51,840 --> 00:01:53,280 This instruction is complete, 50 00:01:53,280 --> 00:01:55,290 so the computer's ready to move to the next step, 51 00:01:55,290 --> 00:01:58,080 but first it wants to optimize its brain space. 52 00:01:58,080 --> 00:02:01,140 It doesn't really need to remember this instruction anymore, 53 00:02:01,140 --> 00:02:02,550 it doesn't need this information. 54 00:02:02,550 --> 00:02:06,150 So it just clears out its working memory and forgets, 55 00:02:06,150 --> 00:02:08,940 and that makes room for the next instruction. 56 00:02:08,940 --> 00:02:10,770 Now, the computer loads the second line 57 00:02:10,770 --> 00:02:12,210 into its working memory. 58 00:02:12,210 --> 00:02:15,390 And again, it looks for any expressions to evaluate. 59 00:02:15,390 --> 00:02:17,520 It sees the expression three plus two, 60 00:02:17,520 --> 00:02:21,300 and it simplifies that down to the integer five. 61 00:02:21,300 --> 00:02:24,150 Notice that there's no print instruction here. 62 00:02:24,150 --> 00:02:26,400 We didn't actually ask the computer 63 00:02:26,400 --> 00:02:28,200 to do anything with that value. 64 00:02:28,200 --> 00:02:29,407 So the computer's thinking, 65 00:02:29,407 --> 00:02:31,380 "Well, hey, I just did all this work. 66 00:02:31,380 --> 00:02:32,910 I figured out the answer's five, 67 00:02:32,910 --> 00:02:35,130 but I guess you don't want me to tell you. 68 00:02:35,130 --> 00:02:39,600 So it shrugs whatever, and it clears its working memory out, 69 00:02:39,600 --> 00:02:42,720 forgets that five, and just moves on. 70 00:02:42,720 --> 00:02:44,331 Third line, the computer loads, 71 00:02:44,331 --> 00:02:47,970 print the string learn plus the string space. 72 00:02:47,970 --> 00:02:49,950 Careful, this is not the empty string. 73 00:02:49,950 --> 00:02:51,330 There's one little space character 74 00:02:51,330 --> 00:02:53,400 in between these quotation marks. 75 00:02:53,400 --> 00:02:54,843 Plus the string more. 76 00:02:55,710 --> 00:02:58,040 But there are two operators in this expression. 77 00:02:58,040 --> 00:02:59,880 There's two plus signs. 78 00:02:59,880 --> 00:03:02,640 So the computer's actually going to evaluate this expression 79 00:03:02,640 --> 00:03:04,920 in two steps, reading left to right. 80 00:03:04,920 --> 00:03:08,790 First, it evaluates the expression, learn plus space. 81 00:03:08,790 --> 00:03:10,410 Now, when we add strings, 82 00:03:10,410 --> 00:03:12,090 remember that we are concatenating. 83 00:03:12,090 --> 00:03:13,440 We are smushing together. 84 00:03:13,440 --> 00:03:16,200 So we get the string learn space. 85 00:03:16,200 --> 00:03:18,180 Then we add the string more. 86 00:03:18,180 --> 00:03:21,153 We concatenate and we get learn space more. 87 00:03:21,990 --> 00:03:23,340 We're down to a single value, 88 00:03:23,340 --> 00:03:25,980 so the computer peaks outside the parentheses, 89 00:03:25,980 --> 00:03:27,870 sees that we wanted to print that value, 90 00:03:27,870 --> 00:03:31,590 and it prints learn space more in the console. 91 00:03:31,590 --> 00:03:33,390 Finally, it clears its working memory 92 00:03:33,390 --> 00:03:35,670 and it moves to the next line. 93 00:03:35,670 --> 00:03:38,850 What do you think the last two lines of this program do? 94 00:03:38,850 --> 00:03:41,050 Take a second and try and trace it yourself. 95 00:03:42,919 --> 00:03:45,660 Okay, this instruction has the expression, 96 00:03:45,660 --> 00:03:49,830 the string 81 plus the string 19.42. 97 00:03:49,830 --> 00:03:52,470 Now, these may look like integers and floats, 98 00:03:52,470 --> 00:03:54,990 but because there are quotation marks around them, 99 00:03:54,990 --> 00:03:57,330 the computer's going to treat them like strings. 100 00:03:57,330 --> 00:03:59,190 So when we evaluate this expression, 101 00:03:59,190 --> 00:04:00,840 we're concatenating strings 102 00:04:00,840 --> 00:04:05,700 and we get the string 8119.42. 103 00:04:05,700 --> 00:04:06,990 Nothing left to simplify here. 104 00:04:06,990 --> 00:04:09,360 So the computer pops outside the parentheses, 105 00:04:09,360 --> 00:04:14,070 sees the print, and then prints 8119.42 to the console. 106 00:04:14,070 --> 00:04:15,450 Then it clears out working memory 107 00:04:15,450 --> 00:04:17,130 and moves to the last line. 108 00:04:17,130 --> 00:04:19,350 The computer loads the last line of the program 109 00:04:19,350 --> 00:04:20,790 into working memory. 110 00:04:20,790 --> 00:04:23,370 Notice that this whole thing inside the parentheses here 111 00:04:23,370 --> 00:04:25,470 is surrounded by quotation marks. 112 00:04:25,470 --> 00:04:28,170 That means this is already a single value. 113 00:04:28,170 --> 00:04:30,450 It's the string, the two character, 114 00:04:30,450 --> 00:04:32,670 space character, plus character, space character, 115 00:04:32,670 --> 00:04:33,810 two character. 116 00:04:33,810 --> 00:04:37,200 It's not the expression the integer two plus two. 117 00:04:37,200 --> 00:04:39,390 Because we already have a single value, 118 00:04:39,390 --> 00:04:40,650 there's nothing to simplify here. 119 00:04:40,650 --> 00:04:43,140 So the computer pops out the parentheses, 120 00:04:43,140 --> 00:04:44,550 sees the instruction print, 121 00:04:44,550 --> 00:04:47,130 prints two space plus space two to the console, 122 00:04:47,130 --> 00:04:49,080 and clears working memory. 123 00:04:49,080 --> 00:04:50,760 Then it jumps to the next line of the program, 124 00:04:50,760 --> 00:04:52,410 and oh, there is no next line. 125 00:04:52,410 --> 00:04:54,540 We are at the end, we did it. 126 00:04:54,540 --> 00:04:56,970 So the computer terminates the program execution, 127 00:04:56,970 --> 00:05:00,930 it exits, and we have a final result here in the console. 128 00:05:00,930 --> 00:05:02,381 Wanna check my work? 129 00:05:02,381 --> 00:05:04,200 Copy this program into a code editor 130 00:05:04,200 --> 00:05:05,550 and run it for yourself. 131 00:05:05,550 --> 00:05:06,873 Is the result the same?