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 too 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 printed 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 F block let's say if it was if is friend and is user print.
Best friends forever now.
Short circuiting can be done when we use something like for which we haven't seen before but again you
can see here that it's highlighted in blue so it is a keyword.
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 performance.
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 these is true the Python interpreter
is going to say if it's 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 true you'll have to use and 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 his 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 ole 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 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 in the
next one.
Bobby.