Detailed Notes
We start wrapping up with a review of some topics that need more discussion before taking the certification.
Useful Links: https://www.mygreatlearning.com/blog/understanding-mutable-and-immutable-in-python/ https://docs.python.org/3/tutorial/controlflow.html https://docs.python.org/3/whatsnew/3.0.html
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 to get ourselves to a python certification we've gotten through the bulk of items and i've started actually looking at the sample test that they have you'll find a link for it in the show notes and really it was just to sort of make sure that we've covered things in a way that will be helpful besides just understanding some of the topics but actually sort of understanding how they're going to be used and one of those well actually several things one that i'm going to sort focus on today is mostly because of a complex example and a couple little things related to it that i don't think we covered specifically enough has to do with for loops but then there are some other things that i think i may have underestimated uh or i could potentially be underestimating what people know so it's good to make sure you're clear on it the first of those is the idea of immutable versus immutable objects the key in python is to know that native types integers doubles times strings those are all immutable booleans they are immutable tuples and ranges also are immutable other things are mutable not immutable so a list a dictionary those things are immutable the difference between them the concept of mutable versus immutable is that either the memory stamp where something exists changes or it does not so if you were to for example let's just sort of say let's say that you've got in memory you've got this thing uh abcd that's a string okay and this is uh in memory slots and what is that 0 through 3 for those and then you and let's say you assign a variable let's say x equals that string then that means that x is a pointer to basically zero and then there's going to be something to let you know when it's done you know i don't want to get too complicated into it now if you assign x equals let's say x y z then what's going to happen in this case is that abcd is not going to ex going to change there now is going to be there's going to be let's say at 567 you're going to have xyz and memory slots five through seven right four five six seven yes and that memory slot four is probably going to be a string terminator it gets into pointer arithmetic a little bit that you may or may not have been exposed to if it is mutable that would mean that instead what would happen is that abcd would be essentially overwritten as opposed to here where it's saying it's a string so that string exists and this variable points to it here i'm going to create another string somewhere in memory and then point the variable to it i'm not going to override the memory for the string and that works for any of the native types and tuples and ranges that's really what you need to know because there are like a couple of questions related to that and i just i know i have not really haven't really covered that at all so i want to make sure that we brought that up similarly i want to make sure you understand one of the things that i saw that sparked this idea is understanding powers exponent exponents so x to the y and that one let me move this and let me do i think it's easier to show some examples so i'm going to take a bunch of these these are all going to be strings so i'm going to do it this way i want to show you something here so if i do 0 to the 0 power 0 to the 1 power and 0 to the second power and now let me take 3 to the 0 power 10 to the 0 power and this number to the 0 power so let's look at those first and we'll see zero to the zero is one as is three to zero ten to the zero or this big number zero anything to the zero power or z power if you say it that way is equal to one that includes zero other than that other than that zero to any power is 0. and so if i even do 0 to the negative 74th power i think it'll read that right hopefully it will no uh i think i need to do it that way oh it says it cannot be raised to a negative power so if you go to zero to the negative power is going to give you an exception which you may have to run into let's go any other number uh oops so i can't do that but i can now do this and it's just going to give you it so 0 and let's do that 0 to a negative is an exception we'll say it cannot calculate but it can anything else i think that's important because they do have some questions related to those and we've not really you know just sort of assumed that you understood exponents and if you're a little fuzzy with them especially these little rules it's good to know now another thing we haven't talked about is we've done prints p-r-i-n-t-s we have done print commands but there are two things we've not talked about we have not talked about the sep nor have we talked about the end and let's just do a so normally when we print we print and it just it gives us a new line so see here that we just get a a new line here um i don't want to do that yeah i'll do that for now let's do this the ends first let's throw it through this so what i'm going to do here so now if i do the one two three with an end and then end on an a let's look at those so here um i do this gets hard to read example two example one and let's do of this and we're gonna put an extra little one here just to be sure okay so now let's look at it so if you look here example one it prints but notice that it does not do now instead so instead of the end being an end line which we would have here so if we get rid of this guy and run it we're gonna see what looks more familiar see we see the uh one two three because it's doing you can do commas uh you can do multiple items within your print so it's gonna do those and it just prints them all in one line and it gives us a line feed if i say don't do that whoops if i say don't do that and do an end now that one two three notice it does have a little space between those so it shows us that that's a set essentially that's different items and the end now is only on a you know an empty string so what the end thing is an empty string it's not a return so it doesn't give us that line feed and then it dives right into example two notice here and this is just to be clear on it so at the end of the line here instead of adding a blank i'm going to add an a so here it does 1a to a 3a and it still does those little spaces to split the list essentially and then at the end of line it does an a so i could also do like i can do end of line and it'll come back with an eol there you go so end tells me what i want to put at the end of my line if i want to change something around a little bit this is very important if you're doing like comma separated lists or something like that and so for example let's take the same thing and i think i've got yeah so now let's do this i'm just going to print so i get space and let's look at these now let's look at the set now set matters only if i've got a series so if i look at this first one that 1 2 3 that's where it's at that separator is a empty string so now instead of the space which is the default it crams them together here i use a sep of hashtag and you can see here where it puts a hashtag at the end of each of those and so that would commonly be where like let's say i wanted to do a pipe delimited thing that would build out my list of items and i now get a pipe delimited series of items however uh if i do i have a list floating around here somewhere i don't so let's do one more thing and let's do uh my list equals one comma two comma three and now let's do my list with a separator of a pipe and notice that it doesn't separate because the list is printing out the whole list it's not doing anything special with it therefore we're getting this uh this is what we're getting because we're getting the list in the uh sort of the string representation of the list so we would have to and uh similarly just to be clear if i do 4x in my list and then let's just do let's do here let's just do print x comma separator equals this thing then i'm still not going to get what i want so it prints each of these because the separator doesn't print it's only between items so what i would have to do is i would have to change it to be this and then i'm going to get my one two three but then i've got that extra one at the end so you know you've got a couple different ways to get to it but particularly doing a series of things you can play around with separators we didn't see it very often but it does show up in the test apparently and the ends is good to know and we haven't played around with that so i want to like take a look at those two uh let's see so this i wanted to look specifically let's see there's my list okay so let's move this down oh let's do this first uh one thing we didn't talk about before there we go i think we specifically spent enough time in what you do if you come back if you grab 4x in sample where sample is a dictionary and that is that it is a key so if we print and it's home one two three the three items are home and an address office and an address bill in an address if we print the sample itself uh it is here oh yeah because i don't have the line so it just gives us a nice string representation of it that we see here if we do samples dot keys then it gives us a collection of dictionary keys with those three keys if we do val if we do sample.values and you guys will have seen this before we did the dictionary example we get the three values if we do for x and sample it's going to go through the key so you see the keys here home office and bill well x is home office and bill so if we wanted to print the items from here we would actually do a sample sub x will give us those three addresses so let's see you go there we get our three addresses so reminder that if you do a 4x in a dictionary it's not giving you the objects the pairs it's giving you the keys now if you want to do so if i do x comma y and let's do k comma value and sample then i can print k is plus k and v is this v i don't think we've played with this one too much too many values unpack in this case because it's only giving me keys but if i were to do uh let's do because it's not what i want is not key value pairs so if instead i do let's do sample 2. and here so i can come in here and i can see that k is what i'm assigning the first value home office bill v is what i'm assigning the second value in each of those three addresses and so i could make that very complicated i could have three or four items and i can assign all of them out i can also do this with classes so there are a lot of things i can do where i can be i can break something out basically within a loop and assign out some values all at the same time we don't really cover that so i want to make sure that we did touch into that one and then another one i want to make sure i get into is um well first off let me do this one because this is a quick one so if i look at sample two if i do print sample two but i do print let's do sample three equals sample two and then i do a sample two now let me see sorry if i did delete sample two and we'll leave it there so first let's just do this so i'm going to delete sample 2 item i'm going to do negative 1 and then i'm going to print sample 2 again let's just do it that way that's going to be the easiest way to show this and so what i get here is i'm not able to do it but if i do 0 to negative 1 [Music] oh shoot i want it that's not how i want to do it anyways let me do this so if i do 0 to negative 1 that's 1 i want to see because you will see that so um let's see so i come through vs answer so i come here here's the whole thing but if i go zero to negative one it's going to build out it gets rid of the last item if i go zero to negative two see it starts it it's going to end up and it deletes yeah so it deletes the first one if you notice here if i do negative one actually let me do this this way it's gonna be easier to compare these two so i wanna compare these two so if i go zero to negative one blah i'll blah sample two let's do this sample three equals sample two well and then i'm going to try through all that and then i'm going to do 2 equals sample 3 so i'm going to rebuild that all right so now look and note that i lost it okay so print sample two i do that if i delete that and present sample two then i get this guy that's the only one that's left and if i come in here go zero to negative two so let me do this because it's gonna be easier let's just do this sorry okay here we go so sample two if i go from zero to negative one that's where i start it gets rid of this guy and this guy i'm sorry this this guy and this guy if the range goes from zero to negative two then he gets rid of just this guy because now he hasn't run around enough but if i started with zero negative two oh that is what i did okay is that uh note that i'm losing this guy in each case if i go zero to negative four in this case i end up getting rid of nothing because i can't go to that it's an invalid range if i do negative well it pulls the last item off if i do negative two it's going to pull the middle item off so baker street disappears so if i go negative on it then it's going to go it starts up here and then it counts backwards so you can get some sort of funky kind of stuff that goes into it so remember your negatives start essentially your negatives start at the right versus the left now they do have i want to throw this this one out there just to sort of walk through something they have this very complex question which is this guy it basically says how many stars are printed the key is this thing is we've got this for c uh c for c in range r which is right a list for r in range three so if we just do the four r in range 3 we know that that's going to be 0 1 2 right so if we go look we'll see that that is 0 1 and 2. now this is where it becomes complicated so it's going to create this set where c is in range r but r is 0 1 and 2. and so the set is going to be in the first one the range 0 we don't do anything at all so the set is an empty set when you do range one oh let me do this when you do range one then it's gonna be uh zero to one and so you're gonna come here you're gonna get once and then it's gonna be zero is the only thing that's gonna be in there two the items in the range are zero and one so that's your sets and then it comes out and says okay for x and list and list is this thing so the list items are empty set set with zero and a set with zero one and so it's going to come in an empty list and it's going to say for y and x so there's nothing there it's an empty set so nothing happens the next time through it comes for 0 if 0 is less than 2 prints star yes it does the next time it comes through and it's going to go 0 zero is less than two print star and then one one is less than two print star and so in the end it's going to print three stars so it's very very complicated looking but not terribly so oh and that reminds me one other thing because of order operations um another thing to remember is that if you do exponents those are not left to right so if i go 2 to the 3 to the 2 or let's say to the 4. i don't do 2 to the 3 which is 8 and then 8 to the 4th which is what is that 256. instead i do 3 to the 4th which is 81 and then 2 to the 81 and that's going to be a huge number so if i do this perform that x let's just do this here and so it's going to be a huge number not 256. yeah so it's a huge number which is not the same as if i do this oh i'm sorry it's not the same as if i do this let me change the order of operation with 4096. so you go from the outside first so the right to the left when you're doing exponents as far as order of operations they'll do for this first part i only this is only actually covering the first half of the test as i went through it and wanted to bring had some notes on some additional things i wanted to bring up i apologize for maybe not being as clear or not getting into some of these before but i'm going to continue with this i'm going to go take a look at the second half of the test and see what kind of notes i have that come out of that as we're getting pretty darn close we are almost there so you can see all of this out in the repository github repository where we have links in the show notes i'll get that caught up and uh 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 to get ourselves to a python
certification
we've gotten through
the bulk of items and i've started
actually looking at the sample test that
they have
you'll find a link for it in the show
notes
and really it was just to
sort of make sure that we've covered
things
in a way that will be helpful
besides just
understanding some of the topics but
actually sort of understanding how
they're going to be used
and one of those
well actually several things
one that i'm going to sort focus on
today is mostly because of a complex
example and a couple little things
related to it that i don't think we
covered
specifically enough
has to do with for loops
but then there are some other things
that
i think i may have underestimated
uh or i could potentially be
underestimating what people know
so
it's good to make sure you're clear on
it
the first of those
is the idea of immutable versus
immutable
objects
the key in python is to know that
native types
integers doubles times strings those are
all immutable booleans they are
immutable
tuples and ranges also are immutable
other things are
mutable not immutable so
a list a dictionary those things are
immutable
the difference between them
the concept of mutable versus immutable
is that
either
the memory stamp where something exists
changes or it does not
so if you were to
for example
let's just sort of say let's say that
you've got
in memory
you've got this thing uh abcd that's a
string
okay
and this is uh in memory
slots
and what is that 0 through 3 for those
and then you and let's say you assign a
variable let's say x equals
that string
then that means that x is a pointer
to
basically zero
and then there's going to be something
to let you know when it's done you know
i don't want to get too complicated into
it
now
if you assign x equals
let's say x y z
then what's going to happen in this case
is that
abcd
is not going to ex going to change
there now is going to be
there's going to be let's say at 567
you're going to have xyz
and memory slots
five through seven
right four five six seven yes
and that memory slot four is probably
going to be a string terminator
it gets into pointer arithmetic a little
bit that
you may or may not have been exposed to
if it is
mutable that would mean that instead
what would happen is
that abcd would be essentially
overwritten
as opposed to here where it's
saying it's a string so that string
exists and
this variable points to it
here
i'm going to create another string
somewhere in memory and then point the
variable to it i'm not going to override
the memory for the string
and that works for any of the native
types and tuples and ranges
that's really what you need to know
because there are like a couple of
questions related to that
and i just i know i have not really
haven't really covered that at all so i
want to make sure that we brought that
up
similarly
i want to make sure you understand one
of the things that i saw that
sparked this idea is
understanding
powers
exponent exponents
so x to the y
and that one
let me move this
and let me do
i think it's easier to show some
examples
so
i'm going to take a bunch of these
these are all going to be strings so i'm
going to do it this way
i want to show you something here so if
i do 0
to the 0 power
0 to the 1 power
and 0 to the second power
and now let me take
3 to the 0 power
10 to the 0 power
and this number
to the 0 power
so let's look at those first
and we'll see
zero to the zero
is one as is three to zero ten to the
zero or this big number zero
anything to the zero power
or z power if you say it that way
is equal to
one
that includes
zero
other than that
other than
that
zero
to any power
is 0.
and so if i even do
0 to the
negative 74th power
i think it'll read that right hopefully
it will no uh
i think i need to do it that way
oh it says it cannot be raised to a
negative power so if you go to zero to
the negative power
is going to give you
an exception
which you may have to run into
let's go any other number
uh oops
so i can't do that
but i can now
do this and it's just going to give you
it so 0
and let's do that
0 to a negative
is an exception we'll say
it cannot calculate
but it can anything else
i think that's important because they do
have some questions related to those
and we've not really you know just sort
of assumed that you understood exponents
and if you're a little fuzzy with them
especially
these little rules
it's good to know
now
another thing we haven't talked about is
we've done prints
p-r-i-n-t-s
we have done print commands
but there are two things we've not
talked about
we have not talked about the sep
nor have we talked about
the end
and let's just do a
so normally when we print
we print and it just it gives us a new
line
so see here that
we just get a
a new line here
um
i don't want to do that
yeah i'll do that for now
let's do this the ends first
let's throw it through this so what i'm
going to do here so now if i do the one
two three with an end and then end on an
a
let's look at those
so
here um
i do
this gets hard to read example two
example one
and let's do
of this
and we're gonna put an extra little one
here
just to be sure
okay so now let's look at it
so if you look here example one
it prints but notice that it does not do
now instead
so instead of the end
being an end line which we would have
here
so if we get rid of this guy
and run it we're gonna see what looks
more familiar see we see the uh one two
three because it's doing
you can do commas uh you can do multiple
items within your print so it's gonna do
those and it just prints them all in one
line
and it gives us a line feed
if i say don't do that whoops
if i say don't do that
and do an end
now
that one two three
notice
it does have a little space between
those so it shows us that that's a set
essentially that's different items
and
the end now is only
on a you know an empty string so what
the end thing is
an empty string it's not
a return
so
it doesn't give us that line feed and
then it dives right into
example two
notice here
and this is just to be clear on it so
at the end of the line here instead of
adding a blank i'm going to add an a
so here it does 1a to a 3a and it still
does those little spaces to split
the
list essentially
and then at the end of line it does an a
so i could also do like i can do end of
line
and it'll come back with an eol there
you go
so end tells me what i want to put at
the end of my line if i want to change
something around a little bit this is
very important if you're doing like
comma separated lists or something like
that
and so for example let's take the same
thing
and i think i've got yeah
so now
let's do this
i'm just going to print
so i get space
and let's look at these now let's look
at the set
now set matters only if i've got a
series so if i look at this first one
that 1 2 3
that's where it's at that separator is a
empty string so now instead of the space
which is the default
it crams them together here i use a sep
of hashtag
and you can see here where it puts a
hashtag at the end of each of those
and so that would commonly be where like
let's say i wanted to do a pipe
delimited
thing that would build out my list of
items
and i now get a pipe delimited
series of items however uh if i do
i have a list floating around here
somewhere
i don't
so let's do one more thing and let's do
uh my list equals
one comma two comma three
and now let's do
my list
with a separator of a pipe
and notice
that it doesn't separate because the
list is printing out the whole list
it's not doing anything special with it
therefore
we're getting this
uh this is what we're getting
because we're getting the list in the uh
sort of the string representation of the
list
so we would have to and
uh
similarly just to be clear if i do 4x
in my list
and then let's just do
let's do here let's just do print
x comma separator equals this thing
then i'm still not going to get what i
want
so it prints each of these because the
separator doesn't print it's only
between items so what i would have to do
is i would have to change it to be this
and then
i'm going to get my one two three but
then i've got that extra one at the end
so you know you've got a couple
different ways to get to it but
particularly doing a series of things
you can play around with separators we
didn't see it very often but it does
show up in the test
apparently and the ends is good to know
and we haven't played around with that
so i want to like take a look at those
two
uh let's see so this
i wanted to look specifically
let's see there's my list okay so let's
move this down oh let's do this first
uh one thing we didn't talk about before
there we go
i think we specifically spent enough
time in
what you do if you come back if you grab
4x in sample where sample is a
dictionary
and that is that it is a key
so if we print
and it's home one two three the three
items are home and an address office and
an address bill in an address if we
print the sample itself
uh
it is
here oh yeah because i don't have the
line so it just gives us a nice string
representation of it that we see here
if we do
samples dot keys
then it gives us
a collection of dictionary keys with
those three
keys if we do val if we do sample.values
and you guys will have seen this before
we did the dictionary example
we get the three values
if we do
for x and sample it's going to go
through the key so you see the keys here
home office and bill
well x is home office and bill
so if we wanted to print the items from
here we would actually do
a sample sub x
will give us those three addresses
so let's see you go there we get our
three addresses so reminder that if you
do a 4x in a dictionary it's not giving
you the objects the pairs it's giving
you the keys
now if you want to do
so if i do x comma y and let's do
k comma value
and sample
then i can print
k
is
plus k
and v is
this v
i don't think we've played with this one
too much
too many values unpack in this case
because
it's only giving me keys
but if i were to do
uh
let's do
because it's not what i want is not key
value pairs so if instead i do let's do
sample 2.
and here
so i can come in here and i can see that
k
is what i'm assigning the first value
home office bill v is what i'm assigning
the second value in each of those three
addresses
and so i could make that very
complicated i could have three or four
items and i can assign all of them out
i can also do this with classes so there
are a lot of things i can do where i can
be i can break something out
basically within a loop and assign out
some values all at the same time we
don't really cover that so i want to
make sure that we did touch into that
one
and then another one i want to make sure
i get into
is um
well first off let me do this one
because this is a quick one so if i look
at sample two
if i do
print sample two
but i do
let's do sample three
equals sample two
and then i do a sample
two
now let me see
sorry
if i did delete sample two
and we'll leave it there so first let's
just do this
so i'm going to delete sample 2 item i'm
going to do negative 1
and then i'm going to print sample 2
again
let's just do it that way that's going
to be the easiest way to show this
and so what i get here
is
i'm not able to do it but if i do 0 to
negative 1
[Music]
oh shoot i want it that's not how i want
to do it anyways let me do this so if i
do 0 to negative 1 that's 1 i want to
see
because you will see that so
um
let's see so i come through vs answer so
i come here here's the whole thing
but if i go zero to negative one it's
going to build out it gets rid of the
last item if i go zero to negative two
see it starts it it's going to end up
and it deletes
yeah so it deletes the first one
if you notice here
if i do negative one
actually let me do this this way it's
gonna be easier to compare these two so
i wanna compare these two so if i go
zero to negative one blah
i'll blah
sample two
let's do this
sample three equals
sample two well
and then i'm going to try through all
that and then i'm going to do
2 equals sample 3 so i'm going to
rebuild that
all right so now look
and note
that
i lost it okay
so print sample two i do that if i
delete that and present sample two
then i get
this guy that's the only one that's left
and if i come in here
go zero to negative two so let me do
this
because it's gonna be easier
let's just do this
sorry
okay here we go
so sample two if i go from zero to
negative one
that's where i start
it gets rid of
this guy
and this guy
i'm sorry this this guy and this guy
if the range goes from zero to negative
two
then he gets rid of just this guy
because now he hasn't run around enough
but if i started with zero negative two
oh that is what i did okay
is that uh note that i'm losing
this guy
in each case if i go zero to negative
four
in this case
i end up getting rid of nothing
because i can't go to that it's an
invalid range
if i do negative
well
it pulls the last item off if i do
negative two
it's going to pull the middle item off
so baker street disappears
so if i go negative on it then
it's going to go it starts up here and
then it counts backwards so you can get
some sort of funky kind of stuff that
goes into it
so remember your negatives start
essentially your negatives start at the
right versus the left
now they do have i want to throw this
this one out there just to sort of walk
through something
they have this very complex question
which is this guy it basically says how
many stars are printed
the key
is this thing
is we've got this for c uh c for c in
range r which is
right a list for r in range
three so
if we just do
the four r in range 3 we know that
that's going to be 0 1 2 right so if we
go look we'll see that that is 0 1 and
2.
now this is where it becomes complicated
so
it's going to create this set
where
c is in range
r but r is 0 1 and 2.
and so the set
is going to be in the first one the
range 0 we don't do anything at all so
the set is an empty set
when you do range one
oh let me do this
when you do range one
then it's gonna be uh
zero to one
and so
you're gonna come here you're gonna get
once and then it's gonna be zero is the
only thing that's gonna be in there
two the items in the range are zero and
one so that's your sets
and then it comes out and says okay for
x and list
and list is this thing so the list items
are empty set
set with zero and a set with zero one
and so it's going to come in an empty
list and it's going to say for y and x
so there's nothing there it's an empty
set so nothing happens
the next time through it comes for 0
if 0 is less than 2 prints star yes it
does the next time it comes through and
it's going to go 0 zero is less than two
print star
and then one one is less than two print
star and so in the end it's going to
print three stars
so it's very very complicated
looking
but not terribly so
oh and that reminds me one other thing
because of order operations
um another thing to remember is that if
you do
exponents
those are not left to right so if i go 2
to the 3 to the 2
or let's say to the 4.
i don't do 2 to the 3 which is 8 and
then 8 to the 4th which is what is that
256.
instead i do 3 to the 4th which is 81
and then 2 to the 81 and that's going to
be a huge number so if i do this
perform that x let's just do this here
and so it's going to be a huge number
not 256. yeah so it's a huge number
which is not the same
as if i do this
oh i'm sorry it's not the same as if i
do this let me change the
order of operation
with 4096.
so you go from the outside first so the
right to the left when you're doing
exponents as far as order of operations
they'll do for this first part i only
this is only actually covering the first
half of the test as i went through it
and wanted to bring had some notes on
some additional things i wanted to bring
up
i apologize for maybe not being as clear
or not getting into some of these before
but i'm going to continue with this i'm
going to go take a look at the second
half of the test and see what kind of
notes i have that come out of that as
we're getting pretty darn close we are
almost there so
you can see all of this out in the
repository github repository where we
have links in the show notes
i'll get that caught up and uh go out
there have yourself a great day a great
week and we will talk to you
next time
you