1
00:00:00,870 --> 00:00:04,710
Now before you start getting really committed to print and start making

2
00:00:04,710 --> 00:00:07,050
friendship bracelets for this print statements,

3
00:00:07,410 --> 00:00:10,020
there's another tool that I really want to talk about

4
00:00:10,230 --> 00:00:14,610
and it's a tool that's bigger, it's fancier, it's more complex.

5
00:00:14,610 --> 00:00:15,840
So what is it?

6
00:00:15,960 --> 00:00:19,770
It's a debugger and we've already seen debuggers a little bit.

7
00:00:20,580 --> 00:00:23,070
So I've already shown you pythontutor.com

8
00:00:23,430 --> 00:00:28,080
and you've also seen us use the debugger in the Thonny editor right here.

9
00:00:28,890 --> 00:00:32,610
Now, in this example, I'm going to stick with the web version of Python Tutor

10
00:00:32,850 --> 00:00:35,280
just so that you don't have to install anything extra.

11
00:00:35,640 --> 00:00:39,450
But if we go ahead and comment out the previous block and uncomment

12
00:00:39,480 --> 00:00:44,310
the last one, you can see that we have yet another problem to solve

13
00:00:44,490 --> 00:00:48,360
and yet another piece of code, that's not doing what it's supposed to do.

14
00:00:49,020 --> 00:00:49,830
So in this case,

15
00:00:49,830 --> 00:00:54,830
we've got a function that's supposed to mutate a list so that it takes a list as

16
00:00:55,320 --> 00:00:56,153
an input

17
00:00:56,400 --> 00:01:00,600
and then it's supposed to multiply each of the items in the list

18
00:01:00,840 --> 00:01:05,310
so the final output should be something like 2, 4,

19
00:01:05,550 --> 00:01:08,310
6, 10, and so on and so forth.

20
00:01:08,310 --> 00:01:12,240
Basically taking each of these numbers and multiplying them by 2,

21
00:01:12,420 --> 00:01:16,620
which happens on this line of code. But unfortunately when we run it,

22
00:01:16,830 --> 00:01:21,300
we just get a list with a single value, 26. So what's going on?

23
00:01:21,750 --> 00:01:23,460
Let's try and debug it.

24
00:01:23,550 --> 00:01:28,550
So I want you to copy these lines of code and then paste it into Python Tutor

25
00:01:29,430 --> 00:01:32,310
and then go ahead and click Visualize Execution.

26
00:01:32,940 --> 00:01:37,080
Now that it's inside the debugger, we can do a lot of things with it.

27
00:01:37,230 --> 00:01:42,030
We can step through the code to see it line by line. So for example,

28
00:01:42,030 --> 00:01:47,030
the first thing that happens is we create a function called mutate and this

29
00:01:47,430 --> 00:01:51,150
mutate gets past a input called a_list.

30
00:01:51,630 --> 00:01:54,630
So now inside this function, mutate,

31
00:01:54,810 --> 00:01:59,550
we've got a variable called a_list and it's this list with these values.

32
00:02:00,240 --> 00:02:04,740
Now, as we keep going, we can see we create a b_list, which is an empty list

33
00:02:04,920 --> 00:02:09,690
and then we use the for-loop to loop through each of the items in the list.

34
00:02:10,020 --> 00:02:13,980
So the first time this item is equal to the first item in the list,

35
00:02:14,010 --> 00:02:14,843
which is 1,

36
00:02:15,300 --> 00:02:19,470
and then we multiply it by 2 to create this new_item variable,

37
00:02:19,620 --> 00:02:20,820
which is now equal to 2.

38
00:02:21,510 --> 00:02:26,510
And now the loop goes back to the beginning and we keep doing this with each of

39
00:02:27,180 --> 00:02:31,830
the numbers in the list. We multiply 2 to each of the numbers.

40
00:02:32,520 --> 00:02:37,470
Now, once we're done with that loop, the next line that happens is this one.

41
00:02:38,100 --> 00:02:42,870
This b_list gets appended with the new item and finally,

42
00:02:43,140 --> 00:02:47,520
we print out the b_list. Now this is where the problem occurs.

43
00:02:47,610 --> 00:02:52,470
Notice how b_list is just a list with one value.

44
00:02:52,950 --> 00:02:57,300
And that is in fact what gets printed. So with this debugger,

45
00:02:57,330 --> 00:03:01,000
we've managed to see line line what the computer is doing,

46
00:03:01,390 --> 00:03:03,340
which is really helpful in itself.

47
00:03:03,730 --> 00:03:07,990
But we can actually make this process even simpler. Inside

48
00:03:07,990 --> 00:03:11,410
most debuggers, you can put a breakpoint

49
00:03:11,470 --> 00:03:16,470
which tells the computer to stop what you're doing at this particular line.

50
00:03:17,110 --> 00:03:19,120
And then at that moment in time,

51
00:03:19,390 --> 00:03:24,390
I want to examine what all the variables and all the functions are doing.

52
00:03:25,060 --> 00:03:28,920
So in our line, the key problem happens right here.

53
00:03:29,220 --> 00:03:32,130
If I click on this line, you can see it becomes red

54
00:03:32,580 --> 00:03:36,450
and this is where it happens, this is the break point basically.

55
00:03:36,780 --> 00:03:41,780
So now I can scroll over to that point in time I can examine the values of all of

56
00:03:42,720 --> 00:03:46,230
my variables. a_list is still what I expect it to be,

57
00:03:46,350 --> 00:03:51,350
but b_list is completely empty. And this item happens to be on the last item

58
00:03:52,830 --> 00:03:56,790
number 13 and the new item is doing what it needs to do,

59
00:03:56,790 --> 00:03:58,350
multiplying it by 2,

60
00:03:58,860 --> 00:04:03,570
but none of them are being added to my list because if I click next,

61
00:04:03,840 --> 00:04:08,490
you can see this b_list is equal to just one value.

62
00:04:08,760 --> 00:04:13,050
And it happens to be the last value that new_item was equal to.

63
00:04:14,370 --> 00:04:16,079
So by using this debugger,

64
00:04:16,380 --> 00:04:19,890
you should now have a good idea of what the problem is.

65
00:04:20,160 --> 00:04:25,160
Pause the video and try to fix this code so that the mutated list with all of

66
00:04:26,400 --> 00:04:29,790
its six values gets printed. Pause the video now.

67
00:04:31,920 --> 00:04:32,250
All right.

68
00:04:32,250 --> 00:04:37,250
So the simple problem is that when we are adding the new item to this b_list,

69
00:04:38,160 --> 00:04:40,080
we're doing it outside the loop.

70
00:04:40,890 --> 00:04:44,820
It's when this loop has completed and at the very end

71
00:04:45,060 --> 00:04:46,500
do we actually call this line.

72
00:04:46,950 --> 00:04:51,950
So what we needed to do to fix this code is just to indent that line over.

73
00:04:52,290 --> 00:04:54,330
And now when we run the code,

74
00:04:55,140 --> 00:04:58,170
you can see that the proper output gets printed

75
00:04:58,770 --> 00:05:03,570
and if we change this code in there, the debugger, if we edited it right here,

76
00:05:04,230 --> 00:05:08,160
then when we visualize it, you can see if I put a breakpoint here

77
00:05:08,490 --> 00:05:12,120
that right line of code gets executed six times

78
00:05:12,570 --> 00:05:15,300
and if we go to that line of code and we hit next,

79
00:05:15,630 --> 00:05:20,630
you can see that the b_list is now getting new values added each time as we

80
00:05:21,660 --> 00:05:25,530
would expect it to. And when we examine all of our variables

81
00:05:25,620 --> 00:05:27,570
especially this b_list variable,

82
00:05:27,900 --> 00:05:32,250
it's exactly what we expect it to equal each step of the code.

83
00:05:33,120 --> 00:05:35,640
So a debugger can be really useful.

84
00:05:35,880 --> 00:05:40,230
It's almost like you've put a print statement on all the variables in your

85
00:05:40,230 --> 00:05:41,010
code,

86
00:05:41,010 --> 00:05:46,010
and you can isolate a particular time point to see what each of the values are.

87
00:05:46,980 --> 00:05:49,800
And as we become even more experienced,

88
00:05:50,010 --> 00:05:52,320
we're going to be using the debugger more and more.

