Home -> Objects -> Booleans


Definition: A boolean is an object that has value True or False. (True=1 and False=0.)

Booleans are often used to control how code is used in your program via conditionals.

  • wikipage: if statements to determine whether a block of code should be evaluated.
  • wikipage: while loops to determine when to end the loop
  • wikipage: for loops to determine which values of the counter should be evaluated

EXAMPLES ON MY SAGE PAGE: Conditionals in Sage


Boolean Operators - Using these operators yields a value: True or False

Operator on objects Description
x == y Returns True if the two objects are equal and False if they are not equal.  Notice that == performs a comparison and not an assignment like = does.
x <> y Returns True if the objects are not equal and False if they are equal.
x != y Returns True if the objects are not equal and False if they are equal.
x < y Returns True if the left object is less than the right object and False if the left object is not less than the right object.
x <= y Returns True if the left object is less than or equal to the right object and False if the left object is not less than or equal to the right object.
x > y Returns True if the left object is greater than the right object and False if the left object is not greater than the right object.
x >= y Returns True if the left object is greater than or equal to the right object and False if the left object is not greater than or equal to the right object.
Operator on booleans Description
b1 and b2 Returns True if both booleans are true and False otherwise.
b1 or b2 Returns True if either or both booleans are true and False only if both are false.
not b1 Returns True if boolean is false and False if boolean is true.

Reference: Sage_for_Newbies, pp.28-33 (Ted Kosan)


Keywords: sage, objects, booleans, control, loops, conditionals, conditions, if, else, while, for