Boolean Expressions
From www.norsemathology.org
Contents |
Boolean Expressions
Boolean expressions are logical expressions that evaluate to either true or false. Boolean expressions use comparison operators such as less than or greater than and can be joined together using logic operators such as and, or.
Basic form of a boolean expression:
value comparison_operator value
Boolean Values
Sage
- True
- False
Note: These are case sensititve
Comparison Operators
Below is a list of the basic comparison operators for both Mathematica and Sage.
- < less than
- > greater than
- <= less than or equal to
- >= greater than or equal to
- == equal to
- != not equal to
Differences between Python and Sage
If you want to determine if x is greater than zero and less than or equal to 10 i.e. 0 < x < = 10
In Python the code would look as follows:
x > 0 and x <= 10
In Sage you can also use the code shown below:
0 < x <= 10
Both expressions return the same boolean value of True or False depending on the value of x.
Logic Operators
Sage
- and : returns true if the expression to it's left and right are both true
- or : returns true if either the expression to it's left or the expression to it's right is true.
Examples:
x > 0 and y < 10 with x=1,y=5 returns True with x = 0,y = 5 returns False x > 0 or y < 10 with x=1,y=5 returns True with x = 0,y = 5 returns True with x = 0,y=11 returns False
Mathematica
- && : logical and, returns true if the expression to it's left and right are both true
- || : logical or, returns true if either the expression to it's left or the expression to it's right is true
Predicates
Predicates are functions that return either True or False
Sage
Sage has a large list of built-in predicates below is a list of some of the more common ones.
- is_Integer - returns True if argument is an integer e.x. is_Integer(10) returns True
- is_RealNumber - returns True if argument is a real number (Note: returns false if argument is an integer) e.x. is_RealNumber(10.0) returns True
- is_prime - returns True if argument is a prime number. e.x. is_prime(5) returns True
- is_even - returns True if argument is an even number. e.x. is_even(4) returns True
- is_odd - returns True if argument is an odd number. e.x. is_odd(5) returns True
Sage contains many other predicates such as is_AffineSpace or is_pseduoprime. Sage predicates all start with is_ so to get a complete list of type predicates type is_ into a cell and press the tab key.