It's time to talk about another topic that sounds a lot more confusing short circuiting than it actually
is.
It's quite simple.
Short circuiting looks something like this.
Remember when I taught you the word?
And so we can use.
Let's say, for example, is friend.
Equals to true and let's say is user is equal to true.
And I can do an expression here saying is friend and is user.
Now in here, when we run this, let's print it here just so we can see.
If I run this.
I get true because both of these evaluate to true.
So that if I had this as an if.
BLOCK Let's say if it was if it's friend and is user.
Print best friends forever.
Now short circuiting can be done when we use something like Awe, which we haven't seen before.
But again, you can see here that it's highlighted in blue.
So it is a key word, a python keyword now or says if either one of these conditions are true, then
run this piece of code.
So if I run this.
I get best friends forever.
Now, let me ask you this.
Which operation do you think is more performant?
The or or the end?
Is there a difference?
As a matter of fact, there is because of short circuiting.
You see, when I do or here because I only care if either one of this is true.
The Python interpreter is going to say if.
Is friend.
And it's going to evaluate that and say, oh, this is this is true.
And then it's going to see the word or and it's going to say, hey, I don't care what this is.
Maybe this is a really heavy operation or something that I need to look up.
Well, I don't really care because this is already true.
Whether this is true or this is false, I don't really care.
Because either way, I'm going to have to print this.
As you can see, it still prints best friends forever.
So short circuiting happens when the interpreter is smart enough to say, I already did enough work.
I don't care what this is.
This is just wasted work if I do it.
So I'm going to short circuit and say, Hey, I'm going to just ignore this and I'm going to start printing.
So using or is a little bit more efficient.
But again, it depends on what you need.
If you need to check, if both is friend and is user is true, you'll have to use end.
But this works both ways with and as well.
For example, if its friend is false, this is going to get short circuited because I'm going to say,
hey, if it's friend is false, well, I don't really care what is user is because there's no way that
both of these are true.
I've already seen that one of them is false.
So I'm going to ignore this and I'm going to print this.
And when I say I, I mean our good old friend, Python interpreter.
Now this topic of short circuiting doesn't really get taught in beginner python courses, but now if
you hear this, maybe you hear this in an interview.
As you can see, it sounds a lot more difficult than it actually is.
And frankly, it makes a lot of sense, right?
We want our machines to be efficient and it is quite efficient due to short circuiting.
I'll see you in the next one.
Bye bye.