Coding

Python Programming Tutorial #3 – Conditions

  • 00:00:00 hey guys welcome back to the third video
  • 00:00:03 in my Python programming tutorial series
  • 00:00:05 today we're going to be talking about
  • 00:00:07 something called conditions so I hope
  • 00:00:09 you remember from the other videos we
  • 00:00:11 talked about input printing we did
  • 00:00:14 variables data types and then we talked
  • 00:00:17 about operators so operators like + –
  • 00:00:20 division integer division molecule is
  • 00:00:23 write multiplication all those operators
  • 00:00:26 today we're going to be talking about a
  • 00:00:28 few more operators and then getting into
  • 00:00:30 conditions so here in my file right here
  • 00:00:33 I've put down for basic comparison
  • 00:00:36 operators so pretty much a comparison is
  • 00:00:39 something that's going to return a value
  • 00:00:41 of true or false so here we have let's
  • 00:00:45 say of a real world example is 10 equal
  • 00:00:48 to Jill well no they're not because Tim
  • 00:00:52 obviously is spelt differently than Joe
  • 00:00:54 so that's going to be giving us a value
  • 00:00:56 of false on the same thing if we want to
  • 00:00:58 go into numbers we say is 18 is greater
  • 00:01:01 than 2 well it's not true yes it is so
  • 00:01:06 that condition gives us a value of true
  • 00:01:08 it's a very simple concept but it's
  • 00:01:10 something that we have to understand as
  • 00:01:12 we go into decisions later on which will
  • 00:01:15 be in the next video okay so these
  • 00:01:17 comparison operators I'll read them off
  • 00:01:19 and just kind of give a definition of
  • 00:01:21 each of them so the first one it can you
  • 00:01:22 be a less than or greater than sign same
  • 00:01:25 thing with the second one right so it's
  • 00:01:27 just going to compare if something is
  • 00:01:28 less than greater than usually these are
  • 00:01:31 used for integer values that datatype we
  • 00:01:35 also can use them for Strings but it's a
  • 00:01:38 little more complex and I'm not going to
  • 00:01:40 get into that right now maybe in a more
  • 00:01:41 advanced tutorial later on now the
  • 00:01:44 equivalent operator so the equal sign
  • 00:01:47 here now a lot of people think well if
  • 00:01:49 I'm comparing something why wouldn't I
  • 00:01:51 just use one equal sign like this well
  • 00:01:54 that's a very good question a lot of
  • 00:01:55 people get mixed up on this it is
  • 00:01:57 actually two equal signs that's because
  • 00:01:59 in Python the one equal sign is a
  • 00:02:03 declaration declaration operator so for
  • 00:02:07 example if I put a variable X and I want
  • 00:02:09 to set it equal to four right
  • 00:02:12 this is what we do with the one equal
  • 00:02:16 sign we're setting values right so when
  • 00:02:17 we did name is equal to ten things like
  • 00:02:21 that we're using one equal sign now if
  • 00:02:24 we're going to check something we're
  • 00:02:25 going to compare them we have to use two
  • 00:02:27 because two equal signs and one equal
  • 00:02:29 sign have a very different meaning so
  • 00:02:31 it's important to keep that in mind that
  • 00:02:32 the comparison operator for equivalency
  • 00:02:34 is two equal signs okay the next one is
  • 00:02:38 not equal to so we have an exclamation
  • 00:02:42 mark and then we have the equal sign
  • 00:02:44 like this okay so an example of not
  • 00:02:47 equal to because some people may get
  • 00:02:49 confused here is a if for not equal to
  • 00:02:52 five oops what did I type they're not
  • 00:02:57 equal to five okay so what do you think
  • 00:03:00 this would give us just think about it
  • 00:03:02 for a second well what it's actually
  • 00:03:04 going to return to us is true because
  • 00:03:08 the condition is if for is not equal to
  • 00:03:12 five and here we can see obviously four
  • 00:03:16 is not equal to five so now again if I
  • 00:03:18 give another example we put five here we
  • 00:03:20 say five not equal to five that would
  • 00:03:22 return a value of false because five is
  • 00:03:26 indeed equal to five now we can do the
  • 00:03:29 same thing with strings so for example
  • 00:03:31 we have hello oops
  • 00:03:32 spelled incorrectly there we go and Tim
  • 00:03:37 now you can probably guess by yourself
  • 00:03:40 that this is going to return true
  • 00:03:42 because Tim is indeed not equal to hello
  • 00:03:46 okay it's pretty straightforward but a
  • 00:03:50 lot of people may get mixed up with the
  • 00:03:51 not equal to if it's returning true or
  • 00:03:53 false okay so those are the four basic
  • 00:03:56 comparison operators that we're going to
  • 00:03:58 use so we have equal to not equal to and
  • 00:04:01 greater than and less than so greater
  • 00:04:03 than or less than again we use four
  • 00:04:04 numbers so one less than two again
  • 00:04:07 that's going to return true like that
  • 00:04:10 okay so now we're going to start
  • 00:04:13 printing from some things to the console
  • 00:04:15 and maybe doing a little bit of a quiz
  • 00:04:17 here just to see how you guys are going
  • 00:04:20 to do so a boolean condition I'm going
  • 00:04:23 to store it in a variable so I'm going
  • 00:04:24 to say
  • 00:04:25 oops actually we're not gonna do it
  • 00:04:27 we're just gonna print to the console
  • 00:04:29 two less than three okay now you guys
  • 00:04:35 guess and tell me what you think is
  • 00:04:37 going to be returned to the console it's
  • 00:04:39 either going to be true or false I'll
  • 00:04:41 give you a hint right now now it gives
  • 00:04:45 us a value of true that's because this
  • 00:04:48 condition two is less than three is true
  • 00:04:52 it's equal to three right so now if we
  • 00:04:54 switch the sign and we make it greater
  • 00:04:56 than like that well you can guess what's
  • 00:04:59 going to come up so we click enter and
  • 00:05:01 we get false that's because this
  • 00:05:04 condition returns a value of false okay
  • 00:05:08 now we'll do another condition right
  • 00:05:11 hello equal to zero okay and guess
  • 00:05:20 yourself what this one's going to return
  • 00:05:21 false okay beep that's because they are
  • 00:05:24 spelled differently now we're going to
  • 00:05:27 do the not equal sign just to show that
  • 00:05:28 one as well hello not equal to hello and
  • 00:05:31 we'll see that gives us true because
  • 00:05:34 they are not the same I'm fairly
  • 00:05:36 straightforward but a lot of people do
  • 00:05:38 get these things mixed up now I'll show
  • 00:05:40 you what happens when I try to just do
  • 00:05:42 one equal sign like this see we get
  • 00:05:47 keyword can't be an expression that's
  • 00:05:49 because we're trying to declare a
  • 00:05:52 variable here but we cannot do that okay
  • 00:05:55 because we're just using one equal sign
  • 00:05:57 here we need to use two all right okay
  • 00:06:00 so now let's try another example maybe a
  • 00:06:02 more advanced one this is a little bit
  • 00:06:05 of a trick okay I want to see if you
  • 00:06:06 guys remember from my last tutorial what
  • 00:06:08 I talked about so take a guess right now
  • 00:06:14 what you think is going to be returned
  • 00:06:15 you think it's going to be true or doing
  • 00:06:16 it's gonna be false well it was false
  • 00:06:20 okay that's because again what we talked
  • 00:06:22 about in Python um capitals matter right
  • 00:06:26 so any capital letter is different than
  • 00:06:28 a lowercase letter so something like
  • 00:06:30 this yeah it's going to return false
  • 00:06:33 all right it's important to understand
  • 00:06:35 okay so we've very briefly gone into
  • 00:06:38 conditions now I'm going to write a few
  • 00:06:41 on the side here you guys can go ahead
  • 00:06:42 put them in print statements and test
  • 00:06:45 yourself and see if you get them right
  • 00:06:46 or not okay I'm going to add a few
  • 00:06:51 tricks from the other lesson try to kind
  • 00:06:53 of apply some of our knowledge and see
  • 00:06:57 if we can get them correct
  • 00:06:59 okay so just give me a minute here just
  • 00:07:03 write a few out and then we'll go
  • 00:07:09 through the answers afterwards okay so
  • 00:07:12 you know we'll just we'll just do these
  • 00:07:13 ones for right now and we'll do a few
  • 00:07:14 other examples okay so these are a
  • 00:07:16 little more advanced than what I was
  • 00:07:17 talking about before but they are
  • 00:07:19 conditions we have a left side of the
  • 00:07:21 condition and we have a right side of
  • 00:07:23 the condition so I hope you've taken a
  • 00:07:24 guess now of what you think these are
  • 00:07:26 going to because I'm about to print out
  • 00:07:27 the answer right now
  • 00:07:28 okay so we've gone true false and false
  • 00:07:32 all right so for the first one too less
  • 00:07:35 than three that's an example I already
  • 00:07:37 gave that gives us a value of true
  • 00:07:39 because the back condition is true now
  • 00:07:42 we have two minus three plus four
  • 00:07:44 greater than five so you can do the math
  • 00:07:47 there and see that that is going to be
  • 00:07:49 false and then this one some people may
  • 00:07:52 have done a little bit a little bit
  • 00:07:54 confused on because I actually use the
  • 00:07:56 word false and true in my condition
  • 00:07:59 that's going to return false or true so
  • 00:08:01 I have print false equals equals true
  • 00:08:03 well is false the same thing is true no
  • 00:08:06 it's not so obviously we are going to be
  • 00:08:08 returned with the value false okay so
  • 00:08:11 we've talked a little bit about
  • 00:08:12 conditions now um in the next tutorial
  • 00:08:15 we're going to be doing something called
  • 00:08:16 decision so I'll give you a sneak peek
  • 00:08:18 on what that is something like this okay
  • 00:08:23 so stay tuned for that um the next step
  • 00:08:26 should should be up tomorrow and yeah I
  • 00:08:30 hope you guys enjoyed the video go ahead
  • 00:08:31 like and subscribe and yeah share with