#
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. Let's open that up, open it in your editor. Let's read the first assignment,

0:08
it says one formula for force is mass times acceleration create a function force that takes to keyword only arguments mass and acceleration

0:16
and returns the product of the arguments, put this function in the module called py3code. Let's open up py3code here,

0:27
and it says at the bottom put keyword tests here I can split this if I want to and view both of these.

0:39
I want to make a function called force that takes mass and acceleration here so def force and it takes mass and acceleration

0:53
and returns mass times acceleration. Okay, I will save this now and run this guy and see if it works.

1:10
Okay, and so it looks like I got an error here on this line here,

1:15
when I call force 10, 9.8 it did not raise a type error and it wants to raise a type error.

1:20
The reason why that is because I didn't actually make a keyword only function I just made a normal function that takes mass and acceleration.

1:29
So this will work and it won't raise a type error. The intention of the keyword only arguments is that it makes clear what our numbers are

1:37
in this case it's not clear necessarily which one is mass and which one is acceleration.

1:41
So in order to change that what we need to do is we need to put a star right in front of that. And now when we call this force function,

1:48
we need to specify the mass and acceleration. Let's run it and make sure that it works. Okay, and now I'm on the other problem here

1:59
so that part appeared to work. Let's go on to the next problem, the quadratic formula solves an equation of the form ax^2+ bx + c = 0

2:10
write a function quad that returns a tuple with the solutions make a, b and c keyword only arguments, put this function in a module called py3code.py

2:24
Okay, so if you remember the quadratic formula, it looks something like this, negative b plus or minus the square root

2:37
of b squared minus 4ac and then all of this over 2a so there's my attempt at writing this out in some little language here.

2:56
Let's see if we can implement this as a Python function here and with keyword only arguments so quad, and I'm going to put a star at the front

3:03
because I want everyone to specify a, b and c when they call this. And because this can return 2 results,

3:11
it can return the positive of the square root and the negative of the square root,

3:15
we're going to make 2 results and return that as a tuple of both of those. So what I'm going to do is I'm just going to say,

3:25
the square root part I'm going to say sqrt is equal to let's say b squared minus 4 times a, times c to the .5

3:41
and then x1 is going to be equal to in parenthesis negative b plus the square root portion and this divided by 2 times a

3:54
and the other solution will be this negative b minus the square root of that and let's return x1 and x2.

4:08
Let's run our test over here and make sure that it works. Okay, and we got that it did indeed work.

4:19
So we can see the calling here rather than saying quad 1, 3, 1 here. we have to explicitly say a is equal to 1, b is equal to 3 and c is equal to 1.

4:32
Note that we can change the order of those if we want to as well.

4:35
This just allows us to again be more explicit and not have magic numbers floating around but to have some context around them.