Detailed Notes
We continue our review of functions with an episode digging into parameter options including default values, variable numbers of arguments, and named parameters.
Useful Links: https://www.programiz.com/python-programming/recursion https://www.programiz.com/python-programming/function-argument
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
[Music] hello and welcome back we are continuing our series of tutorials where we're walking through basically walk our way up to a python certification this episode we're continuing our exploration of functions and how they work within python we started with just some basics and defining them and either returning if we go back to that one actually let me flip that back up either returning a value like that or doing a yield these can become important to some extent using those concepts as we move forward and we talk about parameters so today let's start with if you remember we had for example an add where if you gave it an a and a b if i just did print add three comma four oh i need to do that and it's gonna be a string and if i do that then it's gonna just print that out so there you go so if i do three and four that's great now with this it doesn't really matter you know whether we do three and four or if we do four and three however let's say we do let's change this to address and now we're gonna have city state and zip and then what we want to do is uh let's just say are at and then we're gonna do this we're just gonna do uh city and this is probably not the best way to do it because i should have used my little actually let's do this uh let's see what was that comma city state and then zip [Music] format and then we'll do city state zip and then turn right now actually let's just print it now we'll return it for now okay so we're gonna turn that and then we're gonna do print and i want to do print address and i call that with my city state and zip so zip code's gonna be one two three four state will be about say pick illinois and city will be somewhere so now if we run this one so you're at somewhere illinois there's your zip code but now what if i switch those [Music] and it's not going to look like you are at and it just doesn't look right because you don't know what is what necessarily now this you could probably figure it out because let's face it those are very different but if you didn't understand what zip codes were or if you had city and state names that seemed familiar like say new york could be a city could be a state then it could confuse things so what we can do here is we can take this and we can say using the names that are up there so we could say zip equals source the city equals and state equals so if we do that now we're back to the proper one so we don't have to we don't have to keep the order of parameters in general but that's something we're going to look at right here is now what i can do let's create a new one so let's do hello and let's do a name and a time and all this is going to do is just going to say hello to somebody uh it it is and then there's going to be some time let's just do that format take those two note that that format we talked about back in uh one of the string one of the operators or maybe it was in strings anywhere wherever it was i guess probably was strings can be very great way to quickly put stuff together so here we do name and time and if i call hello and it's just gonna be uh bob and let's see morning so that's gonna work fine so if i do that you know hello bob this morning but let's say that i don't always want i don't necessarily always need that so i can just default it is let's say a generic time of the day well that was a lot of so now this goes back to someone as we get a positional now i can do the same thing and i don't have to call because i have this default value i can call that and we see here that it just picks up whatever my default is and i could do this with numbers so i could do add if i do like add two and let's just say b equals 2. so here i could come in and i could just make it three gonna get those balanced right but there we go and if i get that then i get then it's just gonna add to that so i could actually call that multiple times and i'm going to see it gets to oh i'm sorry because it's not adding to the prior one my mistake so there we've got a we've got a default and we could actually do let's say with this address let's do let's just call it location change this around a little bit and i want to do this so now let's do location and let's say um i start with a zip code i need at least a zip code and state would be let's say not entered city equals entered so now let's go back to my little zip code so let's do a couple of these the first one we're going to do is just that and we know it we know this one's going to work because we've just seen it well okay we don't totally know it and now let's do so for city that's going to be nowhere there's a city so let's look at those two and then let's see what happens if we do the state i'll make it ohio this time now notice here you are at not enter not entered so city state zip here nowhere comes in as city state zip and state wasn't entered but notice that because of the ordering it's going to sit here and say hey this you know this doesn't work i'm i you know it assumes because we haven't said anything that the state is uh that the city is a value and not state it does we're back to assuming a an order now let's look at this so now if we name it though then we can see where you know city was blank and so we can use named and default values combined to have default values throughout our um our definition so i could actually take this and if i changed that to my oh yeah so if i change that then i can actually call so now i've got a default for every value and if i call address we'll see that up here well let's just see let's just do so i can make sure we see it as an empty values example and then we're going to see here somewhere in illinois one two three four five and i could just as easily say stay equal so let me change that and now we're going to see here i just changed the state but i picked up the other default values so we can default and use names to actually have a pretty long list of values and and work with those another one we can do is we can have a series so now i can do let's see i'm just going to call it sum and it's gonna be values and we put a star on this oh actually i think it's this way my mistake and then uh let's for item and values let's see let's see result equals zero result equals result plus item return result [Music] and then we can do print string sum and so we can do like a whole bunch of values and we're going to see here that it adds those up as it walks through those and deals with them now here this is much like when we did the generator before uh it's like actually look at this let's see what it says what it does when i actually print out values you should see that well there it doesn't show it as a generator but it does show it as it's just an array and it's going to walk through those values or you can walk through those values and do what you need to do with them now another thing you can do is um recursion let's see how do i want to well actually so i could take this same thing and i can do sum too and this goes back we're doing returns right now but we could do something like that where we can also use the yield and i can actually see it as i um let's see the result equals that and i'm just gonna do that so now what i'm gonna do is i'm gonna see each of the i'm gonna see it actually build up oh except for now i've gotta do um i gotta change this a little bit because now i've gotta do result here equals not one of those and then uh for x in results print x so now let me see this so i can see so it first comes through and it does one then does one plus two one plus two plus three one plus two so you can see that build out so we use yield just as easily another thing we can do is we can do recursion so the famous one for that would be factorial oh i have to define it factorial and it's going to take a value of x and then it's going to say and factorial means so like uh 2 fact well i'll say 3 factorial is actually 3 times 2 times 1. so it's and 4 factorial would be 4 4 times 2 times 1. so that's what we're gonna we're going to generate so for a factorial what we want to do is if you notice that 4 is actually the same as 4 times 3 factorial and so 3 is the same as 2 times factorial so what we want to do is we're going to return x times x oh sorry factorial of x minus one and i should spell return right and then if i do print a string let's do five factor of sorry factorial five oops uh what i forget to do and let's see so i need to do if i have to have this returned uh value so let's say if x equals one return one else return that so my mistake so then i'm going to return that let's see how that looks oops there we go so at each point here um yeah i don't think i'm going to add a print to it so you see here at there there's our thing there's our final value now we could also do at each part of these we could do a yield and we can see that again um so result equals that and for example okay so we're gonna do this uh oh sorry oh the problem with the yield here is that what we need to do is it needs to actually be we have to convert that to an int so this is going to be into x because now we're screwing around with a little bit so it's going to change the type up let's see oh i'm sorry okay yeah that's not going to work if we do a yield here then yield actually generates because it sends back a generator it's not going to build it right so i'd have to actually peel it off of the generator so my mistake but as you can see we can do a recursive call and we can just keep calling this you know and we could have two of them so we could have if we wanted we could have like a define we could have a that does something and it calls b and we could have a b that calls a to some extent and we can do so let's show that real quick got a minute so now if i call a it's going to blow up because we get a maximum recursion death because they just keep going back and forth and we'll see that yeah because it just shows us it just blew out the stack so i can do this or i could counter have a counter or something like that and of course this is a you know not a very useful example but you can also have things call each other so i think that's a good point for here we've we've gone through good deals stuff particularly focused on our parameters and note that there's many ways that you can send parameters around just as you can either send a single result or a a generator of results that comes back that'll do it for today so as always go out there have yourself a great day a great week and we will talk to you next time you
Transcript Segments
[Music]
hello and welcome back
we are continuing our series of
tutorials where we're walking through
basically walk our way up to a python
certification
this episode we're continuing our
exploration of
functions and how they work within
python
we started with just some basics and
defining them and
either returning if we go back to that
one
actually let me flip that back up either
returning a value
like that or doing a yield
these can become important to some
extent using those concepts as we move
forward and we talk about
parameters so today let's start with
if you remember we had for example an
add where if you gave it an a and a b
if i just did print add three comma four
oh i need to do that and it's gonna be a
string
and if i do that then it's gonna just
print that out so there you go so if i
do three and four that's great
now with this it doesn't really matter
you know whether we do three and four
or if we do four and three however
let's say we do
let's change this to address
and now we're gonna have city state and
zip
and then what we want to do
is uh let's just say
are at
and then we're gonna do this we're just
gonna do uh city
and this is probably not the best way to
do it because i should have used my
little
actually let's do this
uh let's see what was that comma city
state
and then zip
[Music]
format and then we'll do city state zip
and then turn right now
actually let's just print it now we'll
return it for now
okay so we're gonna turn that and then
we're gonna do print
and i want to do print address
and i call that with my city state and
zip
so zip code's gonna be one two three
four state will be about
say pick illinois and city will be
somewhere
so now if we run this one
so you're at somewhere illinois there's
your zip code
but now what if i switch those
[Music]
and it's not going to look like you are
at and it just doesn't look right
because you don't know what is what
necessarily now this you could probably
figure it out because
let's face it those are very different
but if you didn't understand what zip
codes were
or if you had city and state names that
seemed familiar like
say new york could be a city could be a
state
then it could confuse things so what we
can do here
is we can take this
and we can say using the names that are
up there so we could say
zip equals source the city equals
and state equals
so if we do that now
we're back to the proper one so we don't
have to
we don't have to keep the order of
parameters
in general but that's something we're
going to look at right here
is now what i can do let's create a new
one
so let's do hello
and let's do
a name and a time
and all this is going to do is just
going to say
hello
to somebody uh it
it is and then there's going to be some
time
let's just do that format
take those two note that that format we
talked about
back in uh one of the string one of the
operators
or maybe it was in strings anywhere
wherever it was i guess probably was
strings
can be very great way to quickly put
stuff together
so here we do name and time and
if i call hello
and it's just gonna be uh
bob and let's see morning
so that's gonna work fine so if i do
that you know hello bob this morning
but let's say that i don't always want i
don't necessarily always need
that so i can just default
it is let's say a
generic time of the day
well that was a lot of
so now this goes back to someone as we
get a positional now i can do the same
thing
and i don't have to call because i have
this default value
i can call that and we see here that it
just picks up whatever my default is
and i could do this with numbers so i
could do add
if i do like add two
and let's just say b equals 2.
so here
i could come in
and i could just make it three gonna get
those balanced right
but there we go and if i get that then i
get then it's just gonna add to that
so i could actually call that multiple
times
and i'm going to see it gets to oh i'm
sorry because it's not adding to the
prior one my mistake
so there we've got a
we've got a default and we could
actually do
let's say with this address
let's do
let's just call it location change this
around a little bit
and i want to do this
so now let's do location
and let's say um
i start with a zip code i need at least
a zip code
and state would be let's say not entered
city equals
entered
so now let's go back to my little zip
code so let's do a couple of these the
first one we're going to do
is just that and we know it we know this
one's going to work because we've just
seen it well
okay we don't totally know it and now
let's do
so for city that's going to be nowhere
there's a city so let's look at those
two and then let's see what happens
if we do the state
i'll make it ohio this time now notice
here
you are at not enter not entered so city
state zip
here nowhere comes in as city state
zip and state wasn't entered but notice
that because
of the ordering it's going to sit here
and say hey this
you know this doesn't work i'm i you
know it assumes
because we haven't said anything that
the state
is uh that the city is a value and not
state it does we're
back to assuming a
an order now let's look at this
so now if we name it though
then we can see where you know city was
blank and so
we can use named and default values
combined to have default values
throughout
our um our definition so i could
actually take this
and if i changed that
to my
oh yeah so if i change that then i can
actually call so now i've got a default
for every value and if i call address
we'll see that up here well
let's just see let's just do
so i can make sure we see it as an empty
values example and then we're going to
see
here somewhere in illinois one two three
four five
and i could just as easily say stay
equal so let me change that
and now we're going to see here i just
changed the state but i picked up the
other
default values so we can default
and use names to actually have a pretty
long list of values and and work with
those
another one we can do is we can have a
series
so now i can do let's see
i'm just going to call it sum and it's
gonna be
values and we put a star on this
oh actually i think it's this way my
mistake
and then uh let's for item
and values
let's see let's see result equals zero
result equals result plus item
return result
[Music]
and then we can do print
string sum and so we can do like a whole
bunch of values
and we're going to see here that it adds
those up as it walks through those
and deals with them now here this is
much like when we did
the generator before uh it's like
actually look at this
let's see what it says what it does when
i actually print out values
you should see that well there it
doesn't show it as a generator but it
does show it as
it's just an array and it's going to
walk through those values
or you can walk through those values and
do what you need to do with them
now another thing you can do is um
recursion let's see how do i want to
well actually so i could take this same
thing
and i can do sum too and this goes back
we're doing returns right now
but we could do something like that
where we can also use the yield
and i can actually see it as i
um let's see
the result equals that
and i'm just gonna do that so
now what i'm gonna do is i'm gonna see
each of the i'm gonna see it actually
build up
oh except for now i've gotta do um
i gotta change this a little bit because
now i've gotta do
result here equals
not one of those and then uh for
x in results
print x so now
let me see this so i can see so it first
comes through and it does one then does
one plus two
one plus two plus three one plus two so
you can see that build out
so we use yield just as easily
another thing we can do is we can do
recursion so the famous one for that
would be factorial
oh i have to define it factorial
and it's going to take a value of x
and then it's going to say and factorial
means so like
uh 2 fact well i'll say 3 factorial is
actually 3
times 2 times 1. so it's and 4
factorial would be 4
4 times 2 times 1. so that's what we're
gonna we're going to generate
so for a factorial what we want to do is
if you notice that 4 is actually the
same as 4 times
3 factorial and so 3 is the same as 2
times
factorial so what we want to do is we're
going to return
x times
x oh sorry factorial
of x minus one and i should spell return
right
and then if i do print
a string let's do five factor
of sorry factorial
five
oops uh what i forget to do
and let's see so i need to do
if i have to have this returned
uh value so let's say if x equals one
return one
else return that so my mistake
so then i'm going to return that let's
see how that looks
oops there we go
so at each point here
um yeah i don't think i'm going to add a
print to it so you see here at
there there's our thing
there's our final value now we could
also do
at each part of these we could do a
yield and we can see that again
um
so result equals
that and for example okay so we're gonna
do this
uh oh sorry
oh the problem with the yield here is
that what we need to do
is it needs to actually be
we have to convert that to an int
so this is going to be
into x because now we're screwing around
with a little bit
so it's going to change the type up
let's see
oh i'm sorry okay yeah that's not going
to work if we do a yield here then
yield actually generates because it
sends back a generator
it's not going to build it right so i'd
have to actually peel it off of the
generator so
my mistake but as you can see
we can do a recursive call and we can
just keep calling this
you know and we could have two of them
so we could have if we wanted we could
have like a define
we could have a that does something and
it calls b
and we could have a b that
calls a to some extent
and we can do so let's show that real
quick got a minute
so now if i call a it's going to blow up
because we get a maximum recursion death
because they just keep going back and
forth
and we'll see that
yeah because it just shows us it just
blew out the stack
so i can do this or i could counter have
a counter or something like that and of
course
this is a you know not a very useful
example but
you can also have things call each other
so i think that's a good point for here
we've
we've gone through good deals stuff
particularly focused on our parameters
and note that there's many ways that you
can send parameters around
just as you can either send a single
result
or a a generator of results that comes
back
that'll do it for today so as always go
out there
have yourself a great day a great week
and we will talk to you
next time
you