1 00:00:00,940 --> 00:00:07,270 It's time to talk about another topic that sounds a lot more confusing, Short-circuiting than it actually 2 00:00:07,270 --> 00:00:07,500 is. 3 00:00:07,510 --> 00:00:08,920 It's quite simple. 4 00:00:10,420 --> 00:00:16,870 Short-circuiting looks something like this, remember when I taught you the word and so we can use, 5 00:00:17,560 --> 00:00:20,500 let's say, for example, is friend? 6 00:00:23,090 --> 00:00:31,080 He calls the true and let's say is user xcode the true? 7 00:00:32,170 --> 00:00:39,460 And I can do an expression here saying is a friend and is user. 8 00:00:41,890 --> 00:00:47,020 Now, in here, when we run this, let's print it here just so we can see. 9 00:00:48,390 --> 00:00:49,710 If I run this. 10 00:00:50,750 --> 00:00:55,460 I get true because both of these evaluate to true. 11 00:00:56,560 --> 00:01:03,040 So that if I had this as an F block, let's say, if it was if his friend and is user. 12 00:01:04,150 --> 00:01:04,640 Print. 13 00:01:05,530 --> 00:01:07,690 Best friends forever. 14 00:01:09,300 --> 00:01:18,510 Now, short circuiting can be done when we use something like four, which we haven't seen before, 15 00:01:18,510 --> 00:01:21,560 but again, you can see here that it's highlighted in blue. 16 00:01:21,570 --> 00:01:30,180 So it is a key word, a python key word now or says if either one of these conditions are true, then 17 00:01:30,180 --> 00:01:32,230 run this piece of code. 18 00:01:32,730 --> 00:01:34,050 So if I run this. 19 00:01:35,220 --> 00:01:37,200 I get best friends forever. 20 00:01:38,760 --> 00:01:45,570 Now, let me ask you this, which operation do you think is more performant the or or the end? 21 00:01:46,080 --> 00:01:47,010 Is there a difference? 22 00:01:48,130 --> 00:01:55,930 As a matter of fact, there is because of short circuiting, you see, when I do or hear, because I 23 00:01:55,930 --> 00:01:58,810 only care if either one of this is true. 24 00:01:59,890 --> 00:02:02,290 The Python interpreter is going to say if. 25 00:02:03,370 --> 00:02:09,790 Is friend, and it's going to evaluate that and say, oh, this is this is true, and then it's going 26 00:02:09,790 --> 00:02:14,560 to see the word or and it's going to say, hey, I don't care what this is. 27 00:02:14,590 --> 00:02:18,910 Maybe this is a really heavy operation or something that I need to look up. 28 00:02:19,130 --> 00:02:22,060 Well, I don't really care because this is already true. 29 00:02:22,060 --> 00:02:28,240 Whether this is true or this is false, I don't really care, because either way, I'm going to have 30 00:02:28,240 --> 00:02:28,900 to print this. 31 00:02:29,290 --> 00:02:32,710 As you can see, it's still prints best friends forever. 32 00:02:33,920 --> 00:02:40,970 So Short-circuiting happens when the interpreter is smart enough to say, I already did enough work, 33 00:02:40,970 --> 00:02:44,030 I don't care what this is, this is just wasted work if I do it. 34 00:02:44,030 --> 00:02:50,300 So I'm going to short circuit and say, hey, I'm going to just ignore this and I'm going to start printing. 35 00:02:51,020 --> 00:02:54,460 So using or is a little bit more efficient. 36 00:02:54,590 --> 00:02:56,780 But again, it depends on what you need. 37 00:02:57,200 --> 00:03:03,110 If you need to check if both its friend and is users true, you'll have to use and. 38 00:03:04,600 --> 00:03:07,480 But this works both ways with and as well. 39 00:03:07,930 --> 00:03:15,940 For example, if its friend is false, this is going to get short circuited because I'm going to say, 40 00:03:15,940 --> 00:03:23,770 hey, if its friend is false, well, I don't really care what is user is, because there's no way that 41 00:03:23,770 --> 00:03:24,760 both of these are true. 42 00:03:24,790 --> 00:03:26,670 I've already seen that one of them is false. 43 00:03:26,920 --> 00:03:30,610 So I'm going to ignore this and I'm going to print this. 44 00:03:30,730 --> 00:03:34,660 And when I say I, I mean our good old friend, Python interpreter. 45 00:03:35,810 --> 00:03:42,650 Now, this topic of short circuiting doesn't really get taught in beginner python courses, but now 46 00:03:42,650 --> 00:03:45,110 if you hear this, maybe you hear this in an interview. 47 00:03:45,660 --> 00:03:50,390 As you can see, it sounds a lot more difficult than it actually is. 48 00:03:50,390 --> 00:03:52,960 And frankly, it makes a lot of sense. 49 00:03:52,970 --> 00:03:53,170 Right. 50 00:03:53,780 --> 00:03:59,050 We want our machines to be efficient and it is quite efficient due to short circuiting. 51 00:03:59,900 --> 00:04:01,700 I'll see in the next one by.