Detailed Notes
The certification series covers if, if-else,if-elif, and if-elif-else in this session.
Helpful links: https://www.tutorialsteacher.com/python/python-if-elif
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 certifications series in python and today we're going to get into ifs and else and else ifs and all kinds of other good stuff we're actually going to start sort of working a little bit more applications as opposed to just really quick sort of script kind of things that we've done to this point and this one we're gonna actually is always it will be out in the github repository when we're done i have jumped ahead just a little bit uh so what we're looking at is we're going to cover if we're going to do if else we're going to use if alif if lfs else so we've got this little example and let me do this so i have a little bit of a little bit better formatting maybe we've done so far so what i'm going to do and this is some of these are things we've already seen so we're going to start building these into and reusing them as we go through some of these uh applets basically that we'll be we'll be building out as we go so this one we're going to use input that we talked about previously this will allow us to get input from the console and we're just going to ask the user to provide an hour of the day we're going to suggest it's between 1 and 24 and we're not going to put any validation or something that may come later but right now we're going to stick with this so they're going to enter a number between 1 and 24. actually we'll even make it between 0 and 24. it doesn't really matter and this is some of the things that we can do we can show a little bit of logic around building applications as well so first we're going to do is we're going to say there's this greeting and it will be good evening and uh oops i need to do this and uh what we do well and i just move this but uh since we're just gonna print out the greeting and we're gonna have little wine feeds before it now because of the if so if we don't do anything uh let me switch over here and we're actually let me do this so we can see a little better so okay so provide the other day let me do uh so this will be six o'clock in the evening it's going to say good evening if i do the same thing and i six o'clock in the morning it still says good evening so obviously a perfect example of where we can use some logic so the first thing we're going to do is we're going to say if and then we have to take the value that we get we have to convert it to an inch because remember it doesn't like comparing strings and integers and so we're just going to say if it's less than or equal to 12 so if it's less than noon we're going to say good morning so notice that if it's pretty straightforward there's no then or anything like that it's just an if statement right here and then you're going to have whatever it's checking and then a colon and then underneath it is going to be indented notice here i come back out there's no end if or anything like that this thing is because it is left you know it's at the same level as the if it assumes that it's going to be um outside of the f so the if is already completed if i were to just move this in so then that means that i'm not going to get the greeting if unless the value is less than 12. so we'll look at that real quick so here if i go back and i do that value of 18 it just goes to application in it it does not print but if i do six it does print good morning so i need to make sure that this part is going to be outside of that if statement so we've got an if here now let me do an if else so let's say um let's do this if it's less than we'll start with evening so we'll see if it's less than 10 it's gonna be actually let's just say well this one it doesn't really matter because it's never going to say this so let's just do this let's say good afternoon and if it's less than 10 then it's going to be actually we could do ah we'll do it this way it's gonna say good morning otherwise it's gonna say good evening and notice now that if we do in the morning it says good morning if we do noon it's going to say good evening if we do 10 o'clock at night which is what 22 then it still says good evening we never see good day uh come back here so we really want is let's say if we're going to do afternoon morning and evening then we're going to do an lf so here we're going to say if it's let's get rid of this if it is less than or equal to 10 we're going to say good morning else if so if it's not less than equal to 10 if it is greater than equal to let's say 17 so 5 o'clock or later it's going to say good evening otherwise it's going to say good afternoon i'll move this around a little bit so now what we'll see is if we do it early in the morning at 5 a.m it says good morning if we do it at 10 a.m it's done the same morning because i moved that to 11 but if i do uh 11 oh sorry uh if we do 11 and it says uh good morning because it was equal to but if i do noon now it's going to say good afternoon even though it's noon and then if i do uh the evening let me do seven o'clock at night now it says good evening so an l if much like an if underneath it it's going to be out notice this out at the same indentation level as if it also like if has its um you know it has whatever it's checking has its its validation the if part of it that logic and then of course you have things underneath it here so this because this backs out here that's the end of the if whenever i get something back to else level or lift level then it's going to be back at that level so i could say um let's just do this we're going to do error and so now we're going to add an else to the end and we're going to take that and this will be good afternoon or we'll say good day so if we do an if else uh if lf else then here again notice we're we're indenting as needed and then we're out denning uh to sort of wrap the uh to complete the thought for the ifs and the lefts and elses in this case what we're going to do is we're going to say if it's less than or equal to um let's say 10 okay so it's less than that or equal to it's going to be good morning else if it's greater than or equal to 17 it's going to be good evening and then if there's anything else we're just going to say good day so here we're actually overwriting greeting no matter what so if for some reason this bails out completely we would get an error so if we go back to and if we do it at say 1 a.m it's going to say good morning if we do it at noon it says good day if we do it at six o'clock it says good evening and so now we've put together we can put together something moderately complicated here now we actually made this one pretty simple because these are um mutually exclusive you can't be less than equal to 10 and greater than or equal to 17. but we can do this so we can say if it's less than let's say if it's less than 10 it's going to say good morning else so let's say if it's less than 17 it's going to say good day otherwise it's going to say good evening and so now we can actually do multiple lifts so here we can say if it's equal to 12. uh let's see we're gonna say it is noon so no so here good day less than 17 it's not going to get there because it's going to actually be hit here and then essentially bail out so let's take a look at that so we've got 10 12 and let's play with these a little bit so the first thing we do is we come in if it's one in the morning it says good morning easy enough if we say 10 then it's good day because uh it's not less than 10. it's actually equal to 10. if we do noon it says it's noon because it comes through here and it says if it's less than 10 good morning otherwise if it's equal to 12 it's noon otherwise if it's less than 17 we're gonna say good day so actually we can go see where if we use 11 it still says good day because here it's greater than 10 it's not equal to 12 but it's less than 17. this is where order can be very important if i were to switch this this lift then what's going to happen is it will always handle the if less than 10 then if less than 17 the 12 never gets here because the 12 was already covered by the 17 by the less than 17. so we would actually never see it in that case so it's important to realize that these end the elifs end if it's if it's true so it does its thing and then it gets out and so then going back to this if we do at the end of the day then it's just going to say good evening so it's important to note um let's do this note that elif ends series if true and note we can have multiple alif's lf sections notice here if i do else bad day if i do two elses it's going to tell me here that's an invalid syntax here on the 19. so it comes in and says you can't have that you also cannot do an else among the elifs see because it's going to blow up on slide 17 which it says oh okay you were done oh wait a minute because basically this if the once you get to the else you're going to be tying up you're going to ending this if block so that means this lift doesn't have an if to tie itself at this point and those are the kind of things you're going to run into in particular because of some of the other logical constraints that we're going to talk about at some point as we're going through this you need you almost need the lifts but you also need to be able to to keep in line the logic that you're actually walking through is realizing this is a if step one else step two l step three and those or say check one check two check three where each of those checks in this case only is hit if the prior one uh fails once it's true so here if it's good morning it didn't even check any of this stuff which from a speed point of view is great that means you could have a really long list and the only time that the whole list gets walked is when you get down to the last value so if i did let's say if it's less than one then if that's less than two if it's less than three if you know all the way down so i could have a whole bunch of lifs then the only one that would execute all of them in this case would be when i get to 24 basically so if i get less than 24 or just you know at some point at the end i may have 20 you know 20 something if lift combinations of you know series of commands here but i'm normally not going to run through them it's only going to be when i have to run all the way through it so those are the the key points of that um i think what i want to do with this one and i do want to touch on uh some of the things we're going to be doing next now we've talked about because we're going to be sort of building an application out of this and i don't want to get too deep in but i do want to talk about um you know highlight a couple of things now one we've talked about this formula make sure we understand that we can build strings using our using our pluses or we actually can do it with our asterisk as well because remember we could do multiples of those so if i did print uh a times five then it's going to print out oh i gotta do it with parentheses though it's gonna print out five a's so whatever number i do we can see where it's just my five a's so we can build strings that way we also have our formats so i could say so the greeting could be good like that and then i could have the second part let's say the second part is uh oh let's just call it day text and i'll use a little thing there so day text could be and we'll start with we'll just say it's blank and then here a text equals morning it's not going to be quite as clean because the way we're doing it but just to show that we can build strings out of this in a formatting but we can also do them uh through our pluses and such and so here this would be uh let's do greeting dot format and then give it the day text and i should spell day text right instead of day next i can get rid of my multiples of fives let's just comment that out and so now there we go so i get my good morning or i get my good evening or whatever it is so we've been able to walk through these and we're gonna start building out and working on our strings that'll be part of our ifs so note that as we're going through this although we have i'm trying to build this out so we sort of cover specific items throughout the certification so you could potentially jump in at any given point uh it may be easier to follow this you know in order because there's going to be things that we may sort of gloss over that and we were actually covered in a prior episode of prior session so we're going to work start working into the the logics and loops and things like that so this is we're going to continue working on our little application next time around so i'm gonna i'll leave it like that for now i think that's a good point to stop just because the next bun chunk will be a sizable enough chunk we're gonna get into some next one actually probably do uh vectors and indexing and slicing and length and so we're gonna we're gonna parse our uh we're gonna make this a little more than telling them to do a number and we're gonna actually be able to do something like you know uh 12 35 or something like that i'm even doing am or pm with it so that'll be coming next i just want to wrap this one up again this is stuff that if it's new to you it will probably become more comfortable as we go on because we're going to just keep we're going to see a lot of repetition we've already seen ifs and else elsa's before we're going to see more of this and a lot of it when you as you saw with some of the stuff i was playing around with the key is always going to be understand where your tabs are basically what your your format is where your index indents are or not because python is very indent sensitive that being said i'll let you get 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]
hello and welcome back
we are continuing our certifications
series in python
and today we're going to get into ifs
and else and else ifs and
all kinds of other good stuff we're
actually going to start
sort of working a little bit more
applications as opposed to just really
quick
sort of script kind of things that we've
done to this point and
this one we're gonna actually is always
it will be out in the github repository
when we're done
i have jumped ahead just a little bit
uh so what we're looking at is we're
going to cover if we're going to do if
else we're going to use if alif
if lfs else so we've got this little
example and let me do this
so i have a little bit of a little bit
better formatting maybe we've done so
far
so what i'm going to do and this is some
of these are things we've already seen
so we're going to start building these
into
and reusing them as we go through some
of these uh applets
basically that we'll be we'll be
building out as we go
so this one we're going to use input
that we talked about previously
this will allow us to get input from the
console
and we're just going to ask the user to
provide an hour of the day we're going
to suggest it's between 1
and 24 and we're not going to put any
validation or something that may come
later
but right now we're going to stick with
this so they're going to enter a number
between 1
and 24. actually we'll even make it
between 0 and 24. it doesn't really
matter
and this is some of the things that we
can do
we can show a little bit of logic around
building applications as well so first
we're going to do is we're going to say
there's this greeting and it will be
good evening
and uh oops i need to do this
and uh what we do well and i just move
this but
uh since we're just gonna print out the
greeting and we're gonna have little
wine feeds before it
now because of the if so if we don't do
anything
uh let me switch over here and we're
actually let me do this so we can see a
little better
so okay so provide the other day let me
do uh
so this will be six o'clock in the
evening it's going to say good evening
if i do the same thing and i six o'clock
in the morning it still says good
evening
so obviously a perfect example
of where we can use some logic so the
first thing we're going to do is we're
going to say
if and then we have to take the value
that we get
we have to convert it to an inch because
remember it doesn't like comparing
strings
and integers and so we're just going to
say if it's less than or equal to 12 so
if it's less than noon
we're going to say good morning so
notice that if it's pretty
straightforward there's no then or
anything like that it's just
an if statement right here
and then you're going to have whatever
it's checking and then a colon
and then underneath it is going to be
indented
notice here i come back out there's no
end if or anything like that
this thing is because it is
left you know it's at the same level as
the if it assumes that it's going to be
um outside of the f so the if is already
completed if i were to just move this in
so then that means that i'm not going to
get the greeting if
unless the value is less than 12. so
we'll look at that real quick
so here if i go back and i do that value
of 18
it just goes to application in it it
does not print but if i do six
it does print good morning so
i need to make sure that this part is
going to be outside of that if statement
so we've got an if here now let me do an
if
else so let's say um
let's do this if it's less than
we'll start with evening so we'll see if
it's less than 10
it's gonna be
actually let's just say well this one it
doesn't really matter because it's never
going to say this so let's just do this
let's say good afternoon
and if it's less than 10 then
it's going to be actually we could do ah
we'll do it this way it's gonna say good
morning
otherwise
it's gonna say good evening
and notice now
that if we do in the morning it says
good morning
if we do noon it's going to say good
evening
if we do 10 o'clock at night
which is what 22 then it still says good
evening we never see good day
uh come back here
so we really want is let's say if we're
going to do afternoon morning and
evening
then we're going to do an lf
so here we're going to say if it's
let's get rid of this if it is less than
or equal to 10 we're going to say good
morning
else if so if it's not less than equal
to 10 if it is
greater than equal to let's say
17 so 5 o'clock or later it's going to
say
good evening otherwise it's going to say
good afternoon
i'll move this around a little bit so
now
what we'll see is if we do it early in
the morning at 5 a.m
it says good morning if we do it at 10
a.m
it's done the same morning because i
moved that to 11 but if i do
uh 11 oh sorry
uh if we do 11 and it says
uh good morning because it was equal to
but if i do noon
now it's going to say good afternoon
even though it's noon and then if i do
uh the evening let me do seven o'clock
at night
now it says good evening
so an l if much like
an if underneath it
it's going to be out notice this out at
the same indentation level as
if it also like if has
its um you know it has whatever it's
checking has its
its validation the if part of it that
logic
and then of course you have things
underneath it here so this
because this backs out here that's the
end of the if
whenever i get something back to else
level or lift level
then it's going to be back at that level
so i could say
um let's just do this
we're going to do error
and so now we're going to add an else to
the end
and we're going to take that and this
will be
good afternoon or we'll say good day
so if we do an if else uh if
lf else then here
again notice we're we're indenting as
needed and then we're out denning
uh to sort of wrap the uh to complete
the thought
for the ifs and the lefts and elses in
this case what we're going to do is
we're going to say
if it's less than or equal to
um let's say 10 okay so it's less than
that or equal to it's going to be good
morning
else if it's greater than or equal to 17
it's going to be good evening and then
if there's anything else we're just
going to say good day
so here we're actually overwriting
greeting no matter what so if for some
reason
this bails out completely we would get
an error
so if we go back to and if we do it at
say 1 a.m
it's going to say good morning if we do
it at noon
it says good day if we do it at
six o'clock it says good evening
and so now we've put together we can put
together something moderately
complicated here now we actually made
this one pretty simple because these are
um mutually exclusive you can't be less
than equal to 10
and greater than or equal to 17. but we
can do
this so we can say if it's less than
let's say if it's less than 10 it's
going to say good morning
else
so let's say if it's less than 17
it's going to say good day otherwise
it's going to say good evening
and so now we can actually do multiple
lifts
so here we can say
if it's equal to 12.
uh let's see we're gonna say it is noon
so no so here good day
less than 17 it's not going to get there
because it's going to actually be hit
here and then essentially bail out
so let's take a look at that so we've
got 10 12 and
let's play with these a little bit so
the first thing we do is we come in if
it's one in the morning
it says good morning easy enough if we
say 10
then it's good day because
uh it's not less than 10. it's actually
equal to 10.
if we do noon it says it's noon because
it comes through here and it says if
it's less than 10 good morning
otherwise if it's equal to 12 it's noon
otherwise if it's less than 17 we're
gonna say good day so actually
we can go see where if we use 11 it
still says good day
because here it's greater than 10 it's
not equal to 12 but it's
less than 17. this is where order can be
very important if i were to switch this
this lift then what's going to happen is
it will always
handle the if less than 10 then if less
than 17
the 12 never gets here because the 12
was already covered
by the 17 by the less than 17. so we
would actually never see it
in that case so it's important to
realize that these end the elifs
end if it's if it's true so it does its
thing and then it gets out
and so then going back to this if we do
at the end of the day
then it's just going to say good evening
so it's important to note um
let's do this note that
elif ends series
if true
and note we can have
multiple alif's
lf sections
notice here if i do else
bad day if i do two elses
it's going to tell me here that's an
invalid syntax here on the 19. so it
comes in and says
you can't have that you also
cannot do an else among the elifs
see because it's going to blow up on
slide 17 which it says oh okay you were
done oh wait a minute
because basically this if the once you
get to the else you're going to be
tying up you're going to ending this if
block
so that means this lift doesn't have an
if to tie itself at this point
and those are the kind of things you're
going to run into in particular
because of some of the other logical
constraints that we're going to talk
about
at some point as we're going through
this
you need you almost need the lifts but
you also need to
be able to to keep in line the logic
that you're actually walking through is
realizing this is a
if step one else step two l step three
and those
or say check one check two check three
where each of those
checks in this case only
is hit if the prior one uh fails
once it's true so here if it's good
morning it didn't even check
any of this stuff which from a speed
point of view is great that means you
could have a really long list
and the only time that the whole list
gets walked is when you get down to the
last value so if i did
let's say if it's less than one then if
that's less than two if it's less than
three if
you know all the way down so i could
have a whole bunch of lifs
then the only one that would execute all
of them in this case would be when i get
to 24 basically so if i get less than 24
or just
you know at some point at the end i may
have 20
you know 20 something if lift
combinations of you know series of
commands here but i'm normally not going
to run through them it's only going to
be when i have to
run all the way through it
so those are the the key points of that
um i think what i want to
do with this one and i do want to touch
on
uh some of the things we're going to be
doing next
now we've talked about because we're
going to be sort of building
an application out of this and i don't
want to get too deep
in but i do want to talk about um you
know
highlight a couple of things now one
we've talked about this formula make
sure we understand that we can build
strings using our
using our pluses or we actually can do
it with our asterisk as well because
remember we could do multiples
of those so if i did
print uh
a times five
then it's going to print out oh i gotta
do it with parentheses though
it's gonna print out five a's
so whatever number i do we can see where
it's just my five a's so
we can build strings that way we also
have our formats so i could say
so the greeting could be
good like that
and then i could have the second part
let's say the second part
is uh
oh let's just call it day text and i'll
use a little thing there
so day text could be and we'll start
with
we'll just say it's blank and then here
a text equals morning
it's not going to be quite as clean
because the way we're doing it
but just to show that we can build
strings out of this
in a formatting but we can also do them
uh
through our pluses and such and so here
this would be
uh let's do greeting dot
format and then give it the day text
and i should spell day text right
instead of day next
i can get rid of my multiples of fives
let's just comment that out
and so now there we go so i get my good
morning
or i get my good evening or whatever it
is
so we've been able to walk through these
and we're gonna start building out and
working on our strings that'll be part
of our ifs so note that
as we're going through this although we
have
i'm trying to build this out so we sort
of cover specific items throughout the
certification
so you could potentially jump in at any
given point
uh it may be easier to follow this you
know in order
because there's going to be things that
we may sort of gloss over
that and we were actually covered in a
prior
episode of prior session so we're going
to work
start working into the the logics and
loops and things like that so this is
we're going to continue working on our
little application next time around
so i'm gonna i'll leave it like that for
now
i think that's a good point to stop just
because the next bun chunk will be
a sizable enough chunk we're gonna get
into some next one actually probably do
uh vectors and indexing and slicing and
length and so we're gonna
we're gonna parse our uh we're gonna
make this a little more than
telling them to do a number and we're
gonna actually be able to do something
like
you know uh 12 35 or something like that
i'm even doing am or pm with it so
that'll be coming next i just want to
wrap this one up
again this is stuff that if it's new to
you
it will probably become more comfortable
as we go on because we're going to just
keep
we're going to see a lot of repetition
we've already seen ifs and else elsa's
before
we're going to see more of this and a
lot of it when you
as you saw with some of the stuff i was
playing around with the key is always
going to be
understand where your
tabs are basically what your your format
is where your index
indents are or not because python is
very indent
sensitive that being said i'll let you
get to it so go out there have yourself
a great day
a great week and we will talk to you
next time
you