Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Add Parentheses (Solution)

00:00 The first expression read False == not True. And we can actually try that out. So if you would just evaluate it like this, you actually get an SyntaxError.

00:12 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.

00:34 That’s why you’re getting the error. So what you’ll need to do is make sure that Python evaluates not True before it does the Boolean comparator == here, and it can do that using a parenthesis.

00:46 Put these two in parentheses and say False == (not True), where first Python evaluates not True to False and then compares False == False, which then evaluates to True.

01:01 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.

01:29 The problem here probably is again that it starts off with evaluating False == True because here you have a Boolean comparator, and these are higher in operator precedence.

01:38 So False == True is going to return False because False is not the same as True. And then it moves on from left to right. True and False is going to be False, and False and False should be True.

01:51 Let me try that out. Looks like I made a logical mistake in here. I said that True and False == True would evaluate to False and False.

02:06 Then we have True and False is False,

02:13 and 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.

02:27 False, there we go, yeah, False and False evaluates False. So I did the right steps. I just had a little mind block in what False and False evaluates to.

02:38 So, you see, first it does this. The Boolean comparator makes it False, then it goes from left to right, does True and False to False and then False and False again to False.

02:50 And that’s a lot of False. So let’s see whether we can turn it True using some parentheses.

02:57 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 True and 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:18 In this case, it’s going to be False == False, and that should be True. Let’s try that. Yep, there you go. Managed to get there in the end. So this can be tricky to think about.

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?

03:42 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 True.

04:08 Let’s look at what gets evaluated first. So we have, again, a conditional expression here. "A" == "B", so this is going to get evaluated first.

04:17 And this should be False because the string "A" is not the string "B".

04:23 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 False.

04:44 So this is where I get to the False here again. Okay, so what’s the way to solve this to turn it True? Let me see.

04:54 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.

05:09 And in this case, we’re going to have "A" == "B" being False and then True and False being False and then not False is True.

05:17 Let me write it out just like we’ve done so far.

05:21 not (True and False),

05:26 and then it’s going to be not False, and that should be True.

05:32 Okay, give it a try.

05:37 Here we are. Okay, great. So we managed to turn all of these expressions to True.

05:44 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.