1 00:00:00,000 --> 00:00:03,690 Okey-Dokey. Let's take a look at comparison operators. 2 00:00:03,720 --> 00:00:07,820 So I'm just going to create a new Jupyter Notebook here. 3 00:00:08,300 --> 00:00:12,640 Now, when it comes to a comparison operator, these are the 4 00:00:12,640 --> 00:00:17,300 little if-else statements that tell program what to do. 5 00:00:17,380 --> 00:00:20,710 So if you ask me, "Kalob, what is your favorite color?" 6 00:00:20,720 --> 00:00:22,960 And I said my favorite color was blue. 7 00:00:22,970 --> 00:00:25,930 You could write a program that says, "if Kalob's favorite color 8 00:00:25,940 --> 00:00:28,600 is blue, say, "Your favorite color is blue". 9 00:00:28,660 --> 00:00:33,020 Now, that is a very simple, overly simplistic example. 10 00:00:33,030 --> 00:00:35,720 But that is really how computers work. 11 00:00:35,730 --> 00:00:39,870 That's even how a lot of advanced mathematics really works 12 00:00:39,880 --> 00:00:41,370 when it comes to programming. 13 00:00:41,380 --> 00:00:44,970 So machine learning, data science, artificial intelligence, 14 00:00:44,970 --> 00:00:47,400 it's a lot of if-else statements. 15 00:00:47,400 --> 00:00:51,500 So what that means is you can assign a variable such as 16 00:00:51,500 --> 00:00:54,200 'age', and let's say my age is 30. It is. 17 00:00:54,370 --> 00:01:02,120 And I can say 'if age >= 21', 'print( 18 00:01:02,400 --> 00:01:09,100 "I can consume alcohols in the USA". 19 00:01:09,100 --> 00:01:12,100 [no audio] 20 00:01:12,100 --> 00:01:12,100 Boom, just like that. 21 00:01:12,190 --> 00:01:18,140 And now that is really just the base behind a lot of programming. 22 00:01:18,140 --> 00:01:21,500 So now I have this variable, and I can do something with it. 23 00:01:21,500 --> 00:01:24,710 Now, what we're looking at here is a comparison operator. 24 00:01:24,720 --> 00:01:27,750 So this says, 'if', that's a keyword. 25 00:01:27,750 --> 00:01:30,200 So you know, probably don't use that as a variable 26 00:01:30,200 --> 00:01:35,000 name, says, if the 'age' variable is greater than or equal to 27 00:01:35,020 --> 00:01:38,550 21. So essentially, the computer is looking at this, and 28 00:01:38,550 --> 00:01:42,700 it says if 30 is greater than or equal to 21, do a thing, 29 00:01:42,780 --> 00:01:43,920 just do something. 30 00:01:43,930 --> 00:01:48,810 Now, the reason that this was executed was not necessarily 31 00:01:48,820 --> 00:01:52,430 because 30 is greater than 21. 32 00:01:52,440 --> 00:01:58,210 It's actually because 30 is greater than or equal to 21 results 33 00:01:58,210 --> 00:01:59,800 in a True statement. 34 00:01:59,800 --> 00:02:04,200 So the Python interpreter sees this, if something is True, 35 00:02:04,260 --> 00:02:05,250 do something. 36 00:02:05,260 --> 00:02:08,070 If it is False, don't do something. 37 00:02:09,300 --> 00:02:12,199 So let's undo that, and I will show you an example. 38 00:02:12,289 --> 00:02:16,940 So 'if True: print("This was true")'. 39 00:02:16,940 --> 00:02:18,700 Now this is going to run. 40 00:02:18,700 --> 00:02:20,170 "This was true". 41 00:02:20,230 --> 00:02:25,140 Now, if I said the opposite, 'if False: print("This was false")'. 42 00:02:25,150 --> 00:02:28,840 This is not going to show up because this condition in here, 43 00:02:28,840 --> 00:02:32,500 this 'if' condition is always looking for something to be True. 44 00:02:32,500 --> 00:02:36,400 And so when I execute this, nothing happens. 45 00:02:36,400 --> 00:02:39,200 Nothing was printed there because it was False. 46 00:02:39,200 --> 00:02:41,450 Now we learned about all sorts of data types. 47 00:02:41,460 --> 00:02:43,940 And the reason we learned about data types is to help us 48 00:02:43,950 --> 00:02:45,470 with comparison operators. 49 00:02:45,480 --> 00:02:50,440 Comparison operators allow you to compare strings against 50 00:02:50,450 --> 00:02:53,290 strings, tuples against tuples, things like that. 51 00:02:53,300 --> 00:02:55,990 So let's really dive into a few different things. 52 00:02:56,170 --> 00:02:59,260 Now, before we do that, I'm going to show you one cool little 53 00:02:59,270 --> 00:03:01,910 thing. In our Python interpreter, 54 00:03:01,920 --> 00:03:05,090 so in here, or inside your Python shell, or your command line, 55 00:03:05,200 --> 00:03:09,100 you don't have to write an 'if' statement to determine if something 56 00:03:09,100 --> 00:03:13,700 is True or not. You can simply say 'True is True'. 57 00:03:14,900 --> 00:03:16,200 Yep. Sure enough, it is. 58 00:03:16,260 --> 00:03:20,030 You could say 30 > 29. 59 00:03:20,040 --> 00:03:25,210 Yep, it is. You could say 40 > 50. 60 00:03:25,210 --> 00:03:27,100 Nope, it's False. 61 00:03:27,120 --> 00:03:30,300 And so these aren't actually really doing anything. 62 00:03:30,310 --> 00:03:32,250 These aren't doing any extra commands. 63 00:03:32,260 --> 00:03:33,840 These are just results. 64 00:03:34,600 --> 00:03:36,100 Is 40 > 50? 65 00:03:36,100 --> 00:03:37,100 No, it is not. 66 00:03:37,100 --> 00:03:39,800 And so Python says, "Okay, it's not. 67 00:03:39,800 --> 00:03:41,500 So declare it False". 68 00:03:41,500 --> 00:03:44,800 So let's take a look at some comparison operators. 69 00:03:44,800 --> 00:03:48,460 When we're comparing numbers, we can use greater than, '>'; greater 70 00:03:48,460 --> 00:03:53,100 than is equal to, '>='; less than, '<'; less than is equal to, '<='; or is equal to, '=='. 71 00:03:54,100 --> 00:03:57,900 So we could say a 'num = 10'. 72 00:03:57,920 --> 00:04:01,340 And then we could say 'num == 10'. 73 00:04:01,340 --> 00:04:02,300 This will be True. 74 00:04:02,300 --> 00:04:04,400 'num > 10'. 75 00:04:05,300 --> 00:04:07,700 It's False because number is 10. 76 00:04:08,000 --> 00:04:10,600 It's not greater than, it's not less than, it is exactly. 77 00:04:10,600 --> 00:04:13,500 But 'num < 10'. 78 00:04:13,500 --> 00:04:14,700 No, it's not. 79 00:04:14,700 --> 00:04:19,200 But what about if 'num <= 10'? 80 00:04:19,200 --> 00:04:22,399 It's not less than 10, but it is in fact equal to 10, so 81 00:04:22,399 --> 00:04:23,899 this will result as True. 82 00:04:23,920 --> 00:04:27,250 Now, comparing numbers is pretty easy, but we can also compare 83 00:04:27,250 --> 00:04:31,700 strings. So we could say 'string' == 'string'. 84 00:04:31,760 --> 00:04:33,050 That's True. 85 00:04:33,060 --> 00:04:41,440 But 'string' == 'string2', is False. 86 00:04:41,450 --> 00:04:43,150 They are technically different. 87 00:04:43,300 --> 00:04:46,810 So while this is the same as this, because this value in 88 00:04:46,820 --> 00:04:48,820 here is different from this value, it's False. 89 00:04:49,040 --> 00:04:51,320 There's a way to compare that as well. 90 00:04:51,330 --> 00:04:54,740 And this gets a little bit more on the inverse side of things. 91 00:04:54,800 --> 00:05:00,400 So we can say 'string' != 'string2'. 92 00:05:00,400 --> 00:05:02,400 Now this is going to result in True. 93 00:05:02,450 --> 00:05:05,370 And the reason for that is because we're saying, "Does this 94 00:05:05,380 --> 00:05:06,330 not equal this?" 95 00:05:06,340 --> 00:05:08,820 And so it's kind of like a double negative. 96 00:05:08,820 --> 00:05:10,800 Does it not-not equal something? 97 00:05:10,840 --> 00:05:14,020 And this is going to result in True, as we can see here, 98 00:05:14,200 --> 00:05:18,200 because 'string' is in fact not like 'string2'. 99 00:05:18,260 --> 00:05:21,500 If I told you that coffee and tea were different, and you 100 00:05:21,500 --> 00:05:24,900 said 'if 'coffee' != 'tea'', that would be True, because 'coffee' 101 00:05:24,900 --> 00:05:25,900 is not 'tea'. 102 00:05:25,980 --> 00:05:29,220 So when we dive into comparison operators, all we're going 103 00:05:29,220 --> 00:05:31,700 to do is really add an 'if' statement in front of this, 104 00:05:32,700 --> 00:05:38,000 'if something is True', that is what Python is looking for, 105 00:05:38,060 --> 00:05:41,400 'if something is True'. Most programming languages are actually 106 00:05:41,400 --> 00:05:43,800 looking for that. They're just looking for something to be True. 107 00:05:43,840 --> 00:05:46,930 So let's go ahead and ask for a 'name'. 108 00:05:48,500 --> 00:05:49,500 'name = input( 109 00:05:49,580 --> 00:05:52,650 "What is your name?")'. 110 00:05:53,500 --> 00:05:54,900 And my name is going to be 111 00:05:56,400 --> 00:05:57,400 "Kalob". 112 00:05:58,050 --> 00:06:01,160 I can now display 'name'. Shows up as "Kalob". 113 00:06:01,170 --> 00:06:05,510 So now I can say 'if name == 'Kalob'', spelt the exact 114 00:06:05,540 --> 00:06:08,820 same way, because if this is lowercase, it's not the same. 115 00:06:09,100 --> 00:06:13,900 For instance, 'kalob' does not equal 'Kalob'. 116 00:06:13,900 --> 00:06:15,610 They're not the same thing in Python. 117 00:06:15,620 --> 00:06:20,180 That capital 'K' versus that lowercase 'k' makes the world of 118 00:06:20,180 --> 00:06:22,200 difference here. So it is very important. 119 00:06:22,280 --> 00:06:27,970 So let's go ahead and use a capital 'K' and say, 'print("Kalob 120 00:06:28,000 --> 00:06:32,000 Taulien is your Python teacher")'. 121 00:06:32,900 --> 00:06:36,600 And boom, that shows up. Now, a nice little trick here is 122 00:06:36,700 --> 00:06:40,660 when you're accepting user input, it's really hard to determine 123 00:06:40,670 --> 00:06:43,870 if the user is going to have all capitals, or all lowercase, 124 00:06:44,000 --> 00:06:49,100 or something spelt weird like this where it's capital 'KAL', lowercase 'ob' 125 00:06:49,100 --> 00:06:54,400 So what we can do, is we can say 'if name.lower', which 126 00:06:54,450 --> 00:06:58,909 is going to turn it all into lowercase, '== 'kalob': print( 127 00:06:58,909 --> 00:07:06,500 "Name is still Kalob but we detected it by a lower case name")'. 128 00:07:06,500 --> 00:07:08,500 [no audio] 129 00:07:08,500 --> 00:07:09,800 Voila! just like that. 130 00:07:10,500 --> 00:07:14,900 And on the inverse, because 'name' is "Kalob", we could say, 131 00:07:14,900 --> 00:07:24,500 'if name != 'Jacob': print("This is anyone except Jacob")'. 132 00:07:25,200 --> 00:07:26,200 And there it is. 133 00:07:26,200 --> 00:07:29,600 And that 'name' could be literally anything except for 'Jacob' 134 00:07:29,600 --> 00:07:30,600 with a capital J. 135 00:07:30,600 --> 00:07:34,200 Now, those are just some basic comparison operators. 136 00:07:34,200 --> 00:07:36,300 That's what we're going to stick with for this particular 137 00:07:36,300 --> 00:07:39,000 lesson. What I would like you to do is get a little bit of 138 00:07:39,020 --> 00:07:42,650 hands on experience with 'if' statements right now. 139 00:07:42,820 --> 00:07:46,750 So just create a variable, could be 'name', could be 'course', 140 00:07:46,750 --> 00:07:47,800 could be 'age', 141 00:07:47,830 --> 00:07:52,390 and then if you're comparing a string such as where I had 142 00:07:52,400 --> 00:07:56,000 'Kalob' ==, or != 'Jacob', make sure you're 143 00:07:56,000 --> 00:07:57,800 comparing it to another string. 144 00:07:57,800 --> 00:08:03,570 And if you're comparing a number, which was somewhere over 145 00:08:03,570 --> 00:08:06,400 here, then you're going to want to make sure that your data 146 00:08:06,400 --> 00:08:08,200 type is compared to a number. 147 00:08:08,400 --> 00:08:13,300 Now, one thing to keep in mind is numbers don't have quotation 148 00:08:13,300 --> 00:08:14,900 marks, so it's not "21", 149 00:08:16,300 --> 00:08:17,400 it's just 21. 150 00:08:17,400 --> 00:08:19,600 Floats don't have quotations either. 151 00:08:19,620 --> 00:08:22,620 So it's not, "3.14", 152 00:08:22,680 --> 00:08:24,120 it's just 3.14. 153 00:08:24,300 --> 00:08:28,350 Python is smart enough to recognize when that is the exact 154 00:08:28,350 --> 00:08:30,300 same thing, but that's not the point. 155 00:08:30,300 --> 00:08:32,299 You want to always make sure you're comparing the same data 156 00:08:32,350 --> 00:08:33,539 type to the same data type. 157 00:08:33,549 --> 00:08:36,690 So here we're comparing a string to a string, here 158 00:08:36,700 --> 00:08:39,840 we're comparing a number to a number, or an integer to an 159 00:08:39,850 --> 00:08:43,429 integer. And remember, your 'if' statement is always looking 160 00:08:43,429 --> 00:08:44,799 for something to be True. 161 00:08:44,840 --> 00:08:48,590 So go ahead, give this a shot, and try something like, if your 162 00:08:48,600 --> 00:08:52,799 name is "Matthew", then print, "Welcome, Matthew". 163 00:08:52,799 --> 00:08:57,500 Or if your 'age > 18', then, "You are not a child". 164 00:08:58,100 --> 00:09:00,100 Play around with the different operator types '>', 165 00:09:00,100 --> 00:09:03,300 '<', '>=', '<=', '==', 166 00:09:03,300 --> 00:09:07,000 with the two equal signs, or '!='. 167 00:09:07,580 --> 00:09:10,040 And when you're done playing around with that, and yeah, 168 00:09:10,050 --> 00:09:12,260 just feel free to experiment and break things. 169 00:09:12,270 --> 00:09:14,150 That's totally okay. When you're done with that 170 00:09:14,200 --> 00:09:17,800 let's head on over to that next lesson where we pick up 171 00:09:17,800 --> 00:09:21,300 on a few comparison operator shortcuts.