📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Functions

2021-08-03 •Youtube

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
0.46

[Music]

26.16

well hello and welcome back

27.76

we are continuing our series of

30.64

tutorials and lessons

32.48

focused on python certification

35.92

this episode we're going to look at

38

functions this is actually going to take

40.399

there's a lot of things about functions

42

and classes that we're going to cover

44

so this will be one in a a series of

47.76

uh tutorials essentially or examples

49.84

we're going to go through

51.28

we'll start with the most basics and

53.039

we've we've seen this

54.48

to some extent although we haven't

56.64

really looked at too closely

59.44

functions in python have a couple key

62.8

things

63.199

they have the def so if i'm going to

65.68

have just a straight function i'm going

67.04

to give it

67.6

i'm going to do a df space and then the

69.76

name

71.2

and then i'm going to have open and

72.56

close parens and then

74.64

a colon and then whatever it is for that

77.759

function

78.24

and to call it let's just do like this

80.96

call

81.439

the function

84.799

and to call it all i do is do it

87.92

with the uh since i'm not saying

89.36

parameters open and close parents

91.439

so this one all i'm doing is just just

92.96

gonna do a print and note the

94.799

uh indenting and then once i unindent

97.759

i'm back outside of it you don't have to

99.36

do an

100.24

end or anything like that so if i take

102.64

that

104.32

and i go run it then we can see here

108.72

you know my function or let me say

113.6

bit this way call

116.76

[Music]

119.119

call to my funk

122.24

just to make sure that it's clear and we

124.24

can see that there so it's calling this

126

print

126.479

so the most basics that's what i have to

128.56

worry about now another thing we can do

134.4

is let's throw a little inline there

137.599

that line there

142.8

so now i can do a second one can't have

144.959

the same name

146

so i'm gonna have a second one and well

148.56

let's do that real quick so i'm gonna

149.68

add a parameter this time this is gonna

151.12

be

151.44

a value

154.8

and then i'm going to call it again with

157.04

a value which is going to be

160.48

my value and now let's see what happens

164.64

so notice that uh here

168.319

that first one

171.519

it's going to say missing required

173.2

positional value argument

174.959

because what happened is is when i

176.56

defined this the second time

178.72

it stepped on this definition

182.08

so you may see some things like that and

185.12

that's why i'm sort of pointing that out

186.64

is that when you define something it

188.72

just redefines it

190.4

whatever was already there so if i did

192.08

it this way

194.879

if i move this here and call it and then

197.599

turn around and call it

199.519

later with a value they both work

205.36

um let's say

209.12

with value

212.72

oop and

215.92

i could use the format but instead i'm

217.92

just going to do

221.84

it this way

226.64

so now we can see that the first time it

228.319

calls it and then we redefine it

230.959

and we send it a parameter so you want

233.439

to make sure that you

235.12

don't redefine something that's already

237.439

been defined it doesn't warn you on that

239.2

necessarily it

240

only does the signature changes then

242.56

you'll see the

244

the break you'll see where you made a

246

mistake otherwise

247.76

you may have it defined twice and could

249.599

have all kinds of neat little

251.12

errors that come up in bugs that you're

252.799

trying to to chase down

255.439

so we can send it a value we can also

258.16

send it let's do my font three

261.54

[Music]

264.88

and i can call this whatever it is and i

266.4

can send it any type so i can do value

268.16

and value two

273.04

and

281.12

and this time i'm going to do value 2

283.84

but now what we can do is

285.199

let's look at if i call it here

293.12

this would be my other value and then

296.639

i'm going to give it a

297.68

2. and what we'll find here

302.08

is something we run into a lot is it

304.24

works fine to some extent

306.8

but so it prints that first line either

308.88

value but then we run into this

310.56

string so even though we don't have a

313.28

type here

314.8

we have to be aware of that and somehow

317.28

handle whatever that type may be

320

so we could do let's see if

327.039

let's see

334.72

let's do if it uh

338.08

well

338.34

[Music]

341.28

