1 00:00:00,990 --> 00:00:01,823 Instructor: It's time to talk 2 00:00:01,823 --> 00:00:04,950 about another topic that sounds a lot more confusing, 3 00:00:04,950 --> 00:00:07,500 short circuiting than it actually is. 4 00:00:07,500 --> 00:00:09,063 It's quite simple. 5 00:00:10,410 --> 00:00:12,960 Short circuiting looks something like this. 6 00:00:12,960 --> 00:00:15,990 Remember when I taught you the word and 7 00:00:15,990 --> 00:00:17,550 so we can use... 8 00:00:17,550 --> 00:00:21,863 let's say for example, is friend equals to true. 9 00:00:24,780 --> 00:00:29,780 And let's say is user is equal to true. 10 00:00:32,220 --> 00:00:34,740 And I can do an expression here 11 00:00:34,740 --> 00:00:39,633 saying is friend and is user. 12 00:00:41,910 --> 00:00:44,820 Now in here when we run this 13 00:00:44,820 --> 00:00:47,133 let's print it here just so we can see. 14 00:00:48,420 --> 00:00:52,440 If I run this I get true 15 00:00:52,440 --> 00:00:55,593 because both of these evaluate to true. 16 00:00:56,550 --> 00:00:59,520 So that if I had this as an if block, 17 00:00:59,520 --> 00:01:03,153 let's say if it was if is friend and is user. 18 00:01:04,140 --> 00:01:07,803 Print best friends forever. 19 00:01:09,300 --> 00:01:14,070 Now short circuiting can be done when we use 20 00:01:14,070 --> 00:01:18,480 something like or, which we haven't seen before. 21 00:01:18,480 --> 00:01:21,570 But again, you can see here that it's highlighted in blue. 22 00:01:21,570 --> 00:01:25,380 So it is a keyword, a Python keyword. 23 00:01:25,380 --> 00:01:29,910 Now or says if either one of these conditions are true 24 00:01:29,910 --> 00:01:32,730 then run this piece of code. 25 00:01:32,730 --> 00:01:37,323 So if I run this, I get best friends forever. 26 00:01:38,760 --> 00:01:40,140 Now let me ask you this. 27 00:01:40,140 --> 00:01:43,200 Which operation do you think is more performant? 28 00:01:43,200 --> 00:01:46,080 The or or the end? 29 00:01:46,080 --> 00:01:47,180 Is there a difference? 30 00:01:48,180 --> 00:01:51,960 As a matter of fact, there is because of short circuiting. 31 00:01:51,960 --> 00:01:55,260 You see when I do or here, 32 00:01:55,260 --> 00:01:58,953 because I only care if either one of these is true. 33 00:01:59,910 --> 00:02:01,830 The Python interpreter is going to say 34 00:02:01,830 --> 00:02:06,090 if is friend and it's going to evaluate that 35 00:02:06,090 --> 00:02:09,150 and say, oh, this is, this is true. 36 00:02:09,150 --> 00:02:11,220 And then it's going to see the word or 37 00:02:11,220 --> 00:02:14,580 and it's going to say, hey, I don't care what this is. 38 00:02:14,580 --> 00:02:16,860 Maybe this is a really heavy operation 39 00:02:16,860 --> 00:02:19,140 or something that I need to look up. 40 00:02:19,140 --> 00:02:22,050 Well I don't really care because this is already true. 41 00:02:22,050 --> 00:02:26,700 Whether this is true or this is false, I don't really care 42 00:02:26,700 --> 00:02:29,370 because either way I'm going to have to print this. 43 00:02:29,370 --> 00:02:32,853 As you can see, it still prints best friends forever. 44 00:02:33,930 --> 00:02:37,500 So short circuiting happens when the interpreter 45 00:02:37,500 --> 00:02:41,010 is smart enough to say, I already did enough work. 46 00:02:41,010 --> 00:02:42,090 I don't care what this is, 47 00:02:42,090 --> 00:02:44,010 this is just wasted work if I do it. 48 00:02:44,010 --> 00:02:47,130 So I'm going to short circuit and say, hey, 49 00:02:47,130 --> 00:02:48,570 I'm going to just ignore this 50 00:02:48,570 --> 00:02:51,030 and I'm going to start printing. 51 00:02:51,030 --> 00:02:54,630 So using or is a little bit more efficient. 52 00:02:54,630 --> 00:02:57,210 But again, it depends on what you need. 53 00:02:57,210 --> 00:02:58,830 If you need to check 54 00:02:58,830 --> 00:03:01,860 if both is friend and is user is true, 55 00:03:01,860 --> 00:03:03,213 you'll have to use and. 56 00:03:04,620 --> 00:03:07,623 But this works both ways with and as well. 57 00:03:08,753 --> 00:03:11,850 For example, if is friend is false, 58 00:03:11,850 --> 00:03:14,490 this is going to get short circuited 59 00:03:14,490 --> 00:03:19,490 because I'm going to say, hey, if is friend is false, 60 00:03:19,830 --> 00:03:22,500 well I don't really care what is user is 61 00:03:22,500 --> 00:03:24,750 because there's no way that both of these are true. 62 00:03:24,750 --> 00:03:26,940 I've already seen that one of them is false. 63 00:03:26,940 --> 00:03:30,780 So I'm going to ignore this and I'm going to print this. 64 00:03:30,780 --> 00:03:32,130 And when I say I, 65 00:03:32,130 --> 00:03:34,773 I mean our good old friend Python interpreter. 66 00:03:35,820 --> 00:03:39,420 Now this topic of short circuiting doesn't really get taught 67 00:03:39,420 --> 00:03:41,700 in beginner Python courses. 68 00:03:41,700 --> 00:03:43,560 But now if you hear this 69 00:03:43,560 --> 00:03:45,720 maybe you hear this in an interview. 70 00:03:45,720 --> 00:03:48,960 As you can see it sounds a lot more difficult 71 00:03:48,960 --> 00:03:50,400 than it actually is. 72 00:03:50,400 --> 00:03:53,790 And frankly it makes a lot of sense, right? 73 00:03:53,790 --> 00:03:55,440 We want our machines to be efficient 74 00:03:55,440 --> 00:03:59,940 and it is quite efficient due to short circuiting. 75 00:03:59,940 --> 00:04:01,030 I'll see you in the next one. 76 00:04:01,030 --> 00:04:01,863 Bye bye.