Python 3, an illustrated tour Transcripts
Chapter: Functions
Lecture: Walk-through: Keyword-only Arguments

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video, we're going to look at keyword test.
0:02 Let's open that up, open it in your editor.
0:05 Let's read the first assignment,
0:07 it says one formula for force is mass times acceleration
0:10 create a function force that takes to keyword only arguments mass and acceleration
0:15 and returns the product of the arguments,
0:17 put this function in the module called py3code.
0:22 Let's open up py3code here,
0:26 and it says at the bottom put keyword tests here
0:34 I can split this if I want to and view both of these.
0:38 I want to make a function called force that takes mass and acceleration here
0:42 so def force and it takes mass and acceleration
0:52 and returns mass times acceleration.
0:59 Okay, I will save this now and run this guy and see if it works.
1:09 Okay, and so it looks like I got an error here on this line here,
1:14 when I call force 10, 9.8 it did not raise a type error and it wants to raise a type error.
1:19 The reason why that is is because I didn't actually make a keyword only function
1:24 I just made a normal function that takes mass and acceleration.
1:28 So this will work and it won't raise a type error.
1:31 The intention of the keyword only arguments is
1:33 that it makes clear what our numbers are
1:36 in this case it's not clear necessarily which one is mass and which one is acceleration.
1:40 So in order to change that what we need to do is we need to put a star right in front of that.
1:45 And now when we call this force function,
1:47 we need to specify the mass and acceleration.
1:50 Let's run it and make sure that it works.
1:55 Okay, and now I'm on the other problem here
1:58 so that part appeared to work.
2:00 Let's go on to the next problem,
2:03 the quadratic formula solves an equation of the form ax^2+ bx + c = 0
2:09 write a function quad that returns a tuple with the solutions
2:13 make a, b and c keyword only arguments,
2:17 put this function in a module called
2:23 Okay, so if you remember the quadratic formula,
2:28 it looks something like this, negative b plus or minus the square root
2:36 of b squared minus 4ac and then all of this over 2a
2:50 so there's my attempt at writing this out in some little language here.
2:55 Let's see if we can implement this as a Python function here
2:58 and with keyword only arguments
3:00 so quad, and I'm going to put a star at the front
3:02 because I want everyone to specify a, b and c when they call this.
3:08 And because this can return 2 results,
3:10 it can return the positive of the square root and the negative of the square root,
3:14 we're going to make 2 results and return that as a tuple of both of those.
3:19 So what I'm going to do is I'm just going to say,
3:24 the square root part I'm going to say sqrt is equal to
3:28 let's say b squared minus 4 times a, times c to the .5
3:40 and then x1 is going to be equal to in parenthesis negative b plus
3:48 the square root portion and this divided by 2 times a
3:53 and the other solution will be this negative b minus the square root of that
4:02 and let's return x1 and x2.
4:07 Let's run our test over here and make sure that it works.
4:14 Okay, and we got that it did indeed work.
4:18 So we can see the calling here rather than saying quad 1, 3, 1 here.
4:24 we have to explicitly say a is equal to 1, b is equal to 3 and c is equal to 1.
4:31 Note that we can change the order of those if we want to as well.
4:34 This just allows us to again be more explicit and not have magic numbers floating around
4:38 but to have some context around them.