In computer's programming we also have another kind of operators, called logical operators, they are used in logical expressions and just return true or false, take a look at the following tables with the most used arithmetical and logical operators in computer programming.
Arithmetical:
** Exponentiation
+x, -x Positive, Negative
*, /, % Multiplication, Division, Remainder
+, - Addition, Subtraction
You should at least know the majority of the operators above, the only one that a few people may know is the Remainder (%), it returns the remainder of a division, for example: 3 % 2 would return 1 because 3 divided by 1 is 1 and 1 remains, the operator % returns the reminder of a division.
The operators above are processed from top to bottom (using the above table order), so the expression:
1 + 3 * 2
Is processed in the following order: first 3 * 2 that will result in 6, then 6 + 1 that will result in 7, test it, open up a Python shell and type? 1 + 3 * 2.
And as I'm a nice guy, I'll not make you remember the order of the operators (like some teachers that never did real code in their life do), there is a way for you to organize the expression so it executes in the order that you want, using parenthesis, like in mathematics, the most internal parenthesis are processed first, so if we want the sum to be processed before the multiplication we need to write our expression like this:
(1 + 3) * 2
Type the above code in Python and see what happens
Logical:
or Boolean operator OR
and Boolean Operator AND
not Boolean Operator NOT
<, <=, >, >=, !=, == Comparisons
Like we have learned in our previous lesson a Boolean variable have just 2 possible values (true or false), logical operators are used in logical expressions and they always return true or false, take a look at the expression:
5 > 3
The above expression is a logical expression and returns true, because its true that 5 is bigger than 3, now a little more complex expression:
5 > 3 ou 3 > 4
The expression above would return false too, because the operator OR returns true if one of the two expressions is true.
The following expressions return false:
10 != 10 -> is 10 different from 10? false!
10 > 10 -> is 10 bigger 10? false!
10 == 11 -> is 10 equals 11? false!
Like we have learned before, true or false to the computer means 1 (true) or 0 (false), on and off, so to process this expressions, the computer multiplies or sum, depending on the operator, it looks complicated at first sight but to help you understand I made some tables, take a look:
Operator AND
1 0 | 1 * 0 = 0 -> false
0 * 1 = 0 -> false
1 * 1 = 1 -> true
0 * 0 = 0 -> false
So for the operator AND return true, both variables need to be true.
Operator OR
1 + 0 = 1 -> true
0 + 1 = 1 -> true
1 + 1 = 1 -> true
0 + 0 = 0 -> false
It the OR operator's case, its necessary just one of the variables to be true, the expression will be false just when both variables are false, what is exactly the contrary of the AND operator.
Operator NOT
The operator not just returns the inverse value of the Boolean variable, so not true is false, and not false is true, as we see bellow:
not 1 -> 0
not 0 -> 1
There are more logical and arithmetical operators, but we are not studying them right now, this lesson is just a introduction about the most used operators, if you want to read more about it, take a look here
And as always, if you have any questions, suggestions or corrections, please comment!
2 comentários:
Hi, I think you have some little typos on this article :)
When you list the OR table
1 + 0 = 1 (not 0 as you typed)
0 + 1 = 1 too (1 is true, as you said below)
1 + 1 = 1 (boolean sum can only return 1 or 0 as a value, 2 is incorrect, hehehe :) )
I think that's all, so far, it's a great blog this, keep it up man :)
thanx for the corrections, I might have been drunk when I wrote that :)
Postar um comentário