- 00:00:01 hey guys welcome back to another video
- 00:00:03 this is the fifth video in my Python
- 00:00:06 programming series and today we're going
- 00:00:08 to be talking about chained conditionals
- 00:00:10 and nested if statements
- 00:00:13 so pretty much chained conditionals are
- 00:00:15 just adding multiple conditions in one
- 00:00:18 one line with using words like and' and
- 00:00:22 or' okay we also have a key word not
- 00:00:25 that we're going to talk about as well
- 00:00:27 so if you haven't seen the last videos
- 00:00:29 in my series go ahead and get watch
- 00:00:31 those now because they are prerequisite
- 00:00:33 for this video all right so let's get
- 00:00:35 right ahead and start so if you remember
- 00:00:37 before we have something cold conditions
- 00:00:39 so for example if we have the variable X
- 00:00:41 I'll set that to two and the variable Y
- 00:00:43 which is equal to three we can have a
- 00:00:45 condition like X equal equal Y now
- 00:00:48 obviously we know from the last video
- 00:00:49 that this is going to give us a value of
- 00:00:51 false and I cannot type today sorry and
- 00:00:57 that's because two is not equal to three
- 00:00:59 pretty straightforward I hope you all
- 00:01:02 understood that from the last video all
- 00:01:04 right so what we want to do in this now
- 00:01:06 is we're going to want to check multiple
- 00:01:08 conditions so we'll put a condition here
- 00:01:11 so if X is equal to Y and then we can
- 00:01:14 put a word in like this the and word now
- 00:01:18 we'll add another condition so if X plus
- 00:01:20 y is equal to five okay so this first
- 00:01:27 condition is going to give us a value of
- 00:01:29 false so if X is you will do Y because
- 00:01:31 they are not the same but now X plus y
- 00:01:33 does indeed equal five so that is going
- 00:01:36 to give us a value of true so where I'm
- 00:01:39 highlighting we have true and then where
- 00:01:40 I'm highlighting now again we have false
- 00:01:42 so because we have the keyword and this
- 00:01:45 if statement is not going to run so I'll
- 00:01:49 put a print here just to show you we're
- 00:01:51 going to print out true or let's just
- 00:01:54 say ran and you'll see you when I run
- 00:01:57 the program nothing is going to happen
- 00:01:59 that is because when we use the and
- 00:02:01 keyword it means both conditions on
- 00:02:03 either side must be true so if we change
- 00:02:07 this to something like if Y is equal to
- 00:02:10 three now that is going to be true
- 00:02:13 and we'll print out red okay so that's
- 00:02:17 pretty basic now let's do the or keyword
- 00:02:20 so we'll put or right here and we'll
- 00:02:23 change this condition back to if Y is
- 00:02:25 equal to X and now take a guess on what
- 00:02:28 you think is going to happen well pretty
- 00:02:31 much with the or keyword it means either
- 00:02:33 of the conditions have to be true so if
- 00:02:36 Y is equal to X or X plus y is equal to
- 00:02:41 five then we are going to run this line
- 00:02:44 now since X plus y does equal five we
- 00:02:46 have a true here and again Y is not
- 00:02:49 equal to X so we have a false and you'll
- 00:02:50 see that the program is going to run
- 00:02:52 that's because only one of the condition
- 00:02:55 conditions has to be true for it to run
- 00:02:58 okay so that is the end in the or
- 00:03:01 keyword
- 00:03:01 they are pretty straightforward and
- 00:03:02 pretty basic now I'd like to point out
- 00:03:05 that you can actually add as many
- 00:03:07 conditions as you like so now I can have
- 00:03:09 an or I can have an end I could have
- 00:03:12 another condition I could do as many as
- 00:03:14 I possibly can imagine as many as I'd
- 00:03:16 like to do so now we'll put an else
- 00:03:19 statement in here and we'll change this
- 00:03:22 just a little bit to use another keyword
- 00:03:26 we'll just put a sad face here to show
- 00:03:28 that we didn't run it that we ran the
- 00:03:30 else okay
- 00:03:32 so now there's something called B not
- 00:03:33 word okay so we're going to put not
- 00:03:35 right here around it and then I will
- 00:03:37 show you what it does so pretty much not
- 00:03:40 reverses anything that you have inside
- 00:03:43 of the brackets of the knot so in here
- 00:03:46 we have if Y is equal to X or X plus y
- 00:03:49 is equal to 5 so we know again that this
- 00:03:51 gives us a value of true so now since
- 00:03:54 true is inside of the not true becomes
- 00:03:58 false now we have if false so obviously
- 00:04:02 that's going to bring us to the else
- 00:04:03 statement which is going to print the
- 00:04:06 sad face so I'll show you that that does
- 00:04:08 indeed work so we have the sad face
- 00:04:11 right now if we change these so that
- 00:04:13 both of these conditions are false

- 00:04:15 you'll see that we have false false
- 00:04:17 which is going to give us an overall
- 00:04:19 value of false and then we're going to
- 00:04:22 have the knot which is going to change
- 00:04:23 that into a true and will allow us to
- 00:04:25 run it just like that
- 00:04:27 Rann so yeah so that's how the not the
- 00:04:31 end in the or work and now let's get
- 00:04:34 into nested loops or sorry not nested
- 00:04:37 loops nested if statements
- 00:04:39 so we'll do a basic if statement here if
- 00:04:42 X is equal to two if y is equal to three
- 00:04:48 and then we'll add our else's like this
- 00:04:53 and we put some print statements in here
- 00:05:00 okay ah just give me a second while I
- 00:05:03 type at the print statements and then we
- 00:05:05 will go into what this is going to do x
- 00:05:09 equals two and y equals three so you may
- 00:05:14 have guessed already but pretty much we
- 00:05:17 can actually add as many if statements
- 00:05:20 embedded or nested as we'd like so I
- 00:05:22 could add another if statement here and
- 00:05:23 just constantly keep checking now the
- 00:05:26 reason I showed you chained conditionals
- 00:05:28 is because if I didn't want to go
- 00:05:30 through the hassle of adding all of
- 00:05:31 these uh nested statements I could just
- 00:05:35 do and and right here and check for
- 00:05:37 another condition but sometimes we do
- 00:05:39 want to do nested statements so we'll
- 00:05:42 say here x equals two Y does not equal
- 00:05:46 what is it three okay so I know I just
- 00:05:51 said that pretty quickly but let's walk
- 00:05:52 through it now so we have if X is equal
- 00:05:55 to two then we're going to run whatever
- 00:05:57 is in here because it is indented so now
- 00:06:01 we come in here and we say well is y
- 00:06:03 equal to three let's say it is now we're
- 00:06:05 going to print out this statement and
- 00:06:07 we're going to be done with this loop
- 00:06:09 with this a bit of code so we're going
- 00:06:11 to skip down to the end of the program
- 00:06:12 where we have nothing else then so let's
- 00:06:15 say we do another example X is equal to
- 00:06:17 two but y equals four so that means
- 00:06:20 we're actually going to run the else
- 00:06:21 which means now we've got print X is
- 00:06:24 equal to 2 but Y does not equal 3 and
- 00:06:27 then the last case is X is not equal to
- 00:06:30 2 so we don't even bother checking if Y
- 00:06:32 is equal to 3 we just go down and we
- 00:06:34 print X does not equal 2 so we'll change
- 00:06:38 around the variables and we'll show you
- 00:06:39 how this works just vary
- 00:06:40 quickly so we can see X is equal to 2
- 00:06:43 and Y is equal to 3 that is because
- 00:06:45 obviously up here 2 & 3 now if I want to
- 00:06:47 change Y and we change it to 4 we get X
- 00:06:52 is equal to 2 but Y is not equal to 3 so
- 00:06:55 that means we ran this one in here now
- 00:06:58 let's change it so that X is equal to 4
- 00:07:01 as well and we can see we get X does not
- 00:07:05 equal 2 all right so that's the basics
- 00:07:10 on nested statements and chained
- 00:07:12 conditionals I hope you found this video
- 00:07:14 helpful um stay tuned for more videos
- 00:07:17 are going to be getting into some more
- 00:07:19 advanced topics and in the next video
- 00:07:20 we're going to be covering loops um yeah
- 00:07:23 so if you liked the video please
- 00:07:25 subscribe and like and I'll see you in
- 00:07:28 the next one