Add Parentheses (Solution)
It’s even an invalid syntax, so it doesn’t evaluate at all. So now what do you need to change to solve that and avoid that error? Keep in mind that Boolean competitors are the first ones to evaluate, before the logical operators. So in this case, Python is trying to say
False == not before it does anything with
True over here. And this is invalid index.
And that is our task. So let’s try it out.
False == (not True) in parentheses, evaluates to
True. Perfect. The next one we had here was
True and False ==
True and False. Again, let’s try it out what this currently does without any parentheses. Okay, so currently it does evaluate, but it evaluates to
False, and you should change this so that it evaluates to
True. Let me see what we can do here.
False == True is going to return
False is not the same as
True. And then it moves on from left to right.
False is going to be
False should be
False would be true. No!
False and False. Okay, so here’s my problem. Just a thinking blob about
False and False evaluating to
True, but this obviously evaluates to
False. Let’s double-check.
If we use
True and False == True and False, if you do the Boolean comparator at the end, if I put a parenthesis here and then evaluate
False first, which is going to be
False, and then I can do the same thing over here because these are actually the same expressions, and then I’m just comparing whether these two expressions are the same.
03:31 You’ve got to keep a lot of different pieces in mind. What’s the operator precedence? What do the different logical operators return? What values do these different pieces have on the left and the right side, et cetera?
So it’s easier to get tripped up as you saw me getting tripped up there, but we got to the
True in the end. Let’s try the final one, which read
not True —and now we’re adding some strings to the mix—
"A" == "B". Just like before, I’m going to test out what this does without any changes. Again, we get a
False, and so we want to change that
False to be a
So it’ll look like this,
not True and False. And then we have a
not here that gets evaluated before the
and, which means that the next step is going look like this:
False and False. And as I’ve just learned before,
False and False evaluates the
True, so this is going to be
False, you said
True and False is also going to be
False, and then I can just turn it around. Okay, so I can put one set of parentheses here to make sure that everything here evaluates before the
not gets evaluated.
And here’s some solutions. You can turn all of them
True: by putting a parenthesis around
not True in the first one, putting a set of parentheses that is around each of those parts on the left and the right side of the
== sign, and here we just wrap everything after the
not into parentheses and then it also turns the overall expression to
True. All right, good job.
06:07 No worries if you tripped up a little. You saw I also tripped up a little. That’s also why we generally let Python do the calculations of these types of things, but it’s good to understand the steps that it goes through to actually evaluate them. Okay, and in the next one, let’s start with writing some more complex code.
Become a Member to join the conversation.