7 Logical Operator
-
Logical operators are identical in both languages
-
Logical comparisons are different
7.1 Logical operation: R & Python
Operator | Explanation | Example |
---|---|---|
== | X equals Y? | X == Y |
!= | X NOT equals Y? | X != Y |
< | X less than Y? | X < Y |
<= | X less than OR equals Y? | X <= Y |
> | X greater than Y? | X > Y |
>= | X greater than or equals Y? | X >= Y |
7.2 Logical comparison: R
Operator | Meaning | X | Y | Decision | Example |
---|---|---|---|---|---|
& |
AND |
TRUE | TRUE | TRUE | X & Y |
& |
AND |
TRUE | FALSE | FALSE | X & Y |
& |
AND |
FALSE | TRUE | FALSE | X & Y |
& |
AND |
FALSE | FALSE | FALSE | X & Y |
| | OR |
TRUE | TRUE | TRUE | X | Y |
| | OR |
TRUE | FALSE | TRUE | X | Y |
| | OR |
FALSE | TRUE | TRUE | X | Y |
| | OR |
FALSE | FALSE | FALSE | X | Y |
Use !
as a logical NOT operator. It is a unary operator that is used before a single operand.
7.3 Logical comparison: Python
Operator | Meaning | X | Y | Decision | Example |
---|---|---|---|---|---|
and |
AND |
TRUE | TRUE | TRUE | X and Y |
and |
AND |
TRUE | FALSE | FALSE | X and Y |
and |
AND |
FALSE | TRUE | FALSE | X and Y |
and |
AND |
FALSE | FALSE | FALSE | X and Y |
or |
OR |
TRUE | TRUE | TRUE | X or Y |
or |
OR |
TRUE | FALSE | TRUE | X or Y |
or |
OR |
FALSE | TRUE | TRUE | X or Y |
or |
OR |
FALSE | FALSE | FALSE | X or Y |
Use not
as a logical NOT operator. It is a unary operator that is used before a single operand.
In Python:
&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR