📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Parameters

2021-08-05 •Youtube

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
0.46

[Music]

26.32

hello and welcome back

27.84

we are continuing our series of

30.8

tutorials where we're walking through

33.2

basically walk our way up to a python

35.2

certification

36.8

this episode we're continuing our

39.52

exploration of

40.719

functions and how they work within

42.84

python

44

we started with just some basics and

45.84

defining them and

47.76

either returning if we go back to that

50.879

one

51.199

actually let me flip that back up either

53.6

returning a value

55.92

like that or doing a yield

60.079

these can become important to some

61.44

extent using those concepts as we move

64.08

forward and we talk about

65.439

parameters so today let's start with

69.28

if you remember we had for example an

72.56

add where if you gave it an a and a b

76.08

if i just did print add three comma four

81.04

oh i need to do that and it's gonna be a

84.84

string

88.799

and if i do that then it's gonna just

92.079

print that out so there you go so if i

94

do three and four that's great

96.32

now with this it doesn't really matter

97.92

you know whether we do three and four

99.68

or if we do four and three however

103.68

let's say we do

106.88

let's change this to address

110.72

and now we're gonna have city state and

113.6

zip

115.759

and then what we want to do

118.96

is uh let's just say

124.56

are at

128.239

and then we're gonna do this we're just

129.679

gonna do uh city

131.84

and this is probably not the best way to

133.599

do it because i should have used my

134.879

little

135.44

actually let's do this

138.959

uh let's see what was that comma city

141.52

state

143.599

and then zip

146.2

[Music]

148.319

format and then we'll do city state zip

156.56

and then turn right now

159.92

actually let's just print it now we'll

162.72

return it for now

163.68

okay so we're gonna turn that and then

165.599

we're gonna do print

170

and i want to do print address

173.76

and i call that with my city state and

176.239

zip

176.879

so zip code's gonna be one two three

178.4

four state will be about

180.159

say pick illinois and city will be

182.84

somewhere

185.04

so now if we run this one

188.48

so you're at somewhere illinois there's

191.04

your zip code

191.84

but now what if i switch those

199.09

[Music]

201.36

and it's not going to look like you are

203.12

at and it just doesn't look right

205.2

because you don't know what is what

206.959

necessarily now this you could probably

208.319

figure it out because

209.84

let's face it those are very different

211.2

but if you didn't understand what zip

213.44

codes were

215.12

or if you had city and state names that

216.72

seemed familiar like

218.239

say new york could be a city could be a

220.239

state

221.28

then it could confuse things so what we

224.159

can do here

226.159

is we can take this

229.44

and we can say using the names that are

232.799

up there so we could say

234.72

zip equals source the city equals

241.599

and state equals

245.28

so if we do that now

248.48

we're back to the proper one so we don't

251.84

have to

252.48

we don't have to keep the order of

254.799

parameters

255.84

in general but that's something we're

257.84

going to look at right here

259.199

is now what i can do let's create a new

263.12

one

263.6

so let's do hello

268

and let's do

271.36

a name and a time

276.08

and all this is going to do is just

277.6

going to say

280.84

hello

282.639

to somebody uh it

287.36

it is and then there's going to be some

289.759

time

290.96

let's just do that format

294

take those two note that that format we

297.28

talked about

298.4

back in uh one of the string one of the

301.199

operators

302.08

or maybe it was in strings anywhere

303.68

wherever it was i guess probably was

305.039

strings

305.84

can be very great way to quickly put

308.4

stuff together

310.08

so here we do name and time and

314.56

if i call hello

318.96

and it's just gonna be uh

322.84

bob and let's see morning

329.6

so that's gonna work fine so if i do

331.039

that you know hello bob this morning

333.199

but let's say that i don't always want i

335.44

don't necessarily always need

337.12

that so i can just default

341.68

it is let's say a

345.52

generic time of the day

351.36

well that was a lot of

354.96

so now this goes back to someone as we

357.52

get a positional now i can do the same

359.039

thing

360.56

and i don't have to call because i have

363.12

this default value

365.759

i can call that and we see here that it

367.52

just picks up whatever my default is

369.84

and i could do this with numbers so i

371.28

could do add

376

if i do like add two

379.28

and let's just say b equals 2.

385.039

so here

390.16

i could come in

396.16

and i could just make it three gonna get

399.52

those balanced right

402.24

but there we go and if i get that then i

405.12

get then it's just gonna add to that

406.8

so i could actually call that multiple

408.319

times

410.96

and i'm going to see it gets to oh i'm

413.44

sorry because it's not adding to the

414.72

prior one my mistake

417.44

so there we've got a

420.56

we've got a default and we could

422.08

actually do

425.36

let's say with this address

433.68

let's do

437.84

let's just call it location change this

440.24

around a little bit

441.759

and i want to do this

446.4

so now let's do location

453.039

and let's say um

456.72

i start with a zip code i need at least

459.28

a zip code

462.88

and state would be let's say not entered

469.199

city equals

472.4

entered

477.28

so now let's go back to my little zip

479.199

code so let's do a couple of these the

481.039

first one we're going to do

482.479

is just that and we know it we know this

485.039

one's going to work because we've just

486.16

seen it well

487.52

okay we don't totally know it and now

489.84

let's do

490.96

so for city that's going to be nowhere

494.319

there's a city so let's look at those

496

two and then let's see what happens

499.68

if we do the state

502.72

i'll make it ohio this time now notice

505.919

here

506.8

you are at not enter not entered so city

509.36

state zip

511.039

here nowhere comes in as city state

514.399

zip and state wasn't entered but notice

516.32

that because

518.959

of the ordering it's going to sit here

522.32

and say hey this

523.279

you know this doesn't work i'm i you

525.6

know it assumes

527.279

because we haven't said anything that

529.68

the state

530.399

is uh that the city is a value and not

533.12

state it does we're

534

back to assuming a

537.76

an order now let's look at this

541.92

so now if we name it though

546.48

then we can see where you know city was

548.48

blank and so

549.68

we can use named and default values

553.6

combined to have default values

556.64

throughout

557.279

our um our definition so i could

561.279

actually take this

565.04

and if i changed that

569.92

to my

573.76

oh yeah so if i change that then i can

577.279

actually call so now i've got a default

578.8

for every value and if i call address

580.56

we'll see that up here well

582

let's just see let's just do

590.16

so i can make sure we see it as an empty

592.399

values example and then we're going to

593.839

see

594.8

here somewhere in illinois one two three

597.519

four five

599.76

and i could just as easily say stay

602

equal so let me change that

608.32

and now we're going to see here i just

610.56

changed the state but i picked up the

611.92

other

612.8

default values so we can default

616.399

and use names to actually have a pretty

618.24

long list of values and and work with

620.88

those

622.079

another one we can do is we can have a

625.04

series

626.64

so now i can do let's see

629.839

i'm just going to call it sum and it's

632.64

gonna be

633.36

values and we put a star on this

638.8

oh actually i think it's this way my

641.12

mistake

644.16

and then uh let's for item

647.36

and values

651.2

let's see let's see result equals zero

656.24

result equals result plus item

664

return result

667.97

[Music]

669.519

and then we can do print

672.88

string sum and so we can do like a whole

676.48

bunch of values

679.68

and we're going to see here that it adds

681.839

those up as it walks through those

684.8

and deals with them now here this is

687.04

much like when we did

688

the generator before uh it's like

690.16

actually look at this

693.92

let's see what it says what it does when

695.68

i actually print out values

697.279

you should see that well there it

698.48

doesn't show it as a generator but it

699.76

does show it as

700.64

it's just an array and it's going to

703.76

walk through those values

705.2

or you can walk through those values and

707.6

do what you need to do with them

709.44

now another thing you can do is um

713.36

recursion let's see how do i want to

715.92

well actually so i could take this same

717.519

thing

721.6

and i can do sum too and this goes back

724.16

we're doing returns right now

730.959

but we could do something like that

732.48

where we can also use the yield

739.92

and i can actually see it as i

743.279

um let's see

746.639

the result equals that

750.32

and i'm just gonna do that so

753.519

now what i'm gonna do is i'm gonna see

754.639

each of the i'm gonna see it actually

755.92

build up

756.56

oh except for now i've gotta do um

760.959

i gotta change this a little bit because

762.32

now i've gotta do

764.72

result here equals

768.48

not one of those and then uh for

774

x in results

778.8

print x so now

782.24

let me see this so i can see so it first

784.24

comes through and it does one then does

785.92

one plus two

787.519

one plus two plus three one plus two so

789.519

you can see that build out

790.72

so we use yield just as easily

794.079

another thing we can do is we can do

797.519

recursion so the famous one for that

800.8

would be factorial

802.8

oh i have to define it factorial

808.079

and it's going to take a value of x

811.92

and then it's going to say and factorial

814.56

means so like

815.839

uh 2 fact well i'll say 3 factorial is

818.16

actually 3

819.279

times 2 times 1. so it's and 4

822.32

factorial would be 4

826.959

4 times 2 times 1. so that's what we're

828.959

gonna we're going to generate

831.04

so for a factorial what we want to do is

834.639

if you notice that 4 is actually the

836.56

same as 4 times

838.079

3 factorial and so 3 is the same as 2

842.48

times

843.12

factorial so what we want to do is we're

845.519

going to return

849.199

x times

854.639

x oh sorry factorial

860.56

of x minus one and i should spell return

864.48

right

867.199

and then if i do print

870.24

a string let's do five factor

873.92

of sorry factorial

877.36

five

882.32

oops uh what i forget to do

887.279

and let's see so i need to do

891.519

if i have to have this returned

895.519

uh value so let's say if x equals one

901.12

return one

904.56

else return that so my mistake

909.199

so then i'm going to return that let's

911.68

see how that looks

919.04

oops there we go

924.639

so at each point here

929.279

um yeah i don't think i'm going to add a

931.36

print to it so you see here at

933.759

there there's our thing

937.839

there's our final value now we could

940.16

also do

943.839

at each part of these we could do a

945.36

yield and we can see that again

957.04

um

961.519

so result equals

966.24

that and for example okay so we're gonna

969.04

do this

970.88

uh oh sorry

975.12

oh the problem with the yield here is

977.44

that what we need to do

978.48

is it needs to actually be

981.68

we have to convert that to an int

986

so this is going to be

992.16

into x because now we're screwing around

995.36

with a little bit

996

so it's going to change the type up

998

let's see

1002.16

oh i'm sorry okay yeah that's not going

1004.72

to work if we do a yield here then

1006.32

yield actually generates because it

1008

sends back a generator

1009.68

it's not going to build it right so i'd

1010.959

have to actually peel it off of the

1012.56

generator so

1013.44

my mistake but as you can see

1018.24

we can do a recursive call and we can

1021.04

just keep calling this

1022.32

you know and we could have two of them

1023.6

so we could have if we wanted we could

1025.52

have like a define

1026.48

we could have a that does something and

1030.079

it calls b

1034.16

and we could have a b that

1037.6

calls a to some extent

1043.439

and we can do so let's show that real

1045.679

quick got a minute

1049.12

so now if i call a it's going to blow up

1054.559

because we get a maximum recursion death

1056.08

because they just keep going back and

1057.2

forth

1059.919

and we'll see that

1063.6

yeah because it just shows us it just

1065.039

blew out the stack

1066.88

so i can do this or i could counter have

1068.96

a counter or something like that and of

1070.08

course

1070.559

this is a you know not a very useful

1072.4

example but

1074.32

you can also have things call each other

1076.559

so i think that's a good point for here

1078.799

we've

1079.2

we've gone through good deals stuff

1081.039

particularly focused on our parameters

1084.08

and note that there's many ways that you

1086.24

can send parameters around

1087.76

just as you can either send a single

1090.48

result

1091.039

or a a generator of results that comes

1094.559

back

1095.679

that'll do it for today so as always go

1098.16

out there

1098.88

have yourself a great day a great week

1101.679

and we will talk to you

1103.2

next time

1119.52

you