1 00:00:08,000 --> 00:00:10,740 Let's learn how to draw some pictures. 2 00:00:10,740 --> 00:00:14,130 Our metaphor here is that there's a virtual turtle. 3 00:00:14,130 --> 00:00:16,365 Starts out in the middle of the screen. 4 00:00:16,365 --> 00:00:20,040 In the turtle's tail, there's a pen. 5 00:00:20,040 --> 00:00:22,305 So, when the turtle moves around, 6 00:00:22,305 --> 00:00:26,205 it leaves a drawing on the screen. 7 00:00:26,205 --> 00:00:29,790 In this code, we've created a turtle named Alex, 8 00:00:29,790 --> 00:00:33,180 and we're telling it to move forward and turn left and go forward again. 9 00:00:33,180 --> 00:00:37,060 Let's see what its picture looks like. 10 00:00:38,540 --> 00:00:40,650 So, in this case, 11 00:00:40,650 --> 00:00:47,685 our virtual turtle is really represented by just an arrow. 12 00:00:47,685 --> 00:00:49,455 Doesn't even look like a turtle. 13 00:00:49,455 --> 00:00:51,570 But he started over here, 14 00:00:51,570 --> 00:00:55,950 moved forward by 150 pixels. 15 00:00:55,950 --> 00:00:58,700 That's what happened on this line of code, 16 00:00:58,700 --> 00:01:02,995 told him to start here and go forward by 150 pixels. 17 00:01:02,995 --> 00:01:08,825 He then turned left by 90 degrees. 18 00:01:08,825 --> 00:01:12,700 That's what the line five told him to do. 19 00:01:12,700 --> 00:01:18,370 Then, he moved forward by 75. 20 00:01:18,370 --> 00:01:22,850 Let's go through in a little more detail what each of these lines of code does. 21 00:01:22,850 --> 00:01:31,005 The first line just says to make our turtle commands be available. 22 00:01:31,005 --> 00:01:34,750 The second line says, make a screen. 23 00:01:35,360 --> 00:01:39,240 So, that's what line two does. 24 00:01:39,240 --> 00:01:41,420 Then, any turtle things that we have in 25 00:01:41,420 --> 00:01:44,440 the rest of the program will happen on that screen. 26 00:01:44,440 --> 00:01:48,405 Line three makes a turtle. 27 00:01:48,405 --> 00:01:51,975 That's what creates our little turtle guy, 28 00:01:51,975 --> 00:02:00,040 and it assigns that turtle object to the variable called Alex. 29 00:02:00,200 --> 00:02:06,995 On line four, we are invoking a method, the forward method. 30 00:02:06,995 --> 00:02:10,400 So, this is our hidden agenda in this lesson, 31 00:02:10,400 --> 00:02:13,670 is to teach you about objects in this dot notation. 32 00:02:13,670 --> 00:02:17,200 So, Alex is the name of the turtle. 33 00:02:17,200 --> 00:02:20,710 If you have an object and then a dot, 34 00:02:20,710 --> 00:02:25,700 it will look up attributes or methods of that object. 35 00:02:25,700 --> 00:02:30,395 So, alex.forward says, find the forward method, 36 00:02:30,395 --> 00:02:34,630 which is just like the functions that you've seen before. It takes an action. 37 00:02:34,630 --> 00:02:36,240 In this case, it says, 38 00:02:36,240 --> 00:02:40,930 take the forward action on Alex, 39 00:02:40,930 --> 00:02:44,280 and we pass in a value 150. 40 00:02:44,280 --> 00:02:48,160 That says how much Alex should move forward. 41 00:02:48,160 --> 00:02:53,440 So, we have object.method, 42 00:02:53,440 --> 00:02:54,630 and then, in parentheses, 43 00:02:54,630 --> 00:02:56,220 we pass a value. 44 00:02:56,220 --> 00:02:58,530 So, on line five, 45 00:02:58,530 --> 00:03:03,160 we're telling Alex to turn left by 90 degrees. 46 00:03:03,160 --> 00:03:06,570 Line six says, move forward by 75 degrees. 47 00:03:06,570 --> 00:03:08,985 Because Alex has a pen in his tail, 48 00:03:08,985 --> 00:03:16,330 we get to see the lines that show where he went. 49 00:03:17,240 --> 00:03:20,405 So, little vocabulary we've gotten here, 50 00:03:20,405 --> 00:03:22,955 we've gotten the idea of a instance. 51 00:03:22,955 --> 00:03:25,405 Alex is an instance of the turtle class, 52 00:03:25,405 --> 00:03:29,840 wn is an instance of the screen class. 53 00:03:29,840 --> 00:03:32,579 So, turtle is a class, 54 00:03:32,579 --> 00:03:34,845 Alex is an instance of that class. 55 00:03:34,845 --> 00:03:36,645 Screen is a class, 56 00:03:36,645 --> 00:03:40,275 wn is an instance of that class. 57 00:03:40,275 --> 00:03:42,730 We've gotten the idea of a method, 58 00:03:42,730 --> 00:03:45,680 like forward or left. 59 00:03:45,680 --> 00:03:49,570 Now, in addition to these things, 60 00:03:49,940 --> 00:03:54,330 we can also set and read attributes. 61 00:03:54,330 --> 00:03:55,530 In this turtle drawing, 62 00:03:55,530 --> 00:03:56,960 we don't really need to do it, 63 00:03:56,960 --> 00:04:02,485 but I want to show you how setting attributes works for object instances. 64 00:04:02,485 --> 00:04:04,595 So, Alex is an instance, 65 00:04:04,595 --> 00:04:07,845 and if I want to set any attribute, 66 00:04:07,845 --> 00:04:10,735 let's say, Alex's salary, 67 00:04:10,735 --> 00:04:15,055 because he should really be paid a lot for doing all of this drawing work for us, 68 00:04:15,055 --> 00:04:16,770 and we'll set his salary, 69 00:04:16,770 --> 00:04:20,775 let's say, to $50,000. 70 00:04:20,775 --> 00:04:27,350 If I do that, Alex will store in its salary attribute the value 50,000, 71 00:04:27,350 --> 00:04:31,345 and I can then refer to it just like we would other variables. 72 00:04:31,345 --> 00:04:34,154 So, Alex refers to that instance, 73 00:04:34,154 --> 00:04:36,760 and we can print out his salary. 74 00:04:36,760 --> 00:04:39,720 As before, when we print things, 75 00:04:39,720 --> 00:04:43,270 they're going to show up in our output window. 76 00:04:43,270 --> 00:04:47,530 So, this is going to redraw that whole thing, 77 00:04:47,810 --> 00:04:54,750 and at the end of it, it's going to print out the 50,000. 78 00:04:54,750 --> 00:04:57,015 That's because we printed it here. 79 00:04:57,015 --> 00:05:00,310 We're not going to do much with these attributes of 80 00:05:00,310 --> 00:05:06,535 instances for a while until much later in this course sequence, 81 00:05:06,535 --> 00:05:11,605 but we wanted to give you a hint of it before we went too far with Python. 82 00:05:11,605 --> 00:05:14,670 I want to make a little more interesting picture. 83 00:05:14,670 --> 00:05:19,230 Let's see if we can finish making a rectangle here. 84 00:05:19,230 --> 00:05:22,410 So, instead of doing this stuff with Alex's salary, 85 00:05:22,410 --> 00:05:28,285 let me get rid of that, and let me have Alex do a little bit more. 86 00:05:28,285 --> 00:05:33,200 I'm going to have him turn to the left by 90 degrees again, 87 00:05:33,200 --> 00:05:39,985 and what that should do is turn him that way so that when he goes forward, 88 00:05:39,985 --> 00:05:42,530 he'll be going in that direction. 89 00:05:46,770 --> 00:05:53,285 Now, how far should I make him go if I want it to be the same distance as this one was? 90 00:05:53,285 --> 00:05:56,625 Well, that one came from here, 91 00:05:56,625 --> 00:06:00,850 so I have to have him go forward by 150. 92 00:06:02,600 --> 00:06:05,550 Let's see what happens when we run that. 93 00:06:05,550 --> 00:06:10,580 It's always a good idea to run things when you write some code. 94 00:06:13,430 --> 00:06:16,270 Great. That seems to have worked. 95 00:06:16,270 --> 00:06:20,310 Let's have him turn and finish the rectangle. 96 00:06:25,490 --> 00:06:31,180 This time, I have to go just 75, the shorter length. 97 00:06:36,890 --> 00:06:41,080 Sure enough, we've got a rectangle there.