0 1 00:00:00,480 --> 00:00:08,430 In this lesson we're going to combine our knowledge of functions with a new topic - objects. 1 2 00:00:08,430 --> 00:00:13,680 Objects are the basis for every Python program that you will ever write. 2 3 00:00:13,680 --> 00:00:20,460 Not to oversimplify, but when programming in Python we do things with stuff. 3 4 00:00:20,460 --> 00:00:22,480 Please pardon my formality. 4 5 00:00:22,530 --> 00:00:22,990 By 5 6 00:00:23,000 --> 00:00:23,940 "do things" 6 7 00:00:24,060 --> 00:00:30,570 I mean calling functions - functions that add two numbers, functions that import data, functions that fit 7 8 00:00:30,570 --> 00:00:38,280 regressions or generate charts or train machine learning models. And by "stuff" I mean objects. 8 9 00:00:38,280 --> 00:00:41,730 This lesson will be all about the stuff part. 9 10 00:00:41,730 --> 00:00:44,430 So let's talk about objects. You see, 10 11 00:00:44,490 --> 00:00:47,730 pretty much everything in Python is an object. 11 12 00:00:47,730 --> 00:00:54,210 Remember those modules that we talked about in previous lessons - modules like pandas, math or our own 12 13 00:00:54,210 --> 00:00:55,740 life module? 13 14 00:00:55,740 --> 00:00:57,120 Those are objects. 14 15 00:00:57,510 --> 00:01:03,760 We also had data frames that we put inside our X and y variables. Data frames are objects too. 15 16 00:01:03,870 --> 00:01:11,850 And the plot inside our plt variable is also an object. More formally, all the data in Python 16 17 00:01:11,850 --> 00:01:17,400 take the form of objects and the goal of this lesson is to explain the code that we wrote in previous 17 18 00:01:17,400 --> 00:01:20,780 lessons in more detail. By the end of this lesson, 18 19 00:01:20,790 --> 00:01:24,220 I want you guys to have a deeper understanding of how 19 20 00:01:24,270 --> 00:01:32,160 pd.read_csv and plt.show and regr.intercept actually work. 20 21 00:01:32,160 --> 00:01:36,310 My goal is to make these lines of code make a lot more sense. 21 22 00:01:36,360 --> 00:01:40,360 Let's make a real life analogy to objects in Python. 22 23 00:01:40,360 --> 00:01:41,880 Now suppose you have a car. 23 24 00:01:41,990 --> 00:01:43,750 If you don't own a car, don't worry. 24 25 00:01:43,920 --> 00:01:46,700 Owning a car is not a prerequisite for this course. 25 26 00:01:46,730 --> 00:01:51,510 Actually, I don't own a car myself but I've watched enough Top Gear to get very good at talking about cars 26 27 00:01:51,510 --> 00:01:53,480 and pretending I own one. 27 28 00:01:53,520 --> 00:01:58,670 Anyhow, we're going to talk about your red car like it's a Python object. 28 29 00:01:58,680 --> 00:02:01,470 First off, your car needs to have a name. 29 30 00:02:01,650 --> 00:02:04,200 After all, every decent car has a name. 30 31 00:02:04,200 --> 00:02:09,630 David Hasselhoff's car in Knight Rider was called Kit and in Pixar's movie, the protagonist was called 31 32 00:02:09,660 --> 00:02:11,360 Lightning McQueen. 32 33 00:02:11,430 --> 00:02:14,810 So in honor of Pixar we're gonna name your red racecar 33 34 00:02:14,910 --> 00:02:22,140 "lightning_mcqueen" all lowercase and with an underscore to keep with Python naming conventions and also 34 35 00:02:22,200 --> 00:02:25,410 avoid expensive American lawsuits. 35 36 00:02:25,450 --> 00:02:31,980 Now if you have to describe this car to your friends, you might talk about your car's color, 36 37 00:02:31,990 --> 00:02:38,160 the number of seats it has, the power of the engine or how much petrol you have left in the tank. 37 38 00:02:38,290 --> 00:02:42,420 All these things are attributes of your car. 38 39 00:02:42,430 --> 00:02:48,850 Now there's something quite significant about this - namely these attributes capture information about 39 40 00:02:48,850 --> 00:02:54,510 the state of your car and your car's state can change. 40 41 00:02:54,510 --> 00:03:00,650 For example, as you're driving along, the amount of fuel in the tank will decrease. In the same way, 41 42 00:03:00,660 --> 00:03:05,150 if you've got a computer game the state of the game will also change, right? 42 43 00:03:05,160 --> 00:03:09,780 Most obviouslym the level that the player is on will change as the program runs. 43 44 00:03:09,780 --> 00:03:14,940 This is the kind of thing I'm alluding to when I'm talking about state. But coming back to these attributes, 44 45 00:03:15,120 --> 00:03:20,130 let's talk about these attributes in a bit more detail and look at the Python's syntax. 45 46 00:03:20,160 --> 00:03:29,300 The thing is much of Python programming boils down to this simple pattern "object.attribute". 46 47 00:03:29,380 --> 00:03:35,590 So if you were writing a piece of Python code, accessing the attributes of your car then you might write 47 48 00:03:35,590 --> 00:03:43,300 something like "lightning_mcqueen.color" or "lightning_mcqueen.engine_power" or "lightning_mcqueen 48 49 00:03:43,310 --> 00:03:45,280 .mileage". 49 50 00:03:45,280 --> 00:03:50,680 We've actually encountered this object.attribute pattern many times before. 50 51 00:03:50,680 --> 00:03:57,820 One of the best examples is when we pulled out the coefficient of our regression or when we accessed the intercept 51 52 00:03:57,910 --> 00:04:05,050 of the regression. In both of these cases, the linear regression inside our regr variable was the object - 52 53 00:04:05,640 --> 00:04:10,270 and this object had an attribute called "intercept_". 53 54 00:04:10,330 --> 00:04:13,610 Now don't worry about the underscore at the end of this name. 54 55 00:04:13,840 --> 00:04:15,730 It's just part of the name of the attribute. 55 56 00:04:15,790 --> 00:04:20,470 The underscore has no special meaning - they could have called his attributes something else but they 56 57 00:04:20,470 --> 00:04:23,580 tagged on the underscore at the end of the name. 57 58 00:04:23,590 --> 00:04:27,010 The point is that we also used the same "object 58 59 00:04:27,010 --> 00:04:33,730 .attribute" pattern when we were talking about our Python modules; with "hitchhikersGuide 59 60 00:04:33,880 --> 00:04:42,280 .theAnswer" and "math.pi". In all of these cases we were able to access information from inside our 60 61 00:04:42,280 --> 00:04:49,360 object using the dot notation and it didn't matter if that object was a linear regression or if it was 61 62 00:04:49,390 --> 00:04:51,210 a Python module. 62 63 00:04:51,310 --> 00:04:57,670 Now accessing information from objects through their attributes is very well and good but remember how 63 64 00:04:57,670 --> 00:05:02,700 we said that Python is all about doing things with stuff. 64 65 00:05:02,710 --> 00:05:09,130 This is where our functions come in. However, because we really like computer programming jargon, 65 66 00:05:09,130 --> 00:05:15,730 we're gonna use a different word to refer to functions in this context. Functions that are used with 66 67 00:05:15,730 --> 00:05:18,820 an object are called methods. 67 68 00:05:18,820 --> 00:05:23,890 Now oftentimes you'll hear the word function and method used interchangeably 68 69 00:05:24,100 --> 00:05:31,480 and trust me I'd be the last person to nitpick about this use of language but it's still a good idea 69 70 00:05:31,510 --> 00:05:37,570 to get familiar with all this vocabulary because you'll definitely come across these words online and 70 71 00:05:37,570 --> 00:05:43,160 also nobody enjoys getting corrected by strangers on the Internet in online forums. 71 72 00:05:43,270 --> 00:05:47,990 So use methods when you're talking about functions that belong to an object. 72 73 00:05:48,050 --> 00:05:54,400 Let's return to our car example. Before we talked about how our car has a state. 73 74 00:05:54,730 --> 00:06:00,340 It can be a new car with low mileage; it can be an old car with high mileage; the car's fuel tank can 74 75 00:06:00,340 --> 00:06:04,010 be full or the car can be almost out of petrol. 75 76 00:06:04,090 --> 00:06:09,200 The state of the car object is captured through the attributes of the car. 76 77 00:06:09,550 --> 00:06:16,830 An attribute is a piece of information about the car like the mileage or the amount of fuel. 77 78 00:06:16,900 --> 00:06:23,980 Now, unless you're playing Top Trumps, the most interesting thing about a car is not its stats but what 78 79 00:06:23,980 --> 00:06:26,140 the car does. 79 80 00:06:26,380 --> 00:06:30,460 And the coolest part of the car is definitely its behavior. 80 81 00:06:30,460 --> 00:06:38,950 Remember that we said programming is about doing things? Well, things get done by calling methods and 81 82 00:06:38,950 --> 00:06:43,450 methods are the name for functions that belong to an object. 82 83 00:06:43,450 --> 00:06:49,960 So, an example of a method on our car object would be something like drive(). 83 84 00:06:49,990 --> 00:06:56,450 Now the question is, how do we call one of these functions that's associated with our car? 84 85 00:06:56,470 --> 00:07:02,220 Well, the format is exactly the same as with the attributes - we use the dot notation. 85 86 00:07:02,290 --> 00:07:08,030 In other words, we have our object, then a dot and then name of the method. 86 87 00:07:08,050 --> 00:07:16,090 So for example, if your red race car can drive then we'd write "lightning_mcqueen.drive()", and if 87 88 00:07:16,090 --> 00:07:21,280 your car can do other things like talk, then we'd follow the same pattern. 88 89 00:07:21,310 --> 00:07:22,620 That's enough theory. 89 90 00:07:22,750 --> 00:07:26,820 Let's put this into practice and add a function to our 90 91 00:07:26,820 --> 00:07:34,750 life.py file. So open the life.py file from your project's folder. In here, 91 92 00:07:34,770 --> 00:07:43,270 we're going to create a function by using the keyword def and we're going to call this function 92 93 00:07:43,270 --> 00:07:44,310 "quote_marvin". 93 94 00:07:44,350 --> 00:07:51,500 A function is not going to take any arguments so we'll just put empty parentheses and then a 94 95 00:07:51,500 --> 00:07:54,990 colon and inside the function, we'll put a print statement. 95 96 00:07:55,250 --> 00:08:02,850 We're gonna print out a single sentence, so we'll have two single quotes and then we'll write. 96 97 00:08:02,990 --> 00:08:07,910 "Ive calculated your chance of survival, 97 98 00:08:11,130 --> 00:08:12,060 but I don't think 98 99 00:08:18,280 --> 00:08:19,710 you'll like it". 99 100 00:08:20,260 --> 00:08:24,160 Now check out what happens if we add an apostrophe before the t 100 101 00:08:24,160 --> 00:08:32,740 in "don't". Putting that single quote there terminates our string, so anything after the single quote is 101 102 00:08:32,740 --> 00:08:33,460 considered to be 102 103 00:08:33,490 --> 00:08:36,790 Python code and then we have another single quote here. 103 104 00:08:37,420 --> 00:08:39,270 So this is a problem, right? 104 105 00:08:39,280 --> 00:08:45,960 We've got a special character, that single quote, inside of our piece of text. 105 106 00:08:46,000 --> 00:08:52,230 The obvious problem is that in Python single quotes also go at the beginning and at the end of a string 106 107 00:08:52,240 --> 00:08:55,500 to tell Python that something is a piece of text. 107 108 00:08:56,410 --> 00:09:01,740 So, we need to tell Python that it should ignore the single quote in don't. 108 109 00:09:01,960 --> 00:09:09,450 In other words, we have to escape the single quote character. To escape a special character like this 109 110 00:09:09,490 --> 00:09:13,350 we have to add a backslash in front of it. 110 111 00:09:13,390 --> 00:09:16,060 This is what a backslash looks like. 111 112 00:09:16,060 --> 00:09:22,260 Notice how as soon as I put it there the syntax highlighting changes, this single quote is ignored 112 113 00:09:22,260 --> 00:09:28,460 and now this single quote at the end is what terminates this string. 113 114 00:09:28,470 --> 00:09:31,920 Now if you're looking at your keyboard you don't want to use this one here. 114 115 00:09:31,920 --> 00:09:33,590 This is the forward slash. 115 116 00:09:33,960 --> 00:09:36,600 And this does not escape the single quote. 116 117 00:09:36,600 --> 00:09:38,730 You want to use the backslash. 117 118 00:09:38,800 --> 00:09:41,260 I am going to add two more backslash us in single quotes. 118 119 00:09:41,340 --> 00:09:46,750 I might add one here, and I'm going to add one here. 119 120 00:09:46,760 --> 00:09:52,140 Now let's save the file and then return to the Python intro notebook that we've been working in. 120 121 00:09:52,220 --> 00:09:59,360 We go to File > Save. I'm going to switch tabs to the Python intro notebook, 121 122 00:09:59,540 --> 00:10:04,810 and this is where we've imported life as hitchhikersGuide. 122 123 00:10:04,980 --> 00:10:11,760 Now if you returning to this notebook and have not run the code in here for a while, you can either click 123 124 00:10:11,820 --> 00:10:19,860 inside this cell here and press Shift+Enter on your keyboard to rerun the cell or you can go to "Cell" 124 125 00:10:20,520 --> 00:10:24,370 and then "Run All" to run all the cells in the notebook. 125 126 00:10:24,690 --> 00:10:33,630 After you've done that go to the bottom of your notebook and there as a challenge see if you can call 126 127 00:10:33,750 --> 00:10:36,210 the quote_marvin method. 127 128 00:10:36,210 --> 00:10:41,100 If you call this method successfully, then you should see a quote from the Hitchhiker's Guide To The Galaxy 128 129 00:10:41,550 --> 00:10:44,850 appear below the cell after you run it. 129 130 00:10:44,850 --> 00:10:46,860 I'll give you a few seconds to pause the video. 130 131 00:10:50,210 --> 00:10:51,920 And here's the solution. 131 132 00:10:52,100 --> 00:10:58,060 You'd write "hitchhikersGuide.quote_ 132 133 00:10:58,310 --> 00:11:01,180 marvin()" and hit Shift+Enter. 133 134 00:11:01,760 --> 00:11:04,700 Remember, to call the quote_marvin method, 134 135 00:11:04,700 --> 00:11:07,280 we need to use our module object. 135 136 00:11:07,280 --> 00:11:10,810 In this case, our module has the name hitchhikersGuide. 136 137 00:11:11,060 --> 00:11:15,860 Put a dot after hitchhikersGuide and then write the method name. 137 138 00:11:15,870 --> 00:11:21,540 Now, suppose you just tried to run quote_marvin on its own, without the object. 138 139 00:11:21,540 --> 00:11:23,950 In that case you'd get an error. 139 140 00:11:24,120 --> 00:11:28,890 The error would read "name 'quote_marvin' is not defined". 140 141 00:11:28,890 --> 00:11:37,380 In other words, our Python notebook doesn't recognize a function called quote_marvin and this is because 141 142 00:11:37,380 --> 00:11:41,540 quote_marvin belongs to the life.py file. 142 143 00:11:41,610 --> 00:11:48,900 It belongs to the module, not our Python intro notebook; and this neatly illustrates the difference between 143 144 00:11:48,900 --> 00:11:52,320 a function and a method. To use quote_marvin, 144 145 00:11:52,320 --> 00:11:59,340 we need to have that object and use the dot notation. In contrast, for a function like print or for a 145 146 00:11:59,340 --> 00:12:01,410 function like type, 146 147 00:12:01,410 --> 00:12:08,520 we didn't have to have anything before that function name. Print and type are functions, while quote_marvin 147 148 00:12:09,120 --> 00:12:10,760 is a method. 148 149 00:12:11,130 --> 00:12:17,010 So let's revisit our old notebook with the linear regression again. Looking at this code, 149 150 00:12:17,100 --> 00:12:19,950 pd.read_csv 150 151 00:12:20,280 --> 00:12:27,020 we now see pd as the object and read_csv as the method. 151 152 00:12:27,300 --> 00:12:28,130 And below, 152 153 00:12:28,260 --> 00:12:33,970 we can now recognize plt as the object and scatter as the method. 153 154 00:12:34,120 --> 00:12:41,860 In fact, we're calling a whole host of methods on this plt object, including xlabel, ylabel, title 154 155 00:12:42,520 --> 00:12:44,140 and show. 155 156 00:12:44,140 --> 00:12:50,860 Now a question that we get a lot and something that can be a little bit confusing is the link between 156 157 00:12:50,860 --> 00:12:53,800 variables and objects. 157 158 00:12:53,800 --> 00:13:00,850 So let's talk about this a little bit and link how this lesson ties in with what we said about variables. 158 159 00:13:01,780 --> 00:13:04,630 Back at the top of our Python intro notebook, 159 160 00:13:04,630 --> 00:13:08,200 we had a variable called myAge. 160 161 00:13:08,590 --> 00:13:16,690 The question you might ask at this point is "Is myAge an object?" and the way to think about it is this - 161 162 00:13:17,590 --> 00:13:23,410 variables are containers. Variables are boxes that hold on to data. 162 163 00:13:23,410 --> 00:13:28,670 The thing inside the variable is the actual object. In this case, 163 164 00:13:28,710 --> 00:13:37,080 the object is the number 32. The object is the data and the variable is the name that is referencing 164 165 00:13:37,320 --> 00:13:42,200 the number 32. Looking at the linear regression notebook, 165 166 00:13:42,210 --> 00:13:49,230 we had a variable called capital X. X contained our movie budgets as a data frame. 166 167 00:13:49,620 --> 00:13:57,580 The data frame contained inside of X is the object. And X is just the name for the data frame. 167 168 00:13:57,600 --> 00:14:04,430 So in summary, a variable is a way to refer to an object at some point in time. 168 169 00:14:04,440 --> 00:14:12,260 Variables are containers for data, but a variable is not the data itself. And this is actually quite important, 169 170 00:14:12,290 --> 00:14:19,490 because it links back to our lesson on data types. You see, in Python the type actually lives with the 170 171 00:14:19,490 --> 00:14:23,330 object and not with the variable. 171 172 00:14:23,340 --> 00:14:24,390 Check this out. 172 173 00:14:24,750 --> 00:14:32,320 When I scroll to the bottom here and in this cell right here I'm going to write 173 174 00:14:32,800 --> 00:14:44,970 myAge = '200' and then I'm going to write type(myAge) and hit Shift+Enter. 174 175 00:14:45,160 --> 00:14:52,990 This now shows me that myAge is a string at this point in time of the program. At the top of the Python 175 176 00:14:52,990 --> 00:14:53,880 notebook, 176 177 00:14:53,920 --> 00:15:01,020 myAge was still an integer - an int, but at the bottom of this notebook myAge is now a string. 177 178 00:15:01,080 --> 00:15:08,160 In other words, I can change the type of myAge so let's change it again, I'm going to say "myAge = 178 179 00:15:08,580 --> 00:15:15,070 20.53", then I say type(myAge) and hit Shift+Enter. 179 180 00:15:15,120 --> 00:15:26,940 Now we see that myAge is a float so in our notebook myAge starts out as an integer equal to 32. 180 181 00:15:27,220 --> 00:15:34,900 Then it becomes a string and then it becomes a floating point number. Now this piece of code will actually 181 182 00:15:34,900 --> 00:15:43,060 really, really freak out a Java or C programmer, by the way. The Java and C guys watching this probably 182 183 00:15:43,060 --> 00:15:47,920 just had a cold shiver run down their spine like when they watched the girl come out of the well in 183 184 00:15:47,920 --> 00:15:50,470 the horror movie The Ring. 184 185 00:15:50,500 --> 00:15:58,000 The important point I'm trying to make here is that in Python that type lives with the object inside 185 186 00:15:58,030 --> 00:16:01,230 the variable, not the variable itself. 186 187 00:16:01,240 --> 00:16:08,620 So in this code we've simply changed what the object is inside of my age, inside of the variable; we've 187 188 00:16:08,620 --> 00:16:16,550 changed which object the variable is referencing. And there's actually another important link to our 188 189 00:16:16,550 --> 00:16:23,090 lesson on types - namely the fact that the behavior of the object will depend on its type. 189 190 00:16:24,260 --> 00:16:28,490 So our car can drive because it's of type car. 190 191 00:16:28,850 --> 00:16:33,370 And we could add one to myAge because it was a number. 191 192 00:16:33,410 --> 00:16:39,900 In other words the operations that you can do on an object depend on its type. 192 193 00:16:39,950 --> 00:16:48,920 So even your simple integer is an object in Python an integer has a value say like 199, and then some 193 194 00:16:48,950 --> 00:16:53,190 operations that we can do on it like multiplying or adding. 194 195 00:16:53,190 --> 00:17:00,320 And this is why programmers like to think of objects as pieces of computer memory with a value and a 195 196 00:17:00,320 --> 00:17:08,580 set of operations. In Python we actually care more about what an object does rather than what it is. 196 197 00:17:08,610 --> 00:17:15,750 Python programmers care more about an object's behavior and its methods than what its data type is. 197 198 00:17:15,750 --> 00:17:23,730 This is what makes Python's types dynamic and this is at the root of Python's flexibility 198 199 00:17:23,730 --> 00:17:28,230 as a programming language. On pythons Wikipedia page, 199 200 00:17:28,490 --> 00:17:35,750 this is what people mean when they talk about Python's dynamic type system. That said, I harp on and on about 200 201 00:17:35,750 --> 00:17:42,740 types because, in my experience, even though they give Python a lot of flexibility they're also a big 201 202 00:17:42,830 --> 00:17:43,700 stumbling block 202 203 00:17:43,700 --> 00:17:50,710 when you first starting out with Python. Now, as a challenge, let's put a couple of the past lessons together. 203 204 00:17:50,820 --> 00:17:55,020 You're back as the engineering lead at the calculator company in Silicon Valley. 204 205 00:17:55,110 --> 00:18:00,030 You've been laughing at your boss's terrible jokes and you've got a big bonus, but your company needs 205 206 00:18:00,030 --> 00:18:03,870 to ship its calculators to its first paying customers ASAP. 206 207 00:18:04,020 --> 00:18:10,530 So it's crunch time for you and your development team, and it's on you to implement the square root functionality 207 208 00:18:10,650 --> 00:18:12,410 of the calculator. 208 209 00:18:12,450 --> 00:18:13,990 Here's what you got to do. 209 210 00:18:14,040 --> 00:18:20,340 You got to open that life.py file and you've got to import the math module and then you're 210 211 00:18:20,340 --> 00:18:26,940 going to try to figure out how to use the python square root functionality from the math module. You're 211 212 00:18:26,940 --> 00:18:30,990 going to create your own function called square_root, 212 213 00:18:30,990 --> 00:18:36,960 and that function is going to take one input and return the square root of whatever that function has 213 214 00:18:36,960 --> 00:18:40,630 been given as an argument. After you're all done, 214 215 00:18:40,740 --> 00:18:47,040 you're going to save the life.py file and in your Jupyter notebook, you're gonna go to Restart and 215 216 00:18:47,250 --> 00:18:51,850 Run All. Finally in the bottom cell of your python intro notebook, 216 217 00:18:51,870 --> 00:18:58,470 you can calculate the square root of 63.14. I'll give you a few seconds to pause the 217 218 00:18:58,470 --> 00:19:03,760 video and have a go at this challenge. Ready? 218 219 00:19:03,820 --> 00:19:09,820 Here's the solution. At the top of our life.py file, we're gonna import math 219 220 00:19:13,010 --> 00:19:22,220 and then we're gonna create a function called def square_root and it's gonna take a single 220 221 00:19:22,220 --> 00:19:34,650 input, say x, semicolon and then in the body of the function we're going to return the result of the calculation. 221 222 00:19:35,760 --> 00:19:41,500 So the question is how do we make that calculation? The way we figure this out is that we're going to 222 223 00:19:41,500 --> 00:19:47,200 Google for the official Python documentation on the math module. 223 224 00:19:47,200 --> 00:19:57,610 So we're going to write Python math module documentation into Google and then we're gonna go to the 224 225 00:19:57,790 --> 00:20:00,700 Python 3, which is what we're using, 225 226 00:20:00,730 --> 00:20:10,200 documentation. The documentation will look something like, this and it's a pretty long page, but let's 226 227 00:20:10,200 --> 00:20:20,740 see if we can find square root and if we search for the words square on this page we can see that the 227 228 00:20:20,740 --> 00:20:25,570 guidance for using the square root in the math module looks like this. 228 229 00:20:25,570 --> 00:20:34,720 We'd have to use the math module then put a dot after it and then write sqrt and provide a single 229 230 00:20:35,020 --> 00:20:43,810 argument. Let's use this functionality back in our life.py module. On the same line where we have 230 231 00:20:43,810 --> 00:20:45,070 our return statement, 231 232 00:20:45,070 --> 00:20:54,640 we're going to write math.sqrt, and we're going to provide the same input, the same parameter 232 233 00:20:54,970 --> 00:20:57,460 that we have for our function. 233 234 00:20:57,460 --> 00:21:01,590 So the name of that parameter in our case is x. 234 235 00:21:01,780 --> 00:21:05,950 Now again the exact name of this parameter doesn't really matter. 235 236 00:21:05,950 --> 00:21:09,730 We could've called this thing something else. 236 237 00:21:09,730 --> 00:21:11,940 In this case we'd have to be consistent. 237 238 00:21:11,980 --> 00:21:16,390 We'd have to call this something else as well. 238 239 00:21:16,390 --> 00:21:20,890 That way we're using the same parameter. Back in our Jupyter notebook, 239 240 00:21:20,890 --> 00:21:27,940 we said we'd go to Kernel > Restart & Run All to make sure that this works no matter what it is that 240 241 00:21:27,940 --> 00:21:31,350 you've been doing prior to getting to this point in the video. 241 242 00:21:31,360 --> 00:21:42,310 So here in our last cell we can write hitchhikersGuide.square_root, and then provide 242 243 00:21:42,310 --> 00:21:46,450 the value 63.14. 243 244 00:21:46,450 --> 00:21:50,350 This was the last part of the challenge. Hitting Shift+Enter, 244 245 00:21:50,350 --> 00:21:52,400 I can see an attribute error. 245 246 00:21:52,780 --> 00:21:56,600 Life has no attribute called square root. 246 247 00:21:56,680 --> 00:21:58,980 What did we forget to do? 247 248 00:21:59,230 --> 00:22:02,910 If you're getting this as well, it's because you haven't saved your life.py 248 249 00:22:02,920 --> 00:22:10,240 file and you can actually tell that they are unsafe changes by the little star in the name 249 250 00:22:10,240 --> 00:22:11,440 of the tab. 250 251 00:22:11,650 --> 00:22:19,150 So go to file, hit Save, that little star will disappear and it'll say that your file was saved a few 251 252 00:22:19,150 --> 00:22:23,640 seconds ago. And back in our Python notebook, 252 253 00:22:23,860 --> 00:22:28,510 we're gonna have to go again to Kernel > Restart & Run All. 253 254 00:22:35,750 --> 00:22:40,700 Scrolling down now we can see that our cell has evaluated correctly. 254 255 00:22:40,940 --> 00:22:48,440 And the square root of 63.14 is about 7.95. 255 256 00:22:48,440 --> 00:22:49,640 So there you go. 256 257 00:22:49,640 --> 00:22:57,280 We've got the solution right below our cell, but since our method here is returning a value, let's save 257 258 00:22:57,290 --> 00:22:59,220 this value in a variable. 258 259 00:22:59,300 --> 00:23:08,810 So when I say "result = hitchhikersGuide.square_root". And then below I am just going to print 259 260 00:23:09,260 --> 00:23:16,430 the result and hit Shift+Enter. So I hope this lesson helped demystify the code that we've written previously 260 261 00:23:16,850 --> 00:23:20,400 and explain what's going on behind the scenes. 261 262 00:23:20,750 --> 00:23:28,520 Looking at this line of code, reading "result = hitchhikersGuide.square_root(63 262 263 00:23:28,520 --> 00:23:29,840 .14)" 263 264 00:23:29,930 --> 00:23:36,080 We can see that it's exactly the same pattern as with the Python code that we wrote when we're doing 264 265 00:23:36,080 --> 00:23:37,530 our linear regression. 265 266 00:23:37,730 --> 00:23:38,390 There we wrote 266 267 00:23:38,390 --> 00:23:43,110 data = pd.read_csv 267 268 00:23:43,370 --> 00:23:45,910 We've got an object called pd. 268 269 00:23:46,160 --> 00:23:49,160 It has a method called read_csv 269 270 00:23:49,370 --> 00:23:55,280 And that method is returning a value that we're storing in a variable called theta. 270 271 00:23:55,280 --> 00:23:56,030 Cool. 271 272 00:23:56,030 --> 00:23:58,790 So at this point you pretty much know Python. 272 273 00:23:58,790 --> 00:24:04,360 So now Ryan Sawyer's comic here can probably bring a smile to your face as well. 273 274 00:24:04,380 --> 00:24:10,120 Also, I bet you're itching to use your Python knowledge to run another regression. 274 275 00:24:10,130 --> 00:24:16,300 I'm gonna head off and grab a bite to eat now, but I do look forward to seeing you in the next lesson. 275 276 00:24:16,310 --> 00:24:16,850 Take care.