Difference between revisions of "Boolean"
(New page: A '''boolean''', named after George Boole, is an entity that can only hold the values '''true''' and '''false'''.) |
m (Added Categories) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | A '''boolean''' | + | Boolean logic, named after [[George Boole]], is a complete system of logical operations. A '''boolean''' is a primitive [[datatype]] in almost all programming languages that can represent the values true and false in logical expressions. They are used primarily in conditional tests. In [[Python]] the test in the first line of an if statement or while loop must evaluate to a boolean, and the reserved words <tt>True</tt> and <tt>False</tt> (note the capitalization) are constants representing the two possible values of a boolean. |
+ | |||
+ | All the [[relational operators]] return booleans. | ||
+ | |||
+ | ==Boolean Operations== | ||
+ | *<tt>x '''and''' y</tt> will return true [[iff]] both x and y are true. | ||
+ | *<tt>x '''or''' y</tt> will return true iff either x or y is true (or both). | ||
+ | *<tt>'''not''' x</tt> will return true iff x is false (i.e. it negates x). | ||
+ | Operations can be chained, and if necessary precedence can be forced with parentheses. For example, | ||
+ | (x and not y) or (z and not y) | ||
+ | will return true iff y is false and at least one of x and z are true. | ||
+ | |||
+ | Also note that <tt>and</tt> and <tt>or</tt> are '''short-circuit''' operators. This means that if the value of the first operand is sufficient to determine the result, the second is never evaluated. That is, if the first operand of an <tt>and</tt> statement is false, or if the first operand of an <tt>or</tt> statement is true, the second operand won't be checked. For example, | ||
+ | (5 in myList) and (myList.index(5) < 3) | ||
+ | will never give an error, because if 5 isn't in myList the second half of the statement will never be evaluated (normally, if you called index for something that isn't in a list you'd get an error, but that can never happen in this statement because in that case it would short-circuit). | ||
+ | |||
+ | [[Category:Datatypes]] | ||
+ | [[Category:Introduction to Programming]] | ||
+ | [[Category:Logic]] |
Latest revision as of 11:06, 28 September 2024
Boolean logic, named after George Boole, is a complete system of logical operations. A boolean is a primitive datatype in almost all programming languages that can represent the values true and false in logical expressions. They are used primarily in conditional tests. In Python the test in the first line of an if statement or while loop must evaluate to a boolean, and the reserved words True and False (note the capitalization) are constants representing the two possible values of a boolean.
All the relational operators return booleans.
Boolean Operations
- x and y will return true iff both x and y are true.
- x or y will return true iff either x or y is true (or both).
- not x will return true iff x is false (i.e. it negates x).
Operations can be chained, and if necessary precedence can be forced with parentheses. For example,
(x and not y) or (z and not y)
will return true iff y is false and at least one of x and z are true.
Also note that and and or are short-circuit operators. This means that if the value of the first operand is sufficient to determine the result, the second is never evaluated. That is, if the first operand of an and statement is false, or if the first operand of an or statement is true, the second operand won't be checked. For example,
(5 in myList) and (myList.index(5) < 3)
will never give an error, because if 5 isn't in myList the second half of the statement will never be evaluated (normally, if you called index for something that isn't in a list you'd get an error, but that can never happen in this statement because in that case it would short-circuit).