Building Complex Expressions
00:00 It’s all very well to read and to listen and absorb all this information about Boolean comparators, logical operators, operator precedence, but until you actually get your hands dirty and see it in action, it’s never really going to sink in. So in this lesson, you’re just going to look at a couple more examples of some more complex expressions.
00:24
So the first example reads True and not
(1 != 1)
. So what do you think this will evalute to overall? Okay, so you had that expression typed out as a comment.
00:41
And that’s just not to give the game away. We want to try and work out what this is without actually executing it first. So how would you go about understanding a complex expression like this? Well, let’s break it up into little pieces. You know that operations in brackets (()
) evaluate first.
01:00
So what does 1 != 1
evaluate to? 1 != 1
gives False
because 1
is equal to 1
. So now you can rewrite the expression as True and not
False
. So what’s next—and
or not
? not
is next. What does not False
evaluate to?
01:26
Remember, it just inverts the value. So that would be True
. So now you can rewrite expression True
and True
. What would that give you?
01:35
What does and
need to evaluate to True
?
01:39
It needs both sides to be true. So if you do True and True
, you’ll get True
.
01:47
So now take a look at example number 2. This one reads ( "a" != "a") or not (2 >= 3)
Take a quick look at this and go to the REPL and see if you can work it out.
02:09 Okay, as before, you’ve got a comment here, so you can see the whole expression without evaluating it and giving the game away. So here you have a complex expression.
02:18
How do you break it up? Well, you start off with the things in brackets. Look at the first one. "a" != "a"
. It gives you False
because the string "a"
is equal to the string "a"
. So now you can rewrite the whole expression as False
or not (2 >= 3)
.
02:48
So what’s next? It’s the other expression in brackets: 2 >= 3
. So does that expression evaluate to True
or False
? Is 2
greater than or equal to 3
? No, that’s wrong. 2
is not greater than or equal to 3
. It’s less than 3
, so that will give you a False
value.
03:07
So now you can rewrite the whole expression as False or not False
. So what comes first, or
or not
? not
comes first, so not False
. What’ll that give you?
03:18
It inverts the value, so it gives you True
. Now you can write the final expression as False
or True
. So what does or
need for it to give you a True
value or False
value?
03:35
or
only needs one side to be true or both sides to be true, and it’ll give you a True
value. So if we just type that out, you’ll see that now you get a True
value.
03:48
So let’s just copy the whole expression to verify that. And indeed, it is True
.
03:57 You just went through a couple of examples showing you how to figure out step by step how a complex expression evaluates. You’ve taken your knowledge of operator precedence and applied it to these expressions to see which ones go first and how it reduces into its final value.
04:16 To really internalize all of this, the best thing is to practice. Come up with your own complex expressions, use variables to store the values of expressions, assign the results of expressions to variables, mix and match.
04:34 Come up with interesting chains of Boolean comparators and logical operators. Understanding is not enough in programming. To build fluency, you need to practice.
Become a Member to join the conversation.