Detailed Notes
This episode of python certification starts the discussion of functions and return values via return or yield.
Useful Links: https://realpython.com/python-keywords/#returning-keywords-return-yield
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] well hello and welcome back we are continuing our series of tutorials and lessons focused on python certification this episode we're going to look at functions this is actually going to take there's a lot of things about functions and classes that we're going to cover so this will be one in a a series of uh tutorials essentially or examples we're going to go through we'll start with the most basics and we've we've seen this to some extent although we haven't really looked at too closely functions in python have a couple key things they have the def so if i'm going to have just a straight function i'm going to give it i'm going to do a df space and then the name and then i'm going to have open and close parens and then a colon and then whatever it is for that function and to call it let's just do like this call the function and to call it all i do is do it with the uh since i'm not saying parameters open and close parents so this one all i'm doing is just just gonna do a print and note the uh indenting and then once i unindent i'm back outside of it you don't have to do an end or anything like that so if i take that and i go run it then we can see here you know my function or let me say bit this way call [Music] call to my funk just to make sure that it's clear and we can see that there so it's calling this print so the most basics that's what i have to worry about now another thing we can do is let's throw a little inline there that line there so now i can do a second one can't have the same name so i'm gonna have a second one and well let's do that real quick so i'm gonna add a parameter this time this is gonna be a value and then i'm going to call it again with a value which is going to be my value and now let's see what happens so notice that uh here that first one it's going to say missing required positional value argument because what happened is is when i defined this the second time it stepped on this definition so you may see some things like that and that's why i'm sort of pointing that out is that when you define something it just redefines it whatever was already there so if i did it this way if i move this here and call it and then turn around and call it later with a value they both work um let's say with value oop and i could use the format but instead i'm just going to do it this way so now we can see that the first time it calls it and then we redefine it and we send it a parameter so you want to make sure that you don't redefine something that's already been defined it doesn't warn you on that necessarily it only does the signature changes then you'll see the the break you'll see where you made a mistake otherwise you may have it defined twice and could have all kinds of neat little errors that come up in bugs that you're trying to to chase down so we can send it a value we can also send it let's do my font three [Music] and i can call this whatever it is and i can send it any type so i can do value and value two and and this time i'm going to do value 2 but now what we can do is let's look at if i call it here this would be my other value and then i'm going to give it a 2. and what we'll find here is something we run into a lot is it works fine to some extent but so it prints that first line either value but then we run into this string so even though we don't have a type here we have to be aware of that and somehow handle whatever that type may be so we could do let's see if let's see let's do if it uh well [Music] so now we can we can set a couple values we can also do various types although we have to know what they are so we could do let's see value one value two let's do four uh x in value two let's just do parenthesis x so we can also set it here so let's do a b c [Music] let's do that and so we see here where it comes through and it prints each of those values i guess we'll drop that off of this one so there's a lot of things we can do basics we can send whatever we want as far as parameters concerned now we can also do let's do add a comma b nice simple one and then we're gonna return a plus b we've seen this one before so we can call add 1 comma 2 and we're going to print and so now we can print we can use that return value but because it's a string it's an integer we do have to convert it so we'll do that and we call it and then we get this three down there so that's the i will just do that [Music] sum is we'll do that [Music] and we can see that there it is so we can return a value now we can also return something complex [Music] so let's return um oh let's did you turn uh well we'll just do something like this we'll just return i'll just call it address so this would be something complex and we're going to send it well we're going to do that so so let's do address equals let's do we're going to call return value just because and we can do more complicated stuff but let's just do this so if we do this guy so we're going to turn something a little more complicated and then uh let's just do print address [Music] and we'll see down here that it returns a list we can also return classes and other things which we are going to look at another value that we can another thing we do is yield and what yield does is it actually builds so let's i don't want to do that i want to do this okay and actually let me close a couple of these while i'm cleaning my workspace up a little bit so now let's take the same thing uh let's do the add and we're gonna do add two and instead of return we're gonna yield and let's look at the difference between these two so if we look at uh where we put so add and add two let's see how this one looks and so you see here it's a generator so there's a generator object so when we do a yield it's not actually it's not the same it's actually generate sending a generator back and the idea here is is that you can then go let's see for i know in print item so now if we do this let's see how that happens so now we see that it comes back out so what we're seeing here is it's four items so i can actually do um multiple i can actually build these things up and actually have a series which we see in scraping uh often is it or other things where you've got just a function you're going to call across a series of things so now i could do um well let's do this one let's do a plus b a times b a minus b and a divided by b and if i do that i'm going to do a string of these although notice that when i did the add two it didn't complain it actually stringified it because it was an object it's not an int anymore so actually i think if i do that here and so now i'm going to see where i've got multiple yields there you go as you can see that i've got each of those values kick out and they're strings a yield is going to send me back that object but within it it's essentially it's already converted that into a string for me so i can do this where i want to call something so if i do like a my array equals let's do it a b a b and c so i'm going to take a series of values and now what i'm going to do is i'm going to define i'm just going to call it walker and it's going to take a value [Music] and it just does print the value well let me just say yield um value plus value and then i'm gonna call so i can do this so oh actually let me do uh let's do print first and then i'm gonna do walker and actually i can return that and now here i'm going to take an array and here i'm going to uh do for [Music] x and array and then i'm going to do yield print oh that's gonna be my print shoot it's nothing like that you'll be on my print and this is a contrived example but moderately useful so now i'm going to do my print x and now i'm going to come down here and that's called my array so now i can do i'll call it here so i'm just going to call walker well so here for i am in walker and i'm gonna send it the array print item okay let's just do it that way let's see how this works and here we go so uh here we go all the way up at the top sorry so here's our walker let me do this just to clean this up a little bit uh walker example end of okay so we can just see that a little better let's do a clear so see here for the walker example it comes in and we call this and then for each item it just builds up this list and then it kicks it out and then i can actually walk that list through that generator so now what i've got is something that i can actually build up a series of things and work within that and i could actually build um and there's it could be recursive and things like that so there's all kinds of neat little things i can do with yield which allows me to build on results and actually nest things which gets us into some of the later things we're going to talk about we talk about lambdas and things of that nature that we can actually which is a powerful feature of python that's also as you can see very easy to implement and again as i've mentioned earlier if you look at the scrapey project you can see which is for web scraping it uses yield and turns itself into a very powerful crawler basically because you got to realize that's what you want to do is you're going to with a crawler you're going to go in you're going to look at some stuff and then you're going to do something with it you're going to have to kick that out but then you're also going to probably have to call a deeper step so there's a there's a couple things there that you can look at that are going to help you out um you know in making python a little more powerful for you so i think this good point right now as we've got some basics we've got our defs we've got some parameters we're able to return we're able to yield and then next time around we're going to come back and we're going to do a little bit more in the the world of functions and expand on those probably actually next one probably look at classes a little bit and then swing back around to to start wheeling and dealing with some of these structures we've kept it simple so far now we're getting into some of the more complex stuff uh complex functionality and and data structures that being said as always shoot me a question if you have any or show me a line if you have any questions about any of this i probably don't mention that enough you can see it i think in the notes you can reach us at info developmentor.com if you have questions about any of these or leave comments out on the the video itself that being said i will let you get back to it so go out there have yourself a great day a great week and we will talk to you next time you
Transcript Segments
[Music]
well hello and welcome back
we are continuing our series of
tutorials and lessons
focused on python certification
this episode we're going to look at
functions this is actually going to take
there's a lot of things about functions
and classes that we're going to cover
so this will be one in a a series of
uh tutorials essentially or examples
we're going to go through
we'll start with the most basics and
we've we've seen this
to some extent although we haven't
really looked at too closely
functions in python have a couple key
things
they have the def so if i'm going to
have just a straight function i'm going
to give it
i'm going to do a df space and then the
name
and then i'm going to have open and
close parens and then
a colon and then whatever it is for that
function
and to call it let's just do like this
call
the function
and to call it all i do is do it
with the uh since i'm not saying
parameters open and close parents
so this one all i'm doing is just just
gonna do a print and note the
uh indenting and then once i unindent
i'm back outside of it you don't have to
do an
end or anything like that so if i take
that
and i go run it then we can see here
you know my function or let me say
bit this way call
[Music]
call to my funk
just to make sure that it's clear and we
can see that there so it's calling this
so the most basics that's what i have to
worry about now another thing we can do
is let's throw a little inline there
that line there
so now i can do a second one can't have
the same name
so i'm gonna have a second one and well
let's do that real quick so i'm gonna
add a parameter this time this is gonna
be
a value
and then i'm going to call it again with
a value which is going to be
my value and now let's see what happens
so notice that uh here
that first one
it's going to say missing required
positional value argument
because what happened is is when i
defined this the second time
it stepped on this definition
so you may see some things like that and
that's why i'm sort of pointing that out
is that when you define something it
just redefines it
whatever was already there so if i did
it this way
if i move this here and call it and then
turn around and call it
later with a value they both work
um let's say
with value
oop and
i could use the format but instead i'm
just going to do
it this way
so now we can see that the first time it
calls it and then we redefine it
and we send it a parameter so you want
to make sure that you
don't redefine something that's already
been defined it doesn't warn you on that
necessarily it
only does the signature changes then
you'll see the
the break you'll see where you made a
mistake otherwise
you may have it defined twice and could
have all kinds of neat little
errors that come up in bugs that you're
trying to to chase down
so we can send it a value we can also
send it let's do my font three
[Music]
and i can call this whatever it is and i
can send it any type so i can do value
and value two
and
and this time i'm going to do value 2
but now what we can do is
let's look at if i call it here
this would be my other value and then
i'm going to give it a
2. and what we'll find here
is something we run into a lot is it
works fine to some extent
but so it prints that first line either
value but then we run into this
string so even though we don't have a
type here
we have to be aware of that and somehow
handle whatever that type may be
so we could do let's see if
let's see
let's do if it uh
well
[Music]
so now we can we can set a couple values
we can also do
various types although we have to know
what they are so we could do
let's see value one value two
let's do four uh x
in value two
let's just do parenthesis x
so we can also set it here
so let's do a b
c
[Music]
let's do that
and so we see here where it comes
through and it prints each of those
values
i guess we'll drop that off of this one
so there's a lot of things we can do
basics we can send whatever we want as
far as
parameters concerned now we can also do
let's do add
a comma b nice simple one
and then we're gonna return a plus b
we've seen this one before so we can
call add
1 comma 2 and we're going to print
and so now we can print we can use that
return value but
because it's a string it's an integer we
do have to convert it
so we'll do that and we call it and then
we get this three
down there so that's the i will just do
that
[Music]
sum is we'll do that
[Music]
and we can see that there it is so we
can return a value
now we can also return something complex
[Music]
so let's return um
oh let's did you turn
uh well we'll just do something like
this we'll just return i'll just call it
address
so this would be something complex and
we're going to send it well
we're going to do that so
so let's do address equals
let's do we're going to call return
value
just because
and we can do more complicated stuff but
let's just do this
so if we do this guy so we're going to
turn something a little more complicated
and then uh let's just do print address
[Music]
and we'll see down here that it returns
a list we can also return classes and
other things which we are going to
look at another value that we can
another thing we do is
yield and what yield does is it actually
builds so let's i don't want to do that
i want to do this okay and actually let
me close a couple of these while i'm
cleaning my workspace up a little bit so
now let's take the same thing
uh let's do the add
and we're gonna do add two and instead
of return we're gonna yield
and let's look at the difference between
these two so if we look at uh
where we put so add
and add two
let's see how this one looks and so you
see here
it's a generator
so there's a generator object so when we
do a yield
it's not actually it's not the same it's
actually generate
sending a generator back and the idea
here is
is that you can then go let's see for
i know in
print item
so now if we do this let's see how that
happens
so now we see that it comes back out so
what we're seeing here is it's four
items so i can actually do
um multiple i can actually build these
things up and actually have a series
which we see
in scraping uh often is it or other
things where you've got just a function
you're going to call across
a series of things so now i could do
um
well let's do this one
let's do a plus b a times b
a minus b and a divided by b
and if i do that i'm going to do a
string of these
although notice that when i did the add
two it didn't complain
it actually stringified it because it
was an object it's not an
int anymore so actually i think if i do
that here
and so now i'm going to see where i've
got multiple yields there you go
as you can see that i've got each of
those values kick out and they're
strings
a yield is going to send me back that
object
but within it it's essentially it's
already converted that into a string for
me
so i can do this where i want to call
something so if i do like a
my array
equals let's do it a
b
a b and c so i'm going to take a series
of values
and now what i'm going to do is i'm
going to define
i'm just going to call it walker and
it's going to take
a value
[Music]
and it just does print
the value well
let me just say yield
um value
plus value
and then i'm gonna call so i can do this
so oh actually let me do uh let's do
print first
and then i'm gonna do walker
and actually i can return that
and now here i'm going to take an array
and here i'm going to uh do for
[Music]
x and array and then i'm going to do
yield
print oh that's gonna be my print shoot
it's nothing like that
you'll be on my print
and this is a contrived example but
moderately useful so now i'm going to do
my print
x
and now i'm going to come down here and
that's called my array so now i can do
i'll call it here so i'm just going to
call
walker well
so here
for i am in walker and i'm gonna send it
the array
print item okay let's just do it that
way let's see how this works
and here we go so
uh here we go all the way up at the top
sorry so here's our walker let me do
this just to clean this up a little bit
uh walker example
end of okay so we can just see that a
little better let's do a clear
so see here for the walker example it
comes in and we call this
and then for each item it just builds up
this list
and then it kicks it out and then i can
actually walk that list through that
generator
so now what i've got is something that i
can actually build up
a series of things and work within that
and i could actually build um and
there's it could be recursive and things
like that so there's all kinds of neat
little things i can do
with yield which allows me to build on
results and actually nest things which
gets us into some of the
later things we're going to talk about
we talk about lambdas and things of that
nature
that we can actually which is a powerful
feature of python that's also
as you can see very easy to implement
and again as i've mentioned earlier
if you look at the scrapey project you
can see
which is for web scraping it uses yield
and turns itself into a very powerful
crawler
basically because you got to realize
that's what you want to do is you're
going to
with a crawler you're going to go in
you're going to look at some stuff
and then you're going to do something
with it you're going to have to kick
that out but then you're also going to
probably have to call a deeper step
so there's a there's a couple things
there that you can look at
that are going to help you out um
you know in making python a little more
powerful for you
so i think this good point right now as
we've got some basics we've got our defs
we've got
some parameters we're able to return
we're able to yield and then next time
around we're going to come back and
we're going to do a little bit more
in the the world of functions and expand
on those probably
actually next one probably look at
classes a little bit and then swing
back around to to start wheeling and
dealing with some of these structures
we've kept it simple so far
now we're getting into some of the more
complex stuff uh complex functionality
and and data structures
that being said as always shoot me a
question if you have any or
show me a line if you have any questions
about any of this i probably don't
mention that enough
you can see it i think in the notes you
can reach us at info developmentor.com
if you have questions about any of these
or leave comments out on the
the video itself that being said i will
let you get back to it
so go out there have yourself a great
day a great week and we will talk to you
next time
you