Raspberry Operator
Operators are symbols used to perform code operations in various expressions of programs, and operands are the objects of operators. For example, 2 > 3, where the operands are 2 and 3 and the operator is “>". Depending on different functions, operators can be divided into many kinds, such as arithmetic operators, comparison operators, etc. In this lesson, we will first learn about comparison and logical operators.
Comparison Operator
Comparison operators, also known as relational operators, are used to compare the values of constants, variables, or expressions. If the comparison is true, it returns True; otherwise, it returns False.
In fact, you would have seen them in various conditional judgment statements in the projects of previous lessons. For example, when we programmed a button to control the state of an LED in the last lesson, we used the comparison operator “==" to evaluate whether the return value of the button is 1:
1 while True:
2 val = BUTTON.value)
3 if val == 1:
4 LED.value(1)
5 else:
6 LED.value(0)
Logical operators
Logical operators can concatenate two or more simple statements to form even more complex statements. We often use them in all kinds of conditional statements.