1 00:00:01,350 --> 00:00:02,183 -: All right. 2 00:00:02,183 --> 00:00:05,130 In this video I'm gonna try and trick you. 3 00:00:05,130 --> 00:00:08,220 I want you to look at these expressions 4 00:00:08,220 --> 00:00:10,980 where I'm checking for equality, right? 5 00:00:10,980 --> 00:00:11,813 I'm saying, 6 00:00:11,813 --> 00:00:16,140 "Hey does True == 1; does empty string == 1 7 00:00:16,140 --> 00:00:20,553 do two arrays equal, that are empty, equal well each other? 8 00:00:21,630 --> 00:00:25,258 Now, pause the video here and try to guess what the outcomes 9 00:00:25,258 --> 00:00:26,913 of this are going to be. 10 00:00:28,140 --> 00:00:29,100 Ready? 11 00:00:29,100 --> 00:00:31,020 By the way, if you get a hundred percent on this, 12 00:00:31,020 --> 00:00:32,100 well good for you 13 00:00:32,100 --> 00:00:35,138 because if this was my first time learning Python 14 00:00:35,138 --> 00:00:36,870 I wouldn't get this. 15 00:00:36,870 --> 00:00:37,703 Let's go. 16 00:00:39,750 --> 00:00:42,543 Hmm? Is that why you expected? 17 00:00:43,380 --> 00:00:45,834 True evaluated, so 18 00:00:45,834 --> 00:00:49,410 True == 1 evaluated to True? 19 00:00:49,410 --> 00:00:53,130 An empty string == 1 evaluated to False. 20 00:00:53,130 --> 00:00:53,963 That makes sense 21 00:00:53,963 --> 00:00:56,790 because they definitely don't equal each other, right? 22 00:00:56,790 --> 00:01:00,040 An empty array doesn't equal to 1 23 00:01:01,435 --> 00:01:05,530 and a 10 == 10.0 24 00:01:07,440 --> 00:01:10,830 and then both arrays that are empty equal to True. 25 00:01:10,830 --> 00:01:13,590 Now, the reason I'm showing you here is 26 00:01:13,590 --> 00:01:16,283 that the double equals checks 27 00:01:16,283 --> 00:01:21,283 for equality or equality of value. 28 00:01:21,660 --> 00:01:23,370 That is if, for example, 29 00:01:23,370 --> 00:01:26,970 the first one, True == 1, there's two different types. 30 00:01:26,970 --> 00:01:29,790 One's an integer, one's a bulleon 31 00:01:29,790 --> 00:01:33,060 it will convert them into the same type. 32 00:01:33,060 --> 00:01:36,600 In this case, this will be converted 33 00:01:36,600 --> 00:01:39,960 like this to a bulleon value. 34 00:01:39,960 --> 00:01:42,900 And if you remember, one is truthy. 35 00:01:42,900 --> 00:01:47,900 So this will evaluate to True, which is why we get True. 36 00:01:49,260 --> 00:01:51,870 What about the other one? 37 00:01:51,870 --> 00:01:56,640 Well, an empty string is falsey, so it evaluates to False. 38 00:01:56,640 --> 00:02:01,110 And False doesn't equal True, right? 39 00:02:01,110 --> 00:02:04,530 Because we're checking for equality here. 40 00:02:04,530 --> 00:02:08,430 So one of these gets converted to the other's type. 41 00:02:08,430 --> 00:02:10,949 What about an empty array? 42 00:02:10,949 --> 00:02:15,430 Again, an empty array is actually falsey 43 00:02:16,650 --> 00:02:18,720 so that's not going to == 1. 44 00:02:18,720 --> 00:02:22,500 A 10 equals to a float of 10.0. 45 00:02:22,500 --> 00:02:26,340 That gets converted to an integer or float 46 00:02:26,340 --> 00:02:27,930 and they're going to equal each other, 47 00:02:27,930 --> 00:02:30,270 and that's an expected behavior. 48 00:02:30,270 --> 00:02:34,260 And then an empty array will equals an empty array. 49 00:02:34,260 --> 00:02:39,260 If I add in this array '1, 2, 3', '1, 2, 3', 50 00:02:39,630 --> 00:02:42,420 and I click run, I get an invalid syntax. 51 00:02:42,420 --> 00:02:43,560 Oh, because I have this here. 52 00:02:43,560 --> 00:02:44,643 Let's run that again. 53 00:02:46,050 --> 00:02:47,493 I get True. 54 00:02:48,510 --> 00:02:49,440 That makes sense. 55 00:02:49,440 --> 00:02:51,930 Let's change this to 1 and see what happens. 56 00:02:51,930 --> 00:02:55,005 If I click run, all right. 57 00:02:55,005 --> 00:02:56,733 Still False. 58 00:02:57,780 --> 00:03:01,080 Now this does get a little tricky, right? 59 00:03:01,080 --> 00:03:04,680 Like, hmm, should this have evaluated to True? 60 00:03:04,680 --> 00:03:08,640 But no, we get False, so this doesn't get converted 61 00:03:08,640 --> 00:03:12,120 in type versus what we had here. 62 00:03:12,120 --> 00:03:13,710 Now, don't get confused by this 63 00:03:13,710 --> 00:03:15,540 because this would be bad code 64 00:03:15,540 --> 00:03:18,060 if you're checking something like this. 65 00:03:18,060 --> 00:03:22,290 Well, obviously you should be checking two types, 66 00:03:22,290 --> 00:03:25,290 two of the same types together. 67 00:03:25,290 --> 00:03:28,805 Ideally, when you use comparison operators 68 00:03:28,805 --> 00:03:33,120 or logical operators like this, you're comparing two types 69 00:03:33,120 --> 00:03:36,450 and you're not letting Python do this type conversion. 70 00:03:36,450 --> 00:03:39,720 And hopefully Python figures it out for us. 71 00:03:39,720 --> 00:03:43,740 But I hope the double equality makes sense 72 00:03:43,740 --> 00:03:46,440 because there's another check that we can do, 73 00:03:46,440 --> 00:03:51,440 which is, is and is, well, is a keyword in Python. 74 00:03:52,080 --> 00:03:57,080 What happens if we change all these equal signs to is? 75 00:03:58,860 --> 00:04:00,963 Do you think there would be a difference? 76 00:04:01,830 --> 00:04:02,730 Let's have a look. 77 00:04:04,110 --> 00:04:05,403 If I click run: 78 00:04:07,620 --> 00:04:10,926 I get False for everything. 79 00:04:10,926 --> 00:04:11,793 Hmm. 80 00:04:12,750 --> 00:04:14,550 So what's the difference here? 81 00:04:14,550 --> 00:04:19,550 Equals checks for the equality in value such as 1, 2, 3. 82 00:04:20,100 --> 00:04:23,160 Well, that's has the same value as 1, 2, 3 83 00:04:23,160 --> 00:04:24,577 in a list. 84 00:04:24,577 --> 00:04:29,160 "is" actually checks if the location and memory 85 00:04:29,160 --> 00:04:32,673 where this value is stored is the same. 86 00:04:34,620 --> 00:04:35,880 Hmm, let's go through that. 87 00:04:35,880 --> 00:04:38,790 So True is that 1. 88 00:04:38,790 --> 00:04:40,740 No, True is not 1. 89 00:04:40,740 --> 00:04:44,040 True is well, only True, right? 90 00:04:44,040 --> 00:04:45,630 That will be True. 91 00:04:45,630 --> 00:04:48,390 What about string one? 92 00:04:48,390 --> 00:04:49,800 Is that 1? 93 00:04:49,800 --> 00:04:50,957 No. 94 00:04:50,957 --> 00:04:55,957 I mean, for 1 to be a string, that needs to be 1 right? 95 00:04:56,460 --> 00:05:01,230 Because the one string isn't only in one place 96 00:05:01,230 --> 00:05:05,370 in memory, it's literally the exact same thing. 97 00:05:05,370 --> 00:05:06,750 What about this list? 98 00:05:06,750 --> 00:05:10,500 As a matter of fact, let's do, is array empty array? 99 00:05:10,500 --> 00:05:15,480 Is that, or is this list a list? 100 00:05:15,480 --> 00:05:20,013 Well, if we run this, I still get False. 101 00:05:20,940 --> 00:05:22,304 Hmm. 102 00:05:22,304 --> 00:05:25,920 And this is a little tricky and also advanced. 103 00:05:25,920 --> 00:05:30,920 Every time I create a list, it's added in memory somewhere. 104 00:05:31,050 --> 00:05:35,520 So this is in a location in memory, but whenever 105 00:05:35,520 --> 00:05:40,520 I create a new list, it's created in another location. 106 00:05:40,980 --> 00:05:45,574 So these are two completely different lists that live 107 00:05:45,574 --> 00:05:47,820 in different locations in memory. 108 00:05:47,820 --> 00:05:48,757 So it's going to check, 109 00:05:48,757 --> 00:05:52,110 "Hey, is this in the same memory space? 110 00:05:52,110 --> 00:05:53,756 Same bookshelf as that one? 111 00:05:53,756 --> 00:05:56,220 Nope, that's not it." 112 00:05:56,220 --> 00:06:01,220 Wait, why this work for things like numbers and strings? 113 00:06:01,470 --> 00:06:03,750 And that's because underneath the hood 114 00:06:03,750 --> 00:06:08,750 these are types that are very simple that are in memory 115 00:06:09,150 --> 00:06:11,730 but in one location. 116 00:06:11,730 --> 00:06:13,560 Versus something like a list. 117 00:06:13,560 --> 00:06:17,680 Even though this might be the same list with the same items 118 00:06:18,690 --> 00:06:20,730 because this is a data structure, 119 00:06:20,730 --> 00:06:25,694 every time we create it, it's created in a new location 120 00:06:25,694 --> 00:06:30,663 so that even if we have a variable A that contains this list 121 00:06:33,750 --> 00:06:35,400 and by the way, this will be the same 122 00:06:35,400 --> 00:06:40,400 for all our data structures, like dictionaries, sets, tops. 123 00:06:40,927 --> 00:06:45,927 If I do B equals this and I check A, is that B? 124 00:06:48,453 --> 00:06:52,410 Nope. They're created in a different memory space. 125 00:06:52,410 --> 00:06:56,130 So this where A points is in a different place 126 00:06:56,130 --> 00:06:58,050 than where B points, 127 00:06:58,050 --> 00:07:03,050 but if I do A == B, I get true 128 00:07:03,210 --> 00:07:08,210 because this double equality checks only the values. 129 00:07:08,675 --> 00:07:12,150 Now, this is a bit of a hard topic, 130 00:07:12,150 --> 00:07:14,610 so you might have to watch this video a couple times. 131 00:07:14,610 --> 00:07:17,580 You might have to practice this a few times yourself, 132 00:07:17,580 --> 00:07:19,950 but just keep in mind the difference between 133 00:07:19,950 --> 00:07:24,510 is and double equals, i-is tends to be a little stricter. 134 00:07:24,510 --> 00:07:28,410 You're checking for the exact thing that you're looking 135 00:07:28,410 --> 00:07:32,340 for versus equality, which checks the value. 136 00:07:32,340 --> 00:07:33,173 All right. 137 00:07:33,173 --> 00:07:34,350 Whew. That was a doozy. 138 00:07:34,350 --> 00:07:35,412 I'll see you in the next video. 139 00:07:35,412 --> 00:07:36,245 Bye-bye.