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 Logic (Solution)

00:00 Here’s our first expression. You can see that there’s two parts that are in parentheses. So we have the first one that reads (1 <= 1), and then we have another one that reads (1 != 1).

00:12 So the first one, (1 <= 1), I’m going to say that this evaluates to True because it’s true that 1 is equal to 1, so it’s smaller or equal to 1. And then the second one, (1 != 1), that’s going to evaluate to False because 1 is equal to 1. So we have True and False.

00:33 And then now I can think about what does True and False evaluate to, and we’re using the logical operator and in here, and and is only True if both the left side and the right side are True. So in this case, this is going to evaluate to False. Let’s test it out.

00:53 Okay, so it evaluates to False. Perfect. Let’s move on to the next one. Next one is not and then in parentheses (1 != 2).

01:03 Let’s do the same thing as before. In parentheses, (1 != 2) evaluates the True. So this is basically not True, and not True should be False. Let’s try it out. Okay, that’s correct, evaluates to False.

01:22 The next one, we’re going to have in parentheses, ("good" != "bad") or False. We copy that. Okay, so again, I want to evaluate the piece in parentheses first.

01:41 So I’m going to look at this, and it says the string "good" is not equal to the string "bad". So that’s True. This means that our new expression looks like this, True or False.

01:54 And then we know if we use the Boolean operator or, then it is True if either one on the left or on the right side is True.

02:03 And in this case, we have a True on the left side. So, the overall value of this should be True. Let’s try it out. Great, we’re getting True. And then we have one more that is a little similar.

02:15 So let me just make my life a bit easier and I have to type less. ("good" != "Good"), and then it continues with and not

02:27 (1 == 1).

02:31 We have two pieces in parentheses. So I’m going to evaluate these two first. We’re going to look at this that says, that reads, the string "good" lowercase is not equal to the string "Good" capitalized. So this is True. I’m going to say True, and then I keep the Boolean comparator here. I’m going to say and not, and this part here, (1 == 1), evaluates to True.

02:54 Now you have to think about operator precedence. Here we have an and and the not here. And if you remember, the not evaluates before the and.

03:02 So we have not True is our next one to evaluate. So this should come to True and False because not True evaluates to False. And then we have True and False. Again, we know that and is only True if both the left and the right side is True.

03:19 So in this case, that should evaluate to False. Let’s try it out.

03:26 And there you go. So, here you see the reasoning steps that I went through when evaluating these more complex expressions here that involve both Boolean comparators and logical operators. And yeah, you can just go piece by piece, start with the parentheses, then kind of keep the operator precedence in mind and then evaluate them down until you come to a final value. This is also what the computer does.

03:47 So, you’re just kind of following in the footsteps of how Python evaluates these expressions to come to a conclusion here. And

03:57 here we are back at the slide. Just to conclude the exercise, here’s True and False marked next to the expression. So the first two were False, third one is True, and the fourth one is False again.

Become a Member to join the conversation.