1 00:00:08,030 --> 00:00:12,210 Welcome back. In all our turtle examples, 2 00:00:12,210 --> 00:00:15,015 I glossed over the import statement. 3 00:00:15,015 --> 00:00:18,580 In this specialization, we're trying to minimize the amount of magic. 4 00:00:18,580 --> 00:00:21,375 We want you to be able to read code and reason about it. 5 00:00:21,375 --> 00:00:25,720 So this video provides some details about that import statement. 6 00:00:25,720 --> 00:00:30,080 There are lots of functions and methods available to you in Python, 7 00:00:30,080 --> 00:00:33,440 that other people have already written and we can just invoke them. 8 00:00:33,440 --> 00:00:35,255 We don't have to write them ourselves. 9 00:00:35,255 --> 00:00:38,660 Some of those functions are built into the core of Python, 10 00:00:38,660 --> 00:00:40,705 like the len function. 11 00:00:40,705 --> 00:00:43,350 You can just invoke it, it's always there. 12 00:00:43,350 --> 00:00:49,160 Lots of other functions are not built into the core of Python that are readily available. 13 00:00:49,160 --> 00:00:51,920 They're part of a standard library of modules. 14 00:00:51,920 --> 00:00:57,590 A module is a code file that defines some functions that can be used by other programs. 15 00:00:57,590 --> 00:01:01,640 Before you can invoke a function that's from a standard library module, 16 00:01:01,640 --> 00:01:03,920 you need to tell the Python interpreter to you make it 17 00:01:03,920 --> 00:01:07,425 available and that's what the import statement does. 18 00:01:07,425 --> 00:01:11,225 Turtle is one of those standard library modules, 19 00:01:11,225 --> 00:01:12,800 but we'll come back to that in a minute. 20 00:01:12,800 --> 00:01:15,035 Let's look at a simpler module first. 21 00:01:15,035 --> 00:01:18,710 There's a module called, "random" and you just 22 00:01:18,710 --> 00:01:22,565 have to import it in order to make all of its functions available. 23 00:01:22,565 --> 00:01:27,155 One of its functions is called, "randrange", 24 00:01:27,155 --> 00:01:30,190 you can see this on line six. 25 00:01:30,190 --> 00:01:35,900 So we've imported the random module and then on line six, 26 00:01:35,900 --> 00:01:38,045 we asked for randrange which says, 27 00:01:38,045 --> 00:01:41,820 "Give me an integer between one up to, 28 00:01:41,820 --> 00:01:42,990 but not including seven." 29 00:01:42,990 --> 00:01:46,135 So one, two, three, four, five, or six. 30 00:01:46,135 --> 00:01:49,010 There's another function called 31 00:01:49,010 --> 00:01:54,965 random and I'll talk in a minute about this weird thing of random.random. 32 00:01:54,965 --> 00:02:01,375 We have this random function that returns a floating point number between zero and one. 33 00:02:01,375 --> 00:02:08,070 In this case, we got 0.72 and then we get a die roll, a one, 34 00:02:08,070 --> 00:02:11,820 two, three, four, five, or six from the randrange function. 35 00:02:11,820 --> 00:02:21,000 If I do it again, I may get different values, get 0.039 and three. 36 00:02:21,000 --> 00:02:24,645 Run it a third time, I'll get yet again different values. 37 00:02:24,645 --> 00:02:29,675 There are two possible syntaxes for importing and referring the stuff from modules. 38 00:02:29,675 --> 00:02:34,775 The first makes the stuff available that doesn't give it any special alias. 39 00:02:34,775 --> 00:02:38,435 That's what this version that I'm showing in the current code does. 40 00:02:38,435 --> 00:02:42,469 On line one, we make everything from the random module available, 41 00:02:42,469 --> 00:02:44,720 but every time we want to refer to it, 42 00:02:44,720 --> 00:02:48,430 we have to say random dot. 43 00:02:50,540 --> 00:02:53,970 It doesn't give us any alias. 44 00:02:53,970 --> 00:02:58,040 So I say random dot and then the name of the function that's 45 00:02:58,040 --> 00:03:02,975 defined within the random module like randrange or random. 46 00:03:02,975 --> 00:03:07,390 Notice that this is another use of the dot notation. 47 00:03:07,390 --> 00:03:13,770 Before we've seen something like tess.forward, 48 00:03:15,020 --> 00:03:20,655 where tess was a turtle object and dot forward and we would say, 49 00:03:20,655 --> 00:03:22,965 how many pixels to go forward. 50 00:03:22,965 --> 00:03:28,390 Forward is a method that can be applied to tess, the turtle object. 51 00:03:28,390 --> 00:03:32,420 Here we're saying random.randrange. 52 00:03:33,500 --> 00:03:36,235 It's a little bit analogous. 53 00:03:36,235 --> 00:03:38,320 We're getting something from the random module 54 00:03:38,320 --> 00:03:40,615 and that something is the randrange function. 55 00:03:40,615 --> 00:03:44,244 But it's not exactly the same, random is a module, 56 00:03:44,244 --> 00:03:47,315 not the kind of Python object we've been seeing so far. 57 00:03:47,315 --> 00:03:49,840 The way to tell the difference between these two different uses 58 00:03:49,840 --> 00:03:52,375 of the dot notation is just by the context. 59 00:03:52,375 --> 00:03:57,655 If we said, import random as we did on line one then random dot something, 60 00:03:57,655 --> 00:04:00,725 we'll find this something object inside that module. 61 00:04:00,725 --> 00:04:06,100 Anyway, that explains the funny random.random on line three. 62 00:04:06,320 --> 00:04:09,255 Random is the name of the module, 63 00:04:09,255 --> 00:04:13,955 that gets us the first random and inside that module, 64 00:04:13,955 --> 00:04:18,295 there's a definition of a function also called random. 65 00:04:18,295 --> 00:04:22,265 That gives us the second random. 66 00:04:22,265 --> 00:04:26,975 Similarly, random.randrange, random is the name of the module, 67 00:04:26,975 --> 00:04:31,240 randrange is a function inside that module. 68 00:04:31,240 --> 00:04:40,840 One thing we can't say is randrange.random, 69 00:04:43,790 --> 00:04:47,260 that is going to fail. 70 00:04:50,480 --> 00:04:59,060 We do that. The second approach 71 00:04:59,060 --> 00:05:01,219 to importing stuff from other modules 72 00:05:01,219 --> 00:05:03,870 puts the functions directly into our local namespace. 73 00:05:03,870 --> 00:05:07,405 So we don't have to keep referring to the module they came from. 74 00:05:07,405 --> 00:05:10,030 Let me show you how that works. 75 00:05:10,040 --> 00:05:13,870 Instead of saying import random. 76 00:05:14,360 --> 00:05:21,610 We're going to say from random import randrange. 77 00:05:22,370 --> 00:05:27,145 We can also import the random function while we're at it. 78 00:05:27,145 --> 00:05:29,500 Now, when we refer to them, 79 00:05:29,500 --> 00:05:32,260 we no longer have to say what module they came from. 80 00:05:32,260 --> 00:05:35,290 Those names become available in 81 00:05:35,290 --> 00:05:41,030 our local namespace and this will work just like the other one. 82 00:05:43,880 --> 00:05:47,945 We got a new value there when we ran it again. 83 00:05:47,945 --> 00:05:50,645 Either these two different ways works, 84 00:05:50,645 --> 00:05:55,120 either the comment to that version that I have on line one import random 85 00:05:55,120 --> 00:06:00,910 or the other version on line two from random import randrange and random. 86 00:06:00,910 --> 00:06:07,765 But you have to refer to things a little differently depending on which one you did. 87 00:06:07,765 --> 00:06:10,225 So if we say from random, 88 00:06:10,225 --> 00:06:14,510 import randrange, then we'll just refer to random. 89 00:06:14,510 --> 00:06:16,585 If we did the other one, 90 00:06:16,585 --> 00:06:21,130 we will have to say random.random on line four. 91 00:06:21,320 --> 00:06:24,655 With that understanding of module importing, 92 00:06:24,655 --> 00:06:28,740 let's take a look back at how we imported the turtle module. 93 00:06:29,350 --> 00:06:32,300 Let's look at lines one through three here. 94 00:06:32,300 --> 00:06:36,955 On line one, we say import turtle, 95 00:06:36,955 --> 00:06:39,950 make everything from the turtle module available to us. 96 00:06:39,950 --> 00:06:41,990 Then on lines two and three, 97 00:06:41,990 --> 00:06:44,670 we're doing that turtle dot thing. 98 00:06:47,830 --> 00:06:54,575 On line two, we do turtle.Screen and we haven't 99 00:06:54,575 --> 00:06:57,260 explicitly talked about classes yet and you 100 00:06:57,260 --> 00:07:00,605 won't talk about that until much later in the specialization. 101 00:07:00,605 --> 00:07:04,070 But this is creating a new instance of a class, 102 00:07:04,070 --> 00:07:08,990 the class capital Screen is defined in the turtle module. 103 00:07:08,990 --> 00:07:12,545 Similarly, the class turtle is defined. 104 00:07:12,545 --> 00:07:14,405 So it's like we're invoking a function. 105 00:07:14,405 --> 00:07:18,540 But it's a function that creates a new turtle object. 106 00:07:18,540 --> 00:07:22,420 On line two, it is creating a new screen object. 107 00:07:22,450 --> 00:07:29,020 So this is just an example of the same syntax we were seeing with import random. 108 00:07:29,020 --> 00:07:32,630 Previously, we're importing the turtle module and 109 00:07:32,630 --> 00:07:36,199 then we're referring to something screen, 110 00:07:36,199 --> 00:07:40,235 that is defined inside of the turtle module. 111 00:07:40,235 --> 00:07:43,250 By the way we only have a few of 112 00:07:43,250 --> 00:07:47,195 the standard library modules that would normally be available in Python. 113 00:07:47,195 --> 00:07:50,975 Only a few of those are available in this Runestone textbook environment. 114 00:07:50,975 --> 00:07:52,370 We have the random module, 115 00:07:52,370 --> 00:07:54,665 the turtle module, and the math module, 116 00:07:54,665 --> 00:07:57,800 but there are a lot more that are part of 117 00:07:57,800 --> 00:08:01,760 the standard distribution if you just install Python on your own machine, 118 00:08:01,760 --> 00:08:05,410 and it can do lots of stuff for you so you don't have to write very much code yourself. 119 00:08:05,410 --> 00:08:07,900 You just have to find the right module. 120 00:08:07,900 --> 00:08:10,525 Then even beyond the standard library, 121 00:08:10,525 --> 00:08:13,070 there's lots of other modules that you can install 122 00:08:13,070 --> 00:08:16,420 if you've installed Python on your local machine. 123 00:08:16,420 --> 00:08:20,155 By the way, if you haven't seen XKCD before, 124 00:08:20,155 --> 00:08:23,870 there's lots of great geeky humor there and I highly recommend it. 125 00:08:23,870 --> 00:08:25,865 Here's one of my favorites. 126 00:08:25,865 --> 00:08:29,480 A friend is talking to his other friend who's up in the sky says, 127 00:08:29,480 --> 00:08:32,370 ''You're flying, how?'' his friend says, 128 00:08:32,370 --> 00:08:34,820 ''Python'', which I hope you're also experiencing. 129 00:08:34,820 --> 00:08:37,355 You're using Python and you're flying high. 130 00:08:37,355 --> 00:08:40,605 He says, ''I learned it last night. Everything is so simple. 131 00:08:40,605 --> 00:08:43,235 Hello World is just print hello world. 132 00:08:43,235 --> 00:08:46,200 Of course, in the newer version of Python that we have these days, 133 00:08:46,200 --> 00:08:50,075 we would have to have parentheses but still very simple.'' 134 00:08:50,075 --> 00:08:52,160 His friend says, ''I don't know about Python. 135 00:08:52,160 --> 00:08:57,050 I've heard it's got these weird things like dynamic typing and whitespace matters.'' 136 00:08:57,050 --> 00:08:58,650 His friend who's up in the sky says, 137 00:08:58,650 --> 00:09:02,655 ''Come join us. Programming is fun again. It's a whole new world up here." 138 00:09:02,655 --> 00:09:04,755 "But how are you flying?" 139 00:09:04,755 --> 00:09:09,680 "Well, I just typed import anti-gravity. That's it. 140 00:09:09,680 --> 00:09:12,859 Well, I also sampled everything in the medicine cabinet for comparison 141 00:09:12,859 --> 00:09:15,360 but I'm pretty sure it's the Python." 142 00:09:17,210 --> 00:09:22,490 So, Python has lots of great modules. Just import them. 143 00:09:22,490 --> 00:09:24,620 I don't know about the anti-gravity module though. 144 00:09:24,620 --> 00:09:28,800 Well, see you next time.