so now we can we can set a couple values

344

we can also do

346.96

various types although we have to know

348.72

what they are so we could do

350.639

let's see value one value two

354.8

let's do four uh x

358.16

in value two

363.44

let's just do parenthesis x

367.52

so we can also set it here

374.319

so let's do a b

378.84

c

381.87

[Music]

385.919

let's do that

389.039

and so we see here where it comes

390.319

through and it prints each of those

392.84

values

394.96

i guess we'll drop that off of this one

396.88

so there's a lot of things we can do

398

basics we can send whatever we want as

400.08

far as

401.199

parameters concerned now we can also do

405.039

let's do add

408.4

a comma b nice simple one

411.44

and then we're gonna return a plus b

417.039

we've seen this one before so we can

419.36

call add

421.12

1 comma 2 and we're going to print

428.319

and so now we can print we can use that

430.96

return value but

432.96

because it's a string it's an integer we

435.199

do have to convert it

438.4

so we'll do that and we call it and then

441.199

we get this three

442.4

down there so that's the i will just do

444.88

that

446.84

[Music]

448.16

sum is we'll do that

450.45

[Music]

451.599

and we can see that there it is so we

453.12

can return a value

455.36

now we can also return something complex

460.49

[Music]

462.4

so let's return um

466.8

oh let's did you turn

472.319

uh well we'll just do something like

473.68

this we'll just return i'll just call it

475.759

address

476.479

so this would be something complex and

478.879

we're going to send it well

480

we're going to do that so

483.84

so let's do address equals

488.96

let's do we're going to call return

490.84

value

492.24

just because

496.96

and we can do more complicated stuff but

500.08

let's just do this

513.76

so if we do this guy so we're going to

515.599

turn something a little more complicated

517.44

and then uh let's just do print address

520.78

[Music]

528.64

and we'll see down here that it returns

531.2

a list we can also return classes and

533.2

other things which we are going to

535.44

look at another value that we can

538.08

another thing we do is

539.04

yield and what yield does is it actually

542.959

builds so let's i don't want to do that

548.24

i want to do this okay and actually let

549.76

me close a couple of these while i'm

552.24

cleaning my workspace up a little bit so

554.72

now let's take the same thing

557.44

uh let's do the add

562

and we're gonna do add two and instead

564.64

of return we're gonna yield

570

and let's look at the difference between

571.44

these two so if we look at uh

573.44

where we put so add

578.24

and add two

585.519

let's see how this one looks and so you

588.64

see here

589.68

it's a generator

592.8

so there's a generator object so when we

594.64

do a yield

596.24

it's not actually it's not the same it's

599.6

actually generate

600.64

sending a generator back and the idea

602.32

here is

603.76

is that you can then go let's see for

607.92

i know in

620.24

print item

623.839

so now if we do this let's see how that

625.519

happens

629.279

so now we see that it comes back out so

631.36

what we're seeing here is it's four

632.72

items so i can actually do

635.68

um multiple i can actually build these

638.64

things up and actually have a series

640.16

which we see

641.04

in scraping uh often is it or other

643.6

things where you've got just a function

644.88

you're going to call across

645.68

a series of things so now i could do

649.6

um

652.8

well let's do this one

656.48

let's do a plus b a times b

660.64

a minus b and a divided by b

666

and if i do that i'm going to do a

668

string of these

672.16

although notice that when i did the add

674.56

two it didn't complain

676.16

it actually stringified it because it

677.76

was an object it's not an

679.44

int anymore so actually i think if i do

682

that here

684.88

and so now i'm going to see where i've

686.079

got multiple yields there you go

688.72

as you can see that i've got each of

690.079

those values kick out and they're

691.36

strings

693.44

a yield is going to send me back that

696.079

object

697.2

but within it it's essentially it's

699.12

already converted that into a string for

700.839

me

702.16

so i can do this where i want to call

704.959

something so if i do like a

707.2

my array

710.399

equals let's do it a

714.839

b

716.88

a b and c so i'm going to take a series

719.44

of values

721.2

and now what i'm going to do is i'm

724.32

going to define

725.519

i'm just going to call it walker and

728.8

it's going to take

731.68

a value

733.81

[Music]

741.44

and it just does print

747.519

the value well

751.12

let me just say yield

754.72

um value

761.2

plus value

770.32

and then i'm gonna call so i can do this

778.16

so oh actually let me do uh let's do

781.519

print first

783.519

and then i'm gonna do walker

789.76

and actually i can return that

794.48

and now here i'm going to take an array

798.399

and here i'm going to uh do for

801.94

[Music]

803.6

x and array and then i'm going to do

807.04

yield

809.519

print oh that's gonna be my print shoot

812.079

it's nothing like that

814.88

you'll be on my print

825.12

and this is a contrived example but

827.36

moderately useful so now i'm going to do

828.72

my print

830.399

x

834.48

and now i'm going to come down here and

836.48

that's called my array so now i can do

838.32

i'll call it here so i'm just going to

839.6

call

841.199

walker well

848.32

so here

853.279

for i am in walker and i'm gonna send it

856.88

the array

860.72

print item okay let's just do it that

862.32

way let's see how this works

864.8

and here we go so

869.6

uh here we go all the way up at the top

872.88

sorry so here's our walker let me do

875.199

this just to clean this up a little bit

879.44

uh walker example

888.48

end of okay so we can just see that a

890.56

little better let's do a clear

893.839

so see here for the walker example it

895.44

comes in and we call this

898.8

and then for each item it just builds up

900.72

this list

902.32

and then it kicks it out and then i can

903.839

actually walk that list through that

905.12

generator

906.32

so now what i've got is something that i

908.24

can actually build up

909.68

a series of things and work within that

912.72

and i could actually build um and

914.959

there's it could be recursive and things

916.56

like that so there's all kinds of neat

918

little things i can do

919.76

with yield which allows me to build on

923.12

results and actually nest things which

925.839

gets us into some of the

927.12

later things we're going to talk about

928.399

we talk about lambdas and things of that

930.079

nature

930.8

that we can actually which is a powerful

932.959

feature of python that's also

934.639

as you can see very easy to implement

937.92

and again as i've mentioned earlier

940.959

if you look at the scrapey project you

943.279

can see

944.88

which is for web scraping it uses yield

949.36

and turns itself into a very powerful

953.12

crawler

954.079

basically because you got to realize

955.279

that's what you want to do is you're

956.399

going to

957.36

with a crawler you're going to go in

959.92

you're going to look at some stuff

961.839

and then you're going to do something

962.959

with it you're going to have to kick

964.079

that out but then you're also going to

965.199

probably have to call a deeper step

967.519

so there's a there's a couple things

969.04

there that you can look at

970.8

that are going to help you out um

973.839

you know in making python a little more

976.079

powerful for you

977.839

so i think this good point right now as

980.079

we've got some basics we've got our defs

981.92

we've got

982.639

some parameters we're able to return

984.32

we're able to yield and then next time

986.399

around we're going to come back and

987.68

we're going to do a little bit more

989.44

in the the world of functions and expand

991.68

on those probably

992.8

actually next one probably look at

994

classes a little bit and then swing

996

back around to to start wheeling and

997.92

dealing with some of these structures

999.279

we've kept it simple so far

1001.12

now we're getting into some of the more

1002.399

complex stuff uh complex functionality

1005.199

and and data structures

1006.959

that being said as always shoot me a

1008.72

question if you have any or

1010

show me a line if you have any questions

1011.279

about any of this i probably don't

1012.72

mention that enough

1013.759

you can see it i think in the notes you

1015.279

can reach us at info developmentor.com

1018.48

if you have questions about any of these

1019.839

or leave comments out on the

1021.68

the video itself that being said i will

1024.64

let you get back to it

1026.16

so go out there have yourself a great

1027.52

day a great week and we will talk to you

1030.64

next time

1046.88

you