Skip to main content
Chemistry LibreTexts

4: Boolean

  • Page ID
    430696
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Boolean Data Types

    Boolean expressions are expressions that can result in only one of two possible answers, true or false. These results in a special data type, where Boolean values associate a value of zero for a false statement and a value of 1 for a true statement. Boolean algebra had it origins in the 1800's and George Boole's book "The Mathematical Analysis of Logic, Being an Essay Towards a Calculus of Deductive Reasoning" that is available through the Gutenberg Project. Boolean data types are essential to computational reasoning because they can result in logical reasoning based outcomes based on the truth (or falseness) of a statement or condition in terms of binary code. For example, you could have an if statement branch your programming logic based on the conditions resulting from a comparison operator that decides if one number is greater than another.

    clipboard_e7271918f8759a76b9cb64fb783c4241e.pngFigure \(\PageIndex{1}\): In binary code a value of zero equates to a false statement and a value of 1 equates to a true statement. (Belford cc 0.0)

    There are several types of operators that evaluate to boolean values (true or false) and these include Boolean Operators and Comparison Operators

    Boolean Operators

    Boolean operators AND, OR and NOT are used in search engines and in python they evaluate to values of the boolean data type.  These can be understood by the Truth Table and these can be applied to nonmathematical data types like strings (words).  

    AND

    In an AND condition both statements must be true for the result to evaluate to true.  For example, if you have an exam and a calculator you can use the calculator on the exam, but if you have an exam and no calculator, or a calculator and no exam, or neither a calculator or an exam, you can't use a calculator on an exam.

    clipboard_e11816880f4fcd6c7675ef2166bbb9a28.pngFigure \(\PageIndex{1}\): Copy and Paste Caption here. (Copyright; author via source)
    have_calculator=True
    have_exam = True
    print(f"I have calculator {have_calculator} \n and I have exam {have_exam} \n so can I use calculator on exam? {have_calculator and have_exam}\n")
    
    have_calculator=False
    have_exam = True
    #print("Can I use calculator on exam?",have_calculator and have_exam)
    print(f"I have calculator {have_calculator} \n and I have exam {have_exam} \n so can I use calculator on exam? {have_calculator and have_exam} \n")
    
    
    have_calculator=True
    have_exam = False
    #print("Can I use calculator on exam?",have_calculator and have_exam)
    print(f"I have calculator {have_calculator} \n and I have exam {have_exam} \n so can I use calculator on exam? {have_calculator and have_exam} \n")
    
    
    have_calculator=False
    have_exam = False
    #print("Can I use calculator on exam?",have_calculator and have_exam)
    print(f"I have calculator {have_calculator} \n and I have exam {have_exam} \n so can I use calculator on exam? {have_calculator and have_exam}")
    
    Hello world!

     

    OR

    For an OR value to be true either one or both conditions are true, and this only evaluates false if both conditions are not met.

    clipboard_ec08ae34a9473c78cbc866b55a24dd2a4.pngFigure \(\PageIndex{1}\): Copy and Paste Caption here. (Copyright; author via source)
    have_pen = True
    have_pencil = True
    print(f"I have a pen {have_pen} \n and I have a pencil {have_pencil} \n so can I write on paper? {have_pen or have_pencil}\n")
    
    have_pen = False
    have_pencil = True
    print(f"I have a pen {have_pen} \n and I have a pencil {have_pencil} \n so can I write on paper? {have_pen or have_pencil}\n")
    
    have_pen = True
    have_pencil = False
    print(f"I have a pen {have_pen} \n and I have a pencil {have_pencil} \n so can I write on paper? {have_pen or have_pencil}\n")
    
    have_pen = False
    have_pencil = False
    print(f"I have a pen {have_pen} \n and I have a pencil {have_pencil} \n so can I write on paper? {have_pen or have_pencil}\n")
    
    Hello world!

     

    NOT

    The NOT operator negates a boolean value.  This is a little confusing, but it takes a true statement and makes it false.  For example we can say if it is sunny, then it is not cloudy.  This should not be confused with the not equal (!=) comparison operator, that also evaluates to a Boolean value.

    is_sunny = True
    is_cloudy = not is_sunny
    print("Is it sunny",is_sunny)
    print("Is it cloudy", is_cloudy)
    Hello world!

    Comparison Operators

    There are 8 common comparison operators

    symbol meaning  symbol meaning
    < less than <= less than or equal
    > greater than >= greater than or equal
    == equal != not equal
    is object identity is not negated object identity

    Note that in python the equal sign [=] is the assignment operator and assigns a value to a variable.

     


    This page titled 4: Boolean is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?