Making a decision with code can be boiled down into 2 steps: the first is to make a statement to compare data, and the second is to choose a course of action based on that comparison. This video will cover the first step of that process and we will be constructing statements that can only be either true or false. To do so, we will be using *comparative operators* and boolean operators.

Transcript

First, we're going to learn about Boolean logic and comparative operators, and we're going to learn them in a vacuum, which might make them seem a little bit weird and isolated, but it will make a lot more sense when we pair them with what we're going to learn in the rest of the chapter. This chapter is about decision making, and if you were presented with some options to make an important decision on, you wouldn't randomly choose one of the options. You would first examine and analyze the available information, and then make a decision based on that analysis. And that's what we're learning now, the comparing and analyzing part of decision making.

Let's start by learning comparative operators, which allow us to compare different pieces of information. They're super easy, and there's only six that you will ever need to remember. Follow along if you want, we aren't going to wire up any circuits or anything like that, we're just going to be using the shell in Thonny. We'll start easy with the equal to operator, which checks if two values are equal to each other. Our first operator is just two equal signs put next to each other, so if we write the statement five is equal to five, it's going to return true, because the statement is true. If I said five is equal to four, that's obviously going to return false. And these are the only two outcomes from everything we're going to be doing in this video. A statement can be boiled down to be either true or false, nothing else. And this is a fundamental aspect of something called Boolean logic.

Our second operator, which is not equals two, is just an exclamation mark followed by an equal sign. If I write the statement five is not equal to four, it obviously returns true. With five not equal to five returning false, and I think you can get the picture of this, I'm just going to move through these quickly. Our next operator is the less than operator. We can punch in five is less than four, which returns false. Our fourth operator is very similar, and it is the greater than operator, which is just the opposite of the last one. If I write five is greater than four, that obviously returns true. Our fifth operator is the greater than or equal to operator. This one is a little bit more tricky, because it's so similar to the last one, with one little tweak. The difference lies in that if I do five is greater than five, it's going to return false. But if I said five is greater than or equal to five, it's going to return true, because it's or equal to. And in every other situation, they work the same. So five is greater than or equal to four returns true. And our final operator is just the opposite of the last one, less than or equal to. So five is less than or equal to five, obviously true. So that is all six of our comparative operators that exist in MicroPython, and most coding languages actually. But the way we've used them here is really weird. We know that five is greater than four, and we don't need to use MicroPython and A pico to figure that out. How you will more commonly use these comparators is with variables. For example, I could do x is equal to 10, and then I could make a check, is x greater than five? And that returns true. And in our situation here, x could be some sensor data that we're checking and evaluating. And that brings up a big rookie mistake. If I say x is equal to 10 with one equal sign, I'm assigning 10 to the variable x. And if I say x is equal to 10 with two equal signs, I am checking, is x equal to 10? That's the comparative operator, and it's a fairly common beginner syntax mistake. Final thing for comparative operators, you can chain them together. So I could say five is equal to five is equal to however many fives, and it's going to return true. And this is a really handy one, but you can check if a variable is within a certain range. So I can say, is x greater than five, but less than 20? And that's obviously going to return true. We can check if it's in a certain range. So now we know how to compare things to each other. There is one more thing to learn in this, and that is comparing comparisons to each other. Kind of like one layer up from what we've been doing. There's only three things in this that you need to know. The and operator, the or operator, and the not operator. And these are called Boolean operators, but you might also hear them be called logical operators as well.

Let's start with and. If I write the statement five is equal to five and four is equal to four, it's going to return true because both of those statements are true. If I wrote five is equal to five and four is equal to three, it's going to return false. And this is how the and operator works. If the statement on the left and the right side of the operator is true, then it will return true. If one or both of them are false, then it will return false. The next one is the or operator, and it works very similarly, but only one of the statements on either side of it needs to be true. For example, if we plugged in five equals five, or four equals three, it's going to return true. If both of them are false, it returns false. If both of them are true, it returns true. As long as one of them is true, it's going to return true. The final one is the not operator, and it's kind of like an inversifier for statements. So it will turn true statements false and false statements true. For example, if we said five is equal to four, but we said not five equals four, it's going to return true. And it obviously works the other way around as well. Like comparative operators, you can also chain these together. For example, I've got not five equals four, or four equals three, which returns true because the left side becomes true with the not out the front, and then the or evaluates both of those sides, and one is true, so it returns true. You can also use brackets to help chain everything together. For example, I've got the and in a set of brackets here with a not out the front, and this kind of notifies everything in the brackets. And you can go ham with this and chain many, many things together. Congratulations, you now know the six comparative operators and the three Boolean operators that make up some of the fundamental building blocks of intelligence. And with these blocks, if you get creative and chain enough of them together, you can construct any piece of logic or statement or analysis of information that you can think of. And in the coming videos, we're going to start applying this with some actual code.

All right, three things to remember. One, there are six comparative operators. Equals to, not equals to, greater than, less than, greater than or equals to, and less than or equals to. Two, there are three Boolean operators, and, or, and not. And three, you can chain these together into complex statements that ultimately will return a single piece of Boolean logic. Whether a statement will be true or false.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.