 Very good. Well, hello everyone and welcome back to Sailor Academy's introduction to Python This is unit five. We have done Four other reviews of units if you have not taken a course yet Feel free to go to sailor.org Python course come back. I'll make sure there are links in the description below if you're coming out late If you're live now, I'll put the links in the chat over there Um Yeah, that's a big thing Let me just introduce you to Dr. Eric sack who's gonna take you through what we're about to do awesome well, good morning everyone and Good morning What's it? Oh, I was just saying good morning if I'm being the class for you. Okay All right, so we're gonna talk about functions today and I want to I think what I'd like to do is maybe start not only to to kind of go over functions, but also see if we can't Maybe integrate some things from previous units here to sort of start to bring things together a little bit. So You know the idea of a function in general Hopefully I hope everybody's at least seen something in mathematics where if you they showed you this type of notation Where you have some function of x like x squared or something like that Basically, there's a rule that says if I give you any x you take the number and multiply it by itself and you get another Number sort of from the function. So you're sort of like dropping in x in and then you're getting in f of x out And that's that's really pretty much what functions are right? So here's your here's your f of x So so for any function you have an input you've got the rule and What you want to compute and then you get the output so functions basically have inputs They have outputs and they they perform some operation of some some kind the intent is to and And why do you do that really is is to in some sense? If you if you're you know writing code for a living what what usually happens is is like you find yourself Writing chunks of code and they end up being appearing several times in in a program And it just becomes more efficient at that point to sort of package all those Commands up all those instructions up into a little box that we call a function And then and then you can call that function and and you can give it a name and and you're allowed to Do anything that you want with it. So in Python or any really any any any Any programming language for the most part there are built-in functions That the that the language kind of gives you in some senses as part of the package I guess and and we've talked about and and also you know methods I will get to talk about methods also methods are basically functions defined within within classes And we're going to get to object oriented programming So you won't hear me talk too much about methods But but most of what I'm saying is going to hold true for methods as well Although the syntax will be a little bit different because even last week we saw you you you type something some Variable name dot and then you give the method name So that's the syntax for methods is is similar to functions, but not not exactly the same anyway They're a built-in do they're built-in functions And I think I think when we were doing lists I may have shown you like the sum function for instance So that's a function where you drop all you could what you don't have to drop you could drop You drop a list in for instance for example a numerical list and it'll sum the values for you So the input is the list the output is the sum And and and that's and we'll get to the syntax and all but those those are built-in those are important and then the beauty of Of programming is is that is that you are the creator right? You're the you're the one who is given the construct so you can write Any type of of function that you want to those are user defined functions you get to define those You're the you're the expert you're the one who comes along says, you know I need an x squared function or I need to do this out of the other thing and then And then you throw the syntax together that allows you to do that. So It's very powerful And let's see so pop over here again and do that And I should be so let me do that So we'll come back over and so the syntax for defining a function. Let's just take that example. We just did The the definition line or the depth line Is you you you specify the name of the function? Maybe I'll I'll call the function x squared for instance And so you have to give the function a name and then there's going to be an input And anything that's associated with the inputs and functions don't always have to have inputs There could be no inputs But the syntax is you for in python you have to have a left parentheses and a right parentheses And anything that you're sending to the function is placed within those parentheses comma separated and those are what the inputs to the function will be And then you have to end it with with a colon. So that's a syntax for a function call What the plan is is that I'm going to have a function So that won't be a very interesting function, but a function nonetheless just to get the point across We're going to take a number and square it. Okay, and then we're going to and then we're going to return it And we'll see how to do that. So the definition line is use de app. That's that's a reserved A python keyword the the word x squared doesn't exist in python if you try to use the word x squared You get a syntax error because python wouldn't recognize it until we define what it is So I'm calling this function x squared and then and then again as I said before the input is is x and Just for the heck of it. I'll write it like this. You this is not very efficient, but I'll I'll do it this way Is I'm going to take x and square it and then what I'll do is I will Return y so so if your function is going to have outputs There must be a return statement that is going to send the the answer or answers. I mean depending on how it gets defined You know back from the function so And the indentation is critical It's key the you have to have indentation here to to say what's so that should be no surprise Python works by indentation. We've already learned if statements while statements for statements They all kind of work in in in some sense the same way for their definitions you you define The the programming structure that you want to use you put a colon at the end And then once you hit return you indent everything that's associated with that with that particular programming structure So again, not the most exciting function in the world, but maybe what we'll do is we'll set y equal to two and then We'll set z equal to the output. We could do it this way again. It depends on You know what what your What your goal is But so here i'm setting y equal to two notice these this we're back in main now. So I might as well even put put a little Comment here, maybe I'll say something like this of main is here something like that because The function is separate and distinct from the main code. That's why you have the definition line That's why everything is is being defined outside outside of main in some sense main is just running this set of instructions and I could go ahead and and print You know z at this point after it runs and you would expect that You would expect that after you square the number that you'd see Why you'd see a four on the screen and But the the whole point of writing the function is that it can be called again and again and again maybe this time what I'll do is I'll do it with y equal to three and You know, or maybe I'll do it with y equal to four. So so the the idea is that You're planning on I'm using the function over and over again and and really what you do is you you've created your own Personal command basically you've created a new a new python instruction basically and And that's you know, that's that's powerful. That's that's what makes that's what that's what makes programming so So powerful is you get to define Your set of user defined commands or instructions if you want to think about it that way and you can use it over and over and over again With with different values So you can see what happens is if we run this function these instructions will execute the values will be returned from the function and You can you i'm just using the variable basic z as a sink for the output And so there's an assignment saying basically it's saying whatever comes out of that function drop it into the variable z assign it to the variable z And so that's so so every time this is being called we're seeing we're seeing different values Uh be That's interesting. Oh I'm not squaring it. I'm not squaring anything. I am actually raising it to the to a power So there we go. Just for the uh, just for the uh uninitiated. Um, what would you you want to explain what happens? For the for for the chat not for me, obviously, I 100 understand what actually but we'll do that in a second Let's go back. So x squared is just x times x Right x squared is x to the second power So i'm taking x and when I when I want to when I want to square something I want to take a number and multiply it by itself That's what the exponent does x to the power two is x times x and and so I was thinking I was thinking both ways at the same time basically, uh, because you could you could write it like this Which is x times x now if we run it, we'll see the right answer You know, but but you could also say you could also do this if you want to use powers x to the power two, right? So so you could say x squared like that as well That would be a more faithful reproduction of what the equation I gave on on the on the white board was Is that I'm trying to compute x squared and that's x times x So so I kind of mixed those two together in my brain and then I wrote x x to the x power is what I was computing So what I put in two is two to the second power when I was putting in three It was three to the third power which is 27 and then four to the fourth power is 264 so on and so on and so on and so forth. So um That's kind of how that works. So that these are the right answers now. So so I guess first of all for for Computing powers you you have a number of different ways to to raise something to a power in the math We talk about the math module in the course and so there's actually a p o w Function called called pow math dot pow and and you can raise a number to a power using that Or we can take x and raise it to the second power or we can take x and and multiply it by itself So there's a number of different ways to achieve the same thing, but this this is not That is not x squared. Okay. That's not x where that's x to the x power Okay, so uh, so that's that uh, but so here's the deal and you should really think about this because I find sometimes when when teaching at the beginning the beginner level Students The concept of a function is is odd in some sense and and it shouldn't be it's like it's like when you're creating your own commands And we're executing commands sequentially so we start at line 11 Then we execute line 12. Then we execute line 13. Then we execute line 14 So so this is what's called sequential processing instructions are being executed one after the other after the other Okay, and then you get this and then you get this and then this gets executed and then this gets executed And then this gets executed one after the other that's the whole You know idea behind sequential processing is you just If there are no branching instructions, if there are no conditional instruction instructions Like what we talked about like with if statements and so forth You're just going to go on and and execute remember with if statements There are sections of code that that that will not execute because you basically jump over them because a logical condition wasn't satisfied so that's going back to the unit on on conditional statements, but When it comes to just if there are no conditional statements, then then you're just going to execute Instruction after instruction after instruction and and so so x squared the instruction x squared the function is like A python instruction now and so it's just going to execute. What's interesting Is you know just a little bit about how computer systems work is basically when you when you write this this when you execute this instruction This is called calling the function you call it you make a function call And what happens sequentially is that is that the processor in some sense leaves this code Comes up here finds this function x squared and then begins executing all the stuff that's in the function And then when it sees the return statement, it knows to go right back here It started it remembers basically that it was at locate at at instruction 12 line 12 And it knows to go to line 13 after the function executes. And that's a pretty amazing thing It's it's um You you you have to have something in your computer called a called a stack. It's called a stack based architecture But but basically the computer has to remember Where it was in order to get back to the program So it the the central processing unit literally jumps from here It jumps up to this instruction begins executing everything inside until it sees this return statement And when it sees the return statement, it knows to go directly back To uh instruction on line 13 And you know, I mean the print is a function does the same thing the print is actually a You know fairly sophisticated function You're you're dropping something in here and you're telling it to print it out to the screen There's a lot that goes on in the background to do that And then and then the cpu knows to go to knows to return basically to line 14 And then you execute line 15 and jump up here to do all this and then you jump to the And so so sometimes in terms of program control Students get a little bit confused, you know about the order of execution, but it's it's completely sequential And you it's a good way to think about your program is once you've written the function This is this really is like an instruction that gets executed in a sequential Fashion so that when you're done with this one you get to the next you get to the next instruction And that that's just the mechanics of how it works So it should become very natural for you to be writing functions and ultimately methods as well Because what what's going to happen as I said is you're going to have some Operation that that you want to perform and you you want to have it as as one single instruction It makes the code a lot more efficient And it makes it neater it makes it more readable In the course we talk a little bit later on I think it's upwards towards unit 10 just to distinguish between what's called object oriented programming And procedural programming when you're writing procedures, you know, you're writing functions Basically, you're thinking of you're thinking of your program in terms of of small units small chunks That and we'll talk more about this like a so we get to unit 10 But you're thinking about small chunks and you create functions to execute those small chunks And then they they become reusable, you know, you can use them over and over and over again So so functions are are very very useful There's no way that you can become a an excellent programmer without without understanding how to write them And they're not very complicated. I mean look how fast I I chuck this down on paper. Okay, so this was, you know, very You know very easy to do So I'll take any questions about about functions. I think I just want to make sure everybody understands the The structure of a function call there are inputs There's code inside the function and then values are returned And so that's how that works So we don't currently have any questions, but as usual, I'll just vamp and see if we get some mostly positive feedback Our our UX designer Ryan Was in the chat saying functions are cool so so We have that as a comment for you Do with that what you will But uh, yeah, I'm just gonna talk for another couple seconds about mostly nothing just to wait and see again If you want, you know, you can leave a question in the chat if you need if you have a question And also leave one down below. We'll uh get back to it if it's relevant Maybe in another video, but as of right now that seems like enough delay time for people to have asked a question So I'll just kick it back Okay, so what I'm going to do is I just thought maybe I'd throw out an example I think that that would be interesting, you know from a from a programming standpoint Uh, but it requires a tiny tiny bit of understanding of of discrete math So I'm going to spend about five minutes. Uh, just teaching a tiny bit of discrete math for this example um, but basically there's this greek letter capital sigma and uh And sometimes you'll see stuff like this when you when you read about computer programs like the sum from You say this sigma means some you're gonna sum something you're gonna start adding a bunch of stuff And I want to bring this up because like I said, this will help tie together You know the basic for loop that stuff that we were doing maybe last week or the week before But if you if you're planning on summing some things and you've got a lot of terms like like 100 terms And you wanted to write them all out Just like the for loop makes the for loop makes your code efficient for summing a bunch of items The sigma sign does exactly the same thing on paper Just makes it much. So if I said the sum from i equal one, I'll take a small number But it make believe it was like a million, you know Then I'm going to make it three though because I'm not going to write down a million elements And maybe I'll say oh, I want to sum the elements from i starting at i equal zero to i equal three Well, this this translate unravelled unpacked unpackaged that sigma sign means this whatever value of i is You just sum it to the previous value. So this is very very much like a for loop In fact, I would say that if you ever if you ever see a sigma sign In something that you want to program up you could almost you could almost You know conclude like probably about 98.5 of the time that you're going to use something like a for loop to implement that Like an equation let's say But I want I want to you'll see where I'm going with this in a second I just want to get through this notation or if I if I said something like this the sum from i equals zero To and that's how to three of let's say i squared Right i squared then it's going to be zero squared By the way, this is everybody cares But I think you all can do that problem. Uh, so and one squared plus two squared Plus three squared like this, you know, again, you can see how this notation could be helpful If if I weren't just doing like the sum from zero to three You know if I if I were doing the sum from zero to a thousand this equation would get like ridiculously long And so so so the reason that we we pack it up in this Is that it just makes it more efficient, you know, you don't want to write everything out like that You know that just gets too too difficult, you know um, or If you have something like the sum here's another one like the sum equals zero To three of let's say two to the i-th power. Let's say so this is going to be like Uh, I'll make it minus cycles. I will make it converge um, let's say two to the minus zero plus two to the minus one plus Two to the minus two Plus two to the minus three. So what's happening is that as as the i varies From zero to three you're counting by ones. Basically. I'm telling you where to start counting I'm telling you where to stop counting and we're going to we're going to assume we're counting by ones Unless unless there's other notation in there to indicate otherwise So the sum basically tells us how to count and then there's some term inside the summation sign Inside the sigma sign that tells us the rule Okay, so here the rule anything to the zero's power is always one Anything to the minus one to the negative power is one over that. So that's like one over two to the first This is like one over two squared Just to remind you of negative powers one over two cubed, which is eight So so you get a sum that looks like that. So notice, I mean this This doesn't look anything like this But but you have to kind of get used to working through the rule To to generate the series of terms that you're going to sum together But when you see something like this, okay, you automatically think for loop I mean you would program this up or i'll say it in words because i'm going to assume everybody understands that material from the previous week it you do something like for i for i in range four Right because you get a count from zero to three it has to be in range four And then you would just compute a partial sum of two to the minus i And you would just sum those together like I showed you how to do the running sum with with the for loop And uh, I I suppose what I'll do is I'll write the code for that. Let's just do one really quick. I guess Rather than say it. Let's just do it. Um, and I'll write a little function and I'll call it Uh, I don't know what let's in fact. I'll leave that one there. Let's say we'll define. Um, you know to to to to to to the I And then I'll drop in I'll drop in a variable. I'll call it capital n And uh, I'll say two to the i or two to the n. I don't know You know Let's let's do some I'll call Two to the i or something like that. Um, and uh, what I'll do is I'll set a sum equal to zero and then we'll say for Uh, for i In range and you know, it's so funny There's just so many things. Um That Would go into this like um I'm gonna send it and I'm gonna sum from sort of at three So so if I do this, I won't actually add one to it And you'll see remember because the range can always counts to one less But you know, you know, what's funny is I love it students are so accepting and sometimes because uh The uh The in in range I haven't really even it's there because it's actually a set operation We haven't talked about sets. We're going to get to that next week But you're all accepting of of the fact that the range is going to give us some type of a set That uh, is going to have the numbers counting from zero to n basically, which is what we want So I could say like s plus equals Two To the ith power and if I do something like this this this really honestly goodness is the implementation of You know of that thing that little animal that I wrote down over on the board over here So if I if I do something like this if I'm sharing this again just to write it down again Yeah, to say for i equals zero Let's say Two to three what i'm doing is i'm going to send this variable n which is the number i'm going to send the number three And then we'll go ahead and add These partial terms together using a for loop. So this really will be you know one plus One over two to the first power plus one over two to the second power This is really just I don't mean to freak anybody out with a little bit of discrete math But but you know, this is the type of stuff that you're going to see when you're programming, you know So you might as well, you know, I think this is a good thing to know And I feel like it enables me to teach you functions using for loops, which we taught a couple of weeks ago so I think this is a good example and And and so we're going to take these terms these these terms and sum them together in a in a for loop So so this is what we're doing and this is this is actually the code That will implement this if I can call this correctly. So so what I'll do is I'll set I'll do it all in one line. I know before I was I was separating out Variables and so forth. But what I'll do is So the assumption is that we're going to sum two to the i For that maximum value from zero to three In this case, that's why i'm adding one to it because because I want to count from zero to n And remember the range function is going to go one less than the number that I specify So i'm specifying n plus one. So notice i'm sending the number three Which says count from zero to three So my loop is being set up to count from zero to three And uh, and then what i'm doing is i'm taking these partial sums these partial terms Remember how the for loop works is if you if we sum values, we're doing this every inning So in in the zero thinning, you know, we have two to the zero in in the uh, oops, and We're not doing two to the i by the way. We're doing We're going to do two dot dot Minus i just to be clear about that. Okay That's that's what we're doing. That's what we said we were going to do over here When we sum these values together. It's two to the minus i um So to be faithful to what that that we what we wrote there And then the loop now is going to take these partial values and every time we go through the next loop We're going to add the two to the i term for i equals zero And then the one for i equal one and then the next one to the partial sum For i equal two and then the next one for i equal three And if and then the last thing that we want to remember to do is after we have this value We'll have the final sum after the after the loop runs And we will return that value. Okay So so this is you know, the function is more like a contract, you know, you're saying That that these are the input values and then i'm going to use these input values in this way to compute something And then I promise to return this value s to you, you know, so so you have to expect that there's going to be an output Whatever that output is You know, i'm going to just drop that in i'm gonna i'm going to go ahead and print it So if everything works, okay, then what i've done is i've summed One plus a half which is one point five plus a quarter Which is a point two five and that's one point seven five and then you go ahead and get one to the one over eight And now you're at point one two five and you add that and you get One point eight Seven five so so this is the right answer It seems to be and you could you could check this on a calculator You can you know test it you could easily go into a calculator and add one plus a half Plus a fourth plus an eighth and demonstrate to yourself that this really gives you, you know, the right answer But the reason the main reason i'm choosing examples like this today is because uh, I want to immediately begin applying concepts from previous units in a way that that That sort of brings everything together After this unit, you know, we kind of go off on a a number of different I would say more sophisticated Aspects of python, but I think units one through five you'll find are very consistent with The way most programming language courses work. There's always a discussion about loops. There's always a discussion about conditionals There's always a discussion about functions And so I think this is a very sensible point like halfway through the course to to try and Tie everything together And what you need to have the ability to do is to be able to take these structures and plot them into functions And and be able to sort of play around with them and they feel very comfortable making Function calls to to compute stuff So this is just like I said, it's just i'm summing That's what the function is doing. I'm summing two to the ith power in this case. Okay Any questions about that? So that yeah, we we did have we have a question here. Um, uh front Henry gay About recursive functions Yeah, we um We don't talk a lot about recursion in the course And is it so I'll just I'm sorry. I'll just paraphrase. I don't want to cut you off. Sorry, but um, uh, so I guess Is it different in python than in other coding languages? um I think python affords one A certain measure of convenience that I think other languages might not when it comes to certain You know recursive structures Because you can you can write some really interesting return statements. I think with with python so for instance, um You know just as an example if you were going to make the attempt to make a recursive call uh You know for a For like a sir a linear search through an array Uh, and the the way that you sort of break the problem up Into its recursive pieces Um, it's very convenient to like integrate slicing A list slicing into a solution like that and you really You can't in certain in languages like maybe c or java you wouldn't have that Albeit it's possible, you know, and of course you can write code to do it I think with python it becomes much the code becomes a lot more compact um And and much more reflective because of lists and slicing it becomes much more reflective of uh of what the recursion is actually doing Uh when it comes to you know, sort of breaking a larger problem down into into a bunch of smaller problems but I think outside of the syntax, I mean recursion works exactly the same way because uh, you know recursion is recursion you the the the uh You have to you know, have a stack-based architecture. You have to um, you have to have a base case so that you don't make An infinite number of recursive calls And so In all that pretty much stays the same But I would it's interesting to compare like the implementation for instance of like For instance a binary search how you would do that in a in a java or a c versus uh versus a Python that I think python actually gives a much more elegant looking Solution simply because of you know, how lists and slicing work. It's it's it's much it's much nicer I think anyway But you know, it's it works the same way. So I mean we could I could do You know, we could try a simple Like a Fibonacci or something like that if you wanted to do Like it, you know an f of n is equal to an f of n minus one plus and f of n minus two This is a typical type of you know, introductory recursive type of an equation and like the um So this would work exactly the same way you'd end up with like a return just as an example You know, you'd end up with a return Statement that would be like if you called the function Fibonacci or Fibonacci just to make it sort of Fibonacci You know, it would work almost exactly the same way, you know as as other You know languages, but They're the one-to-one translation. I think You know, if you had your function Fibonacci that was adding together This is what a return statement would look like in in Python Uh, so you know, this this these types of A single variable recursions, you know, unlike Recursive equations or difference equations I don't think there's much difference between Python and many other languages But I think when you get to more You know search types of questions and sorting types of questions you would find that the The implementation is is a little bit more elegant. I think then Then what you would see for for typical languages that don't give that don't give the list Slicing so I hope I'm answering that question. Um, I hope so. Yeah, I think I saw a thank you I think in the chat. So I think I think I think we might have got it So I think we need to probably move on to the probably the next bit. Okay. Okay. Sorry No, no, no, it's okay I'm just you know in the back of my mind. I'm thinking whether or not I just it's just I I'm going to stay focused on this. I I was almost tempted to like quickly write a Recursive program, but I'm trying to I'm trying to stick to what's in the course like it's an introductory programming course and and My recollect we don't we didn't go into recursion for that introductory Course, but you know, there's a place for it in introductory courses and You know, it just for some reason that that didn't we had enough I think enough material with the 10 units to to not do that and even as that question comes up now, I almost I almost I'm thinking, you know, that that might be an interesting addition to to this unit But I want to stay focused on on what's in the course, you know Because I just I was almost tempted to write it was like a recursive program and then but we don't go into it in the course So it's like Maybe we'll come back to it. You know All right Anyway, so um, so let's continue with this the summation business because I wanted to I wanted to show you what it's something that I thought was kind of interesting Would be interesting if we understand the summation notation that do not do not do not do not do not Freak out on me here. Okay. I'm gonna I'm gonna I'm gonna write this out Okay, I'm gonna write this out as the summation. Let's say from i equals zero I'll choose three again because it's finite math. This is just discrete math It's just basically addition Subtraction and maybe raising things to a power. I got to show you one little function and speaking of recursion by the way I'm going to show you The factorial function and and I think we this this we do talk about in the course because I think I give a loop example That uses the factorial operation, but n factorial is just start at the value n And then start multiplying numbers all the way down to to one So three factorial is equal to three times two times one and four factorial is equal to and you could You know, I think I actually think I give A question. There's a quiz question I give on factorials And but again, it's done iteratively. It's not done recursively because you could do it recursively as well Uh, but anyway, um So so that's all if I if I say something factorial you just basically take the product of all the numbers up to that number And uh, again, you could you could easily I'm going to assume that everybody could write a loop to do this Because because this would be something that you would you would have learned in the loop section Of the course when we talked about program control The only thing you got to remember is so one factorial is equal to one You know two factorial is equal to two times one, which is two And uh, the only thing you got to remember is that zero factorial is defined As one so the factorial function is defined over over integers positive. I'm sorry, non negative integers Um, and it's it's generalizable. I'm not going to go into there's something called a gamma function Which makes which sort of takes this discrete version and makes it continuous And there's there's something from that can be derived to to compute something called sterling's approximation Because this this n factorial really grows fast. It blows up like like really fast and so these numbers get large really quick Um, and so there comes a point where it doesn't become sensible to do it within to drop Operations anymore. You want to use floating point operations that approximate what that big number will be And that comes out of something called sterling's approximation But the point is is that just take my word for it. This is a good. This is a good definition So zero factorial is defined as one Uh, and so so one factor is one two factorial is two three factorial is six Four factorial is 24 five factorial is five times three times four 120 so on and so forth and So, uh, the reason I'm showing this is because I'm going to show you a formula And what we're going to try and do is we're going to try to build the function the time we have left Together the function is this I'm going to take minus one I'm going to raise it to the i-th power And then what I'm going to do I'm going to this is actually going I'm going to call this a function I'm going to call it f of x which means I can drop x's into this and it's going to give I can drop a whole bunch of x's any x I want to and I'm going to get an answer out And I'm going to raise x to the two i-th power And divide that by i factorial And so if the first thing that we want to do Is excuse me is evaluate each term It looks this looks really nasty if you haven't seen stuff like this before but I don't need to be afraid of stuff like this It's not hard It's just discrete math where you're summing a bunch of simple terms together Depending on what the value of i is in this case. We're actually dropping an x in as if this were a function So if I if I do the if I do the zero terms set i equal to zero and just anywhere you see an i You plug in i equals zero. So that's i to the two times zero and that's over Zero factorial. Okay, and then we'll take minus one Because we're we're we're programmers now. We shouldn't you shouldn't shy away from stuff like this It's not it's not hard to do and and honestly if you do program a lot you're going to uh, you're going to see this stuff Quite a bit, you know as you get more sophisticated Plus so why not why don't we do a little computer science together just a tiny bit? Okay, plus this square when i is two it's x to the two times two divided by two factorial and then if if i is equal to three Then that's the that's the i equal three term It's going to be minus one wherever you see an i now for this term you put a three And then it's going to be two times i which is three and it's going to be over i factorial Which is three okay, and I'm going to tell you something right now. There's something called a taylor series and and if you're like if you're press a function on your on your Calculator, maybe we're taking like trigonometry in high school and you had to compute like a cosine function And and i'm sure nobody ever asked the question I wonder how they compute that because the cosine is just a theoretical object, you know, it's just the if you have a triangle It's just the ratio of you know this distance If you haven't had an angle theta the greek letter theta is what used to use for angles go back to high school trigonometry You know if you take x and you have a radius r it's it's just x divided by r. That's what the cosine is and and so You have to have what's called the numerical method for computing this you can't The and also it's it's a transcendental function. It's like it doesn't In principle, you know a lot of values that you compute for the for the cosine function Theoretically the numbers go on forever. They never end, but the computers are discreet, right? So you can only process an approximation to that to that number Well, i'm going to tell you that this function right here. This f of x is approximately equal to the cosine of x And i'll evaluate this in a second, but you can see this term anything to the zero power is always one so So x to the zero zero this whole term Minus one to the zero is one x to the zero is one zero. This is one So that this nasty looking thing where i'm taking this number and raising it all to all these powers It's just one minus in the next term minus one to the first power is negative one So you'll actually get negative one times x squared Over two factorial And then you'll get the next term is minus one squared minus one times minus one is positive one. This is x to the x to the fourth I'm sorry. This should be two times two times this Two times that and two times that sorry about that So this is going to be over two factorial And then what's going to happen is is the next term is going to be x squared over two factorial and Then here you'll get negative for the next one This is the x squared term. Now you have an x to the fourth term plus x to the fourth Over four factorial and then you'll get minus for this term minus one x to the Sixth over six factorial. If you work this out if you work this this term out, okay? What's going to happen is you're going to You see daisy put that in there like that Um, what's going to happen is this is what you'll end up with you're going to end up with f of x Is equal to and and I encourage you to do this. I really do this is and it's if you you can look it up It's called Taylor series. I'll just do three terms And it's a Taylor series for the cosine function. I'll write this again x to the two i So again, this could look like a complicated animal And I'll put this in parentheses So the point is though when you get done with it looks like this one minus x squared on two So it's called an alternating series with even powers it alternates plus minus plus minus plus minus Okay over four factorial Plus i'm sorry minus x to the six over six factorial And and I and like I said, I encourage you to to uh, you know to look this up Because my claim is that if you take an infinite number of terms That this will be equal if you go dot dot dot went on forever. This would be equal to the cosine of x Okay So what i'm going to do now in the last few minutes that we have is we're going to write a program We're going to write a function to compute this and then i'm going to convince you that it's actually equal to this And i'm going to show you how i'm going to do that. Okay What we'll do is we'll define a function and I'll call it cos A prox and I'll send in a value x. Sorry. I'll send in a value x And then I'll send in the number of terms So I want to compute in my sum on the whiteboard. I used three terms from zero to three But now what i'm going to do is i'm going to i'm going to create a sum Set s equals to zero partial sum. We'll have a for loop and again, it's the same game. We're going to have i in range in range Let's say n plus one. Okay, we'll take it out to you know that value again So nothing changes in this code right here It's exactly like what we did for the two to the i and then we'll have like s plus equals And we're going to have this term. Okay now in order to make this work There's a point where we introduce the math module and I think it's somewhere around The it's right about now. It's actually it's right about now That you've seen the import command where we import a module where we can do some where we could do some math And then I introduce, uh, you know raising numbers to powers trigonometric functions and so forth and uh and factorials so um So in this case we can write something like this. We could say, um maybe like this minus one to the Ith power Okay, that's the first term the second term now we multiply by x star star Let's say two times i like that Okay, so that's the second term in the summation that we come back over here I'm trying to implement This and so you have to get the first term minus one to the i'th power And then you have to get the x to the two i'th power and then you have we have to divide that by The factorial of two i okay, so we need three terms in that summation. Okay in that part in that partial sum Uh, and so if you come back over here, uh, I've got the first term minus one to the i'th power I've got x raised to the two i and then I'm going to divide this by watch this in slow motion now math Dot so this is a method that I'm calling within the math module. It's the factorial method And I'm gonna I'm gonna put two i in here as well Okay, and then what I'm gonna do is I'm going to Return this let's say okay um Get this guy out of here return return s Okay, and so what I'm going to do now is I'm going to call cosine approximation and oopsie daisy That's a That's replete that wasn't Um, and I'm going to call these functions I'm going to call the function with these values and I'm going to set I'll set x equal to You know some special angle will set it equal to zero and the cosine of zero degrees is always is always going to be one So that's an easy check and maybe we'll take like Four terms we'll let n equal four in this or we used n equal three so we'll use n equal three and I'm going to call this function with these values and You'll see over here the answer that gets printed out is one So at least for x equals zero the cosine of zero degrees. We're getting an answer of one so um the the But I think the most important question is you know if I if I use the different x like maybe if I use Like your special angles if you remember 45 degrees in radians again go back to basic trigonometry Is is pi divided by four So math dot pi there's actually a pi in the math module divided by pi divided by four is uh Is 45 degrees So if we go ahead and run this then we get something that looks like 0.707 That's one over the square root of two That's that if you go back like I said in review You'll remember that that the cosine of 40 there were special angles that they wanted you to remember and one of them was the cosine of 45 degrees And the cosine of 45 degrees is one over the square root of two which is 0.707 So it looks like this is approximating Potentially the cosine of an angle now watch this though This is the most important thing is that if we really really wanted to know if we were approximating it You'd want to you'd want to compare the um, so there's an abs function. It's built in in python Which is the absolute value the cosine of x Minus watch this in slow motion now the the cosine of x Minus the r approximation we want to see if we have a good approximation going on okay So what we're going to do is we're going to compute the difference between oopsie daisy math Dot cosine okay So we're applying the math module to compute the cosine of x and then we want to say we're going to say compare this value The value that you would get from your calculator Compare it to this approximation this what's called a series approximation or a tailor series approximation to the cosine of x And we drop this x and and we're going to compare them and we're going to see what happens So what did I do wrong here unexpected end of file? Oh, oh, did I do something down here? No, that's good. Oh, uh, that is the difference between that and then sorry there we go I could have told you that yeah You know what it's it's it's that um Replet is a little frustrating because it like it tries to finish lines for you And sometimes it'll put in stuff there that you never meant to do and it's what anyway But watch this now. So now the difference the difference between The cosine function that the math module computes and the one that we just constructed through this series approximation We're good to about six decimal places. They're actually very very close If we take more terms if I go ahead and take more terms like let's let's go ahead and take 10 terms now So that summation would go out over 10 partial sums partial terms 10 terms Which is a which would give us a better approximation. Let's go ahead and see what happens here Now we get a better approximation out to 16 decimal places So so it looks like this function that we've written actually approximates the cosine function And again, I don't need to go into all the details of where that equation comes from If you ever taken a calculus course, this usually gets brought up in like a calc one or a calc two type of uh, when you talk about what are called infinite series But that's you know, we don't have to go down that path because we're cutting our series off because we do discrete math computers Only compute a discrete number of terms So the reason I'm showing this application because I think it's a really good aspect of numerical methods Of of of how a computer computes Functions using these these finite series. Basically, that's how it does it May not necessarily use this one that there are more efficient implementations, but Of the let's say for trigonometric functions or cosine functions But but the general idea Is that is that you've got a host of functions either on your calculator or within the math module And the question is how does how does a computer compute those things? They pretty much use these series kinds of solutions to Compute to approximate pi to to do anything really um, and so this is one example of approximating the cosine function and uh and so Then you can test Whether or not it's a good approximation or a bad approximation. How do you do that? Well, you you use python's cosine function You compare it to your cosine function Compute the absolute value of the difference take the difference between those two answers Take their absolute value and then and then see if that's getting smaller as you take more and more terms So hopefully these two examples are tying together stuff that we did with loops and for loops and and uh Computing partial sums and that summing function we were talking about and and sort of integrating those now Into callable functions that you could use over and over and over again Um, I think I think if you walk out of today's lesson, you know understanding all of this you'll be well well primed for the uh the second five units But this is really again, there's there's a difference because you hear me say this over and over again There's a difference between memorizing syntax and applying it to write actual programs that solve problems In this case, you know, we we're applying syntax and so I'm talking to you as if you're experts with for loops now Uh and experts, you know on on various modules that we've introduced within the course Uh, and now we're trying to apply them to in this case make a certain kind of a computation. So You know, I hope that that ties, you know, this aspect ties together really the first five units Of of what's been done in the course So that we can kind of take a a breather and then go. Oh, okay. Well, I put all this together into this example Now let's move on to the next unit to do even more interesting things So that's just an example. I thought of I thought maybe it might be helpful to see, you know, this stuff integrated with previous material That's about all I wanted to say for today Oh, well, that's great. I just uh I want to Before I reiterate to everyone that, you know, this is part of the python course is unit five. You should definitely go back Uh, we have other videos that are recaps of the unit, but you should go to sailor.org cs105 and start learning python Replet is the program that you are using to To show everyone how to do it Um But while we wait to see if there's some more questions I just want to ask you because you seem great. So I was thinking while you were talking about I can do 3.14 159 Of pie how much pie do you know while we wait? Oh, no, you know, I never I never did that exercise I never really memorized a lot of those digits. Um I suppose I should, you know, but I I just never did, you know It was never my thing for some reason some people like, you know, you get to oh, I can I know All right, whatever. Okay. Well, it's just one of those things where like You're just like, oh, everyone knows the square root of whatever's whatever and I'm like, oh, you must know what pie So do I know more pie integers than you? You know more pie digits than me. You do. Uh, great All right. Um, so everyone we'll be back here next week. Uh, march 10th 938 m eastern time Again, uh, check the comments below. I'll put it in the chat again This is for our python course and definitely if you haven't caught up with us, um catch up with us We've we've done Four other of these videos that are all related to the course on sailor.org should catch up Leave a comment on an old video. I'll look back and see if we can answer that question Maybe in a newer video and as always, uh, Dr. Eric Sack, you're the best. You teach it like nobody else. So thank you everyone and uh, we'll see you next week