Skip to main content
Engineering LibreTexts

4.1: Boolean Values

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

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

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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{\longvect}{\overrightarrow}\)

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

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    Learning Objectives

    By the end of this section you should be able to

    • Explain a Boolean value.
    • Use bool variables to store Boolean values.
    • Demonstrate converting integers, floats, and strings to Booleans.
    • Demonstrate converting Booleans to integers, floats, and strings.
    • Use comparison operators to compare integers, floats, and strings.

    bool data type

    People often ask binary questions such as yes/no or true/false questions. Ex: Do you like pineapple on pizza? Ex: True or false: I like pineapple on pizza. The response is a Boolean value, meaning the value is either true or false. The bool data type, standing for Boolean, represents a binary value of either true or false. true and false are keywords, and capitalization is required.

    Checkpoint: Example: Crosswalk sign
    Concepts in Practice \(\PageIndex{1}\): Using Boolean variables

    Consider the following code:

        is_fruit = "True"
        is_vegetable = 0
        is_dessert = False

    What is the data type of is_fruit?

    1. Boolean
    2. integer
    3. string
    Answer

    c. "True" uses quotation marks, so the value is a string.

    Concepts in Practice \(\PageIndex{2}\): Using Boolean variables

    Consider the following code:

        is_fruit = "True"
        is_vegetable = 0
        is_dessert = False

    What is the data type of is_vegetable?

    1. Boolean
    2. integer
    3. string
    Answer

    b. is_vegetable is an integer and can be used in arithmetic operations.

    Concepts in Practice \(\PageIndex{3}\): Using Boolean variables

    Consider the following code:

        is_fruit = "True"
        is_vegetable = 0
        is_dessert = False

    What is the data type of is_dessert?

    1. Boolean
    2. integer
    3. string
    Answer

    a. A variable assigned with either True or False is a Boolean variable.

    Concepts in Practice \(\PageIndex{4}\): Using Boolean variables

    Consider the following code:

        is_fruit = "True"
        is_vegetable = 0
        is_dessert = False

    How many values can a Boolean variable represent?

    1. 2
    2. 4
    3. 8
    Answer

    a. A Boolean variable represents the value True or False .

    Concepts in Practice \(\PageIndex{5}\): Using Boolean variables

    Consider the following code:

        is_fruit = "True"
        is_vegetable = 0
        is_dessert = False

    Which is a valid value for a Boolean variable?

    1. true
    2. True
    3. 1
    Answer

    b. True and False are the only two values for a Boolean variable.

    Concepts in Practice \(\PageIndex{6}\): Using Boolean variables

    Consider the following code:

        is_fruit = "True"
        is_vegetable = 0
        is_dessert = False

    Suppose the following is added to the code above:

    is_dessert = 0
    print(type(is_dessert))
    

    What is the output?

    1. <class 'bool'>
    2. <class 'int'>
    3. Error
    Answer

    b. Assigning a bool with an int results in implicit type conversion. The type conversion can cause issues later in the code if the programmer assumes is_dessert is still a Boolean.

    Type conversion with bool()

    Deciding whether a value is true or false is helpful when writing programs/statements based on decisions. Converting data types to Booleans can seem unintuitive at first. Ex: Is "ice cream" True? But the conversion is actually simple.

    bool() converts a value to a Boolean value, True or False.

    • True: any non-zero number, any non-empty string
    • False: 0, empty string
    Checkpoint: Converting integers, floats, and strings using bool()
    Concepts in Practice \(\PageIndex{7}\): Converting numeric types and strings to Booleans

    bool(0.000)

    1. True
    2. False
    Answer

    b. Any numeric type representing 0 converts to False . Non-zero numbers convert to True .

    Concepts in Practice \(\PageIndex{8}\): Converting numeric types and strings to Booleans

    bool(-1)

    1. True
    2. False
    Answer

    a. A negative number is non-zero and converts to True .

    Concepts in Practice \(\PageIndex{9}\): Converting numeric types and strings to Booleans

    bool("")

    1. True
    2. False
    Answer

    b. The empty string is the only string that converts to False .

    Concepts in Practice \(\PageIndex{10}\): Converting numeric types and strings to Booleans

    bool("0")

    1. True
    2. False
    Answer

    a. "0" is a non-empty string and is converted to True .

    Concepts in Practice \(\PageIndex{11}\): Converting numeric types and strings to Booleans

    Given input False, what is bool(input())?

    1. True
    2. False
    Answer

    a. The input is read in as a string, so bool("False") is True .

    Concepts in Practice \(\PageIndex{12}\): Converting Booleans to numeric types and strings

    Given is_on = True, what is the value of each expression?

    float(is_on)

    1. 0.0
    2. 1.0
    Answer

    b. True converted to a float is 1.0. False converted to a float is 0.0.

    Concepts in Practice \(\PageIndex{13}\): Converting Booleans to numeric types and strings

    Given is_on = True, what is the value of each expression?

    str(is_on)

    1. "is_on"
    2. "True"
    Answer

    b. True converted to a string is "True" . False converted to a string is "False" .

    Concepts in Practice \(\PageIndex{14}\): Converting Booleans to numeric types and strings

    Given is_on = True, what is the value of each expression?

    int(is_on)

    1. 0
    2. 1
    Answer

    b. When a Boolean is converted to a numeric type, True is converted to 1 , and False is converted to 0 .

    Comparison operators

    Programmers often have to answer questions like "Is the current user the admin?" A programmer may want to compare a string variable, user, to the string, "admin". Comparison operators are used to compare values, and the result is either true or false. Ex: is_admin = (user == "admin"). user is compared with "admin" using the == operator, which tests for equality. The Boolean variable, is_admin, is assigned with the Boolean result.

    The 6 comparison operators:

    • equal to: ==
    • not equal to: !=
    • greater than: >
    • less than: <
    • greater than or equal to: >=
    • less than or equal to: <=
    Checkpoint: Example: Rolling a d20 in a tabletop game
    Concepts in Practice \(\PageIndex{15}\): Comparing values

    For each new variable, what is the value of compare_result?

    x = 14
    compare_result = (x <= 13)
    
    1. True
    2. False
    Answer

    b. 14 is greater than 13, so 14 is not less than or equal to 13.

    Concepts in Practice \(\PageIndex{16}\): Comparing values

    For each new variable, what is the value of compare_result?

    w = 0
    compare_result = (w != 0.4)
    
    1. True
    2. False
    Answer

    a. 0 is less than 0.4, so 0 is not equal to 0.4.

    Concepts in Practice \(\PageIndex{17}\): Comparing values

    For each new variable, what is the value of compare_result?

    v = 4
    compare_result = (v < 4.0)
    
    1. True
    2. False
    Answer

    b. 4 is equal to 4.0, so 4 is not less than 4.0.

    Concepts in Practice \(\PageIndex{18}\): Comparing values

    For each new variable, what is the value of compare_result?

    y = 2
    compare_result = (y > "ab")
    
    1. True
    2. False
    3. Error
    Answer

    c. Numeric types cannot be compared to strings using the comparison operators: >, <, >=, <=. Using == or != will produce a Boolean.

    Concepts in Practice \(\PageIndex{19}\): Comparing values

    For each new variable, what is the value of compare_result?

    z = "cilantro"
    compare_result = (z == "coriander")
    
    1. True
    2. False
    Answer

    b. Strings are compared by comparing each character's Unicode values. Letters have ascending Unicode values in alphabetical order (and are case sensitive). "c" and "c" are equal, so "i" is compared to "o" . "i" is not equal to "o" so False is produced, though cilantro and coriander come from the same plant.

    Concepts in Practice \(\PageIndex{20}\): Comparing values

    For each new variable, what is the value of compare_result?

    a = "dog"
    compare_result = (a < "cat")
    
    1. True
    2. False
    Answer

    b. Letters have ascending Unicode values in alphabetical order (and are case sensitive). "d" is greater than, not less than, "c" , so False is produced.

    = vs ==

    A common mistake is using = for comparison instead of ==. Ex: is_zero = num=0 will always assign is_zero and num with 0, regardless of num's original value. The = operator performs assignment and will modify the variable. The == operator performs comparison, does not modify the variable, and produces True or False.

    Exploring further
    Try It: Friday Boolean

    "It's Friday, I'm in love" —from "Friday I'm in Love," a song released by the Cure in 1992.

    Write a program that reads in the day of the week. Assign the Boolean variable, in_love, with the result of whether the day is Friday or not.

    Interactive Code
    # Read in day 
    
    in_love = # Compute boolean
    print("It's", day, "I'm in love?", in_love)
     
    Input

    Friday

    Answer

    day = input()

    in_love = (day == "Friday")
    print("It's", day, "I'm in love?", in_love)

    Try It: Even numbers

    Write a program that reads in an integer and prints whether the integer is even or not. Remember, a number is even if the number is divisible by 2. To test this use number % 2 == 0. Ex: If the input is 6, the output is "6 is even: True".

    Interactive Code
     
     
    Input

    6

    Answer

    num = int(input())

    is_even = (num % 2 == 0)
    print(num, "is even:", is_even)


    This page titled 4.1: Boolean Values is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.