1 00:00:00,000 --> 00:00:03,210 Okay, let's take a look at some comparison operator shortcuts. 2 00:00:03,220 --> 00:00:07,140 So in the last video, if you didn't watch it, you might actually 3 00:00:07,150 --> 00:00:08,220 want to go back and watch it, 4 00:00:08,540 --> 00:00:11,120 there was a point near the beginning of the video where I 5 00:00:11,130 --> 00:00:14,570 said a comparison operator behind the scenes is always looking 6 00:00:14,580 --> 00:00:15,470 for something to be True. 7 00:00:15,470 --> 00:00:20,700 So if something is True, 'print("This is a true statement")'. 8 00:00:20,750 --> 00:00:25,080 And sure enough, this prints because 'if True', well 'True' 9 00:00:25,080 --> 00:00:27,800 is always True. So if it's True, print the statement. 10 00:00:27,860 --> 00:00:31,040 That in itself is somewhat of a shortcut. 11 00:00:31,280 --> 00:00:35,600 But with different data types, things can be cast as either 12 00:00:35,610 --> 00:00:36,500 True or False. 13 00:00:36,500 --> 00:00:40,200 So we sort of saw this back when we were looking at Booleans. 14 00:00:40,660 --> 00:00:42,310 So let's do a string example. 15 00:00:42,310 --> 00:00:46,100 A 'string' example is going to be "Python for Everybody". 16 00:00:46,100 --> 00:00:52,600 So we can say 'if string: print("String has a value"), 17 00:00:53,900 --> 00:00:59,100 And we can also say, 'if not string', 18 00:00:59,160 --> 00:01:04,410 So if 'string' does not have a value, 'print("String does not 19 00:01:04,410 --> 00:01:05,900 have a value")''. 20 00:01:05,900 --> 00:01:09,890 So if we just look over this, 'string = "Python for 21 00:01:09,900 --> 00:01:12,540 Everybody", it's going to execute this one. 22 00:01:12,540 --> 00:01:14,400 And there's another 'if' statement. 23 00:01:14,490 --> 00:01:17,550 If there is nothing in the string, it's going to say "String 24 00:01:17,550 --> 00:01:18,700 does not have a value")'. 25 00:01:20,200 --> 00:01:23,100 And instead of printing the two statements, it only prints the one. 26 00:01:23,140 --> 00:01:25,060 Now let's go ahead and get rid of this. 27 00:01:25,070 --> 00:01:27,880 Let's just use an empty string and rerun this cell. 28 00:01:27,890 --> 00:01:29,170 Sure enough, that changes. 29 00:01:29,170 --> 00:01:30,900 Says, "String does not have a value". 30 00:01:30,920 --> 00:01:32,870 That's because this 'string' is empty. 31 00:01:32,880 --> 00:01:37,770 Now, the reason for that is because if we cast our string 32 00:01:37,780 --> 00:01:41,290 as a Boolean, we can actually see what that's going to be. 33 00:01:41,300 --> 00:01:43,200 The 'string' is currently False. 34 00:01:43,210 --> 00:01:48,040 But if I rerun this, 'string' is "Python for Everybody", rerun 35 00:01:48,050 --> 00:01:49,570 that one, and let's rerun this one, 36 00:01:49,580 --> 00:01:50,500 this will now be True, 37 00:01:50,800 --> 00:01:54,070 and that's because an empty string is always considered False. 38 00:01:54,080 --> 00:01:57,640 And anything that is inside of a string, literally anything 39 00:01:57,820 --> 00:02:01,100 makes it True, at least in the eyes of a Boolean. 40 00:02:01,100 --> 00:02:04,000 And now that comes back up to here where we're saying, if 41 00:02:04,000 --> 00:02:05,000 something is True. 42 00:02:05,000 --> 00:02:09,530 So if a string is empty and we cast it as a Boolean, that 43 00:02:09,538 --> 00:02:10,550 comes back as False. 44 00:02:10,550 --> 00:02:14,000 And so that's basically the exact same as saying 'if False', 45 00:02:14,039 --> 00:02:17,100 well, that's not going to run because False comparison operators 46 00:02:17,260 --> 00:02:19,630 never, ever get executed. 47 00:02:19,640 --> 00:02:24,410 So let's run this one more time and just keep that data consistent. 48 00:02:24,420 --> 00:02:26,360 Let's look at some sort of list. 49 00:02:26,370 --> 00:02:30,160 So 'lst' is equal to an empty list, and we can do all this 50 00:02:30,160 --> 00:02:31,100 one in one cell. 51 00:02:31,840 --> 00:02:38,050 So we can now say 'if lst: print("There are items in the 52 00:02:38,050 --> 00:02:42,500 list")'. And let's count the number of items in this list. 53 00:02:42,850 --> 00:02:46,190 And we can also say, 'if not lst', 54 00:02:46,200 --> 00:02:52,060 so if this list is False, 'print("There are no items in this 55 00:02:52,090 --> 00:02:54,710 list")'. Let's go ahead and execute that. 56 00:02:54,710 --> 00:02:57,800 Sure enough, it says, "There are no items in this list", because 57 00:02:57,800 --> 00:02:59,700 an empty list is also False. 58 00:03:01,100 --> 00:03:04,700 But we could put 'Python' in here, and we could also put 'JavaScript', 59 00:03:04,700 --> 00:03:05,900 for example. 60 00:03:05,900 --> 00:03:07,200 And let's rerun this cell. 61 00:03:07,300 --> 00:03:11,300 And now it says, there are two items in the list and the number 62 00:03:11,300 --> 00:03:13,200 2 shows up, because that's how many items are in here. 63 00:03:13,260 --> 00:03:15,900 Now I'm going to copy this entire thing. 64 00:03:15,900 --> 00:03:20,200 And let's change this from a list to a tuple. 65 00:03:20,260 --> 00:03:22,540 And let's call that 'tup'. 66 00:03:22,550 --> 00:03:24,670 I probably should have just wrote this out from scratch, 67 00:03:24,670 --> 00:03:28,000 it would have been a little easier than doing it this way. 68 00:03:28,600 --> 00:03:30,000 'len(tup)'. 69 00:03:31,400 --> 00:03:36,800 "There are items in the tuple", and "There are no items in the 70 00:03:36,900 --> 00:03:39,660 tuple". Yep, that was a little painful, I bet. 71 00:03:40,000 --> 00:03:44,000 Okay, so if we run this, this tuple has two items in it. Based 72 00:03:44,000 --> 00:03:45,200 on what we know about a list, 73 00:03:45,260 --> 00:03:47,720 do you think this is going to say, "There are items in the 74 00:03:47,730 --> 00:03:49,160 tuple", with a length of the tuple, 75 00:03:49,160 --> 00:03:51,100 or do you think the 'print' statement is going to say, "There are 76 00:03:51,100 --> 00:03:52,200 no items in this tuple."? 77 00:03:52,200 --> 00:03:54,500 [no audio] 78 00:03:54,500 --> 00:03:57,700 It is actually going to say that, "There are items in this 79 00:03:57,720 --> 00:04:00,710 tuple". Sure enough, that's how it works. 80 00:04:00,720 --> 00:04:04,850 And if we get rid of this, just an empty tuple, "There are 81 00:04:04,860 --> 00:04:05,870 no items in the tuple". 82 00:04:06,860 --> 00:04:10,730 Now the same thing works with dictionaries using curly braces, 83 00:04:10,730 --> 00:04:14,330 as well as set, which will be evaluated the same way when 84 00:04:14,330 --> 00:04:15,300 they're empty anyways. 85 00:04:15,320 --> 00:04:18,980 So an empty dictionary will return False, and an empty set 86 00:04:18,990 --> 00:04:20,480 will also return False as well. 87 00:04:21,899 --> 00:04:23,000 Now, here's an interesting one. 88 00:04:23,000 --> 00:04:24,399 What if we look at None? 89 00:04:24,399 --> 00:04:28,100 So let's say 'total_items' of a thing is 'None'. 90 00:04:28,800 --> 00:04:38,700 'if total_items: print("There are items")'. We can say, 'if not 91 00:04:38,770 --> 00:04:43,210 total_items: print("There are no items")'. 92 00:04:43,210 --> 00:04:46,390 Now, this is an interesting one, because 'None' technically 93 00:04:46,500 --> 00:04:47,500 has nothing. 94 00:04:47,780 --> 00:04:53,090 And so should 'None' by default evaluate to be True or to be 95 00:04:53,100 --> 00:04:56,670 False? So let's run this and find out. It says, "There are no items". 96 00:04:56,800 --> 00:05:00,000 By default, 'None' is always False in Python. 97 00:05:00,040 --> 00:05:03,490 Now there's another operator that I'm going to talk about 98 00:05:03,500 --> 00:05:06,760 now, and that is the 'is' operator, and that is not the same 99 00:05:06,820 --> 00:05:07,720 as equal sign. 100 00:05:07,730 --> 00:05:12,480 So a lot of people think 'if total == None' is the 101 00:05:12,490 --> 00:05:18,460 exact same as saying 'if total is None', because when we read 102 00:05:18,470 --> 00:05:20,470 this, we read it in our brains the same way. 103 00:05:20,480 --> 00:05:24,530 If total is equal to None, I mean, I just said it, if total 104 00:05:24,600 --> 00:05:28,200 is None, then what do we do? 'print', 105 00:05:29,400 --> 00:05:31,300 let's do '== None'. 106 00:05:31,390 --> 00:05:34,660 And in here, let's do 'print("is none")'. 107 00:05:36,100 --> 00:05:37,210 And it wasn't 'total', 108 00:05:37,420 --> 00:05:38,680 the reason that didn't work, 109 00:05:38,700 --> 00:05:41,200 it was because it is 'total_items'. 110 00:05:41,200 --> 00:05:42,700 Let's rerun that. There we go. 111 00:05:42,780 --> 00:05:44,030 And they're both triggered. 112 00:05:44,480 --> 00:05:48,450 Now, what's interesting about this is when you're comparing 113 00:05:48,450 --> 00:05:51,700 a number or a string, always use the '==' sign, 114 00:05:51,740 --> 00:05:54,350 and when you're looking for 'None', then use 'None'. 115 00:05:54,590 --> 00:05:58,400 What 'is' is doing, is it's looking for an exact location in 116 00:05:58,410 --> 00:06:00,950 memory. It's trying to make sure that something is literally 117 00:06:00,950 --> 00:06:02,500 the exact same thing. 118 00:06:02,500 --> 00:06:09,150 So if I said 'name1 = "kalob"', and 'name2' 119 00:06:09,180 --> 00:06:12,450 also '= "kalob"', spelt the exact same way. Behind the scenes, 120 00:06:12,460 --> 00:06:15,240 what Python is going to do is say, "Oh, this was set already 121 00:06:15,250 --> 00:06:17,940 once, I'm going to point this variable to this piece of memory, 122 00:06:17,940 --> 00:06:21,100 and I'm going to point this variable to the exact same piece of memory." 123 00:06:21,140 --> 00:06:26,030 And so let's set that, and do 'name1 is name 2', 124 00:06:26,040 --> 00:06:27,260 and we'll see that it's True. 125 00:06:27,680 --> 00:06:30,410 Now this gets into the inner workings of Python, which I 126 00:06:30,420 --> 00:06:33,360 don't want to talk too much about, because for some people, 127 00:06:33,370 --> 00:06:34,500 that can be pretty boring. 128 00:06:34,510 --> 00:06:38,840 But because these are identical, we can use 'is'. 129 00:06:38,850 --> 00:06:41,870 So this has a piece of memory, and this has a piece of memory. 130 00:06:42,060 --> 00:06:46,140 And in Python they're pointing to the exact same piece of 131 00:06:46,140 --> 00:06:47,500 memory. So 'is' is going to work. 132 00:06:47,540 --> 00:06:50,510 The reason why it works with None, and the reason why you'll 133 00:06:50,510 --> 00:06:54,100 see it a lot in Python when comparing to None is because 134 00:06:54,100 --> 00:06:57,800 None is always going to be the same piece of memory in Python as well. 135 00:06:57,880 --> 00:07:00,850 So 'total_items', if that's None it's going to point to the exact 136 00:07:00,860 --> 00:07:04,430 same value, the exact same piece of memory that Python has 137 00:07:04,440 --> 00:07:07,330 already dedicated to just having nothing in there. 138 00:07:07,330 --> 00:07:12,400 Now, if that was complicated, if that was too confusing, 139 00:07:12,400 --> 00:07:16,100 for now, just forget about it and just stick with '=='. 140 00:07:16,130 --> 00:07:18,430 Last but not least, don't forget 141 00:07:18,440 --> 00:07:21,700 when you are comparing variables, you need to make sure that 142 00:07:21,700 --> 00:07:24,200 you are comparing the exact same type. 143 00:07:24,240 --> 00:07:29,730 So if you're comparing a string, if 'name1' is a string, 144 00:07:29,740 --> 00:07:32,220 and 'name2' is not a string, it's not going to work. 145 00:07:32,320 --> 00:07:34,960 You need to make sure you're comparing strings against strings, 146 00:07:34,960 --> 00:07:40,200 numbers against numbers, and lists again against lists. 147 00:07:40,260 --> 00:07:42,540 And actually, as an example, let's do this. 148 00:07:42,600 --> 00:07:45,500 'ls1' = ['hello']'. 149 00:07:45,500 --> 00:07:51,900 It only has one item in there, and 'ls2 = ['hello']' as well. 150 00:07:51,900 --> 00:07:57,500 And then we can say 'if ls1 == ls2: print()', 151 00:07:57,500 --> 00:08:00,200 that is very close to the bottom of the screen there, 'print( 152 00:08:01,600 --> 00:08:05,100 "HELLO IS IN BOTH LISTS")' 153 00:08:05,600 --> 00:08:06,600 And bam, 154 00:08:06,600 --> 00:08:09,500 we are now comparing lists against lists. 155 00:08:09,500 --> 00:08:11,300 Got to make sure they're the same though, because if this 156 00:08:11,300 --> 00:08:15,500 changes at all, let's say this has a second item in here, 157 00:08:15,500 --> 00:08:16,900 it no longer works. 158 00:08:16,900 --> 00:08:18,700 So you got to make sure they're the exact same. 159 00:08:18,700 --> 00:08:21,800 But you can compare lists and tuples, and dictionaries, and 160 00:08:21,800 --> 00:08:23,600 sets against each other.