Skip to main content
Engineering LibreTexts

4.3: Boolean Operations

  • Page ID
    117542
  • \( \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 the purpose of logical operators.
    • Describe the truth tables for and, or, and not.
    • Create expressions with logical operators.
    • Interpret if-else statements with conditions using logical operators.

    Logical operator: and

    Decisions are often based on multiple conditions. Ex: A program printing if a business is open may check that hour >= 9 and hour < 17. A logical operator takes condition operand(s) and produces True or False.

    Python has three logical operators: and, or, and not. The and operator takes two condition operands and returns True if both conditions are true.

    Table 4.1 Truth table: p and q.
    p q p and q
    True
    True
    True
    True
    False
    False
    False
    True
    False
    False
    False
    False
    Checkpoint: Example: Museum entry
    Concepts in Practice \(\PageIndex{1}\): Using the and operator

    Consider the example above. Jaden tries to enter when the capacity is 2500 and there are 2 hours before close. Can Jaden enter?

    1. yes
    2. no
    Answer

    a. 2500 < 3000 is True , and 2.5 > 1.0 is True , so Jaden can enter.

    Concepts in Practice \(\PageIndex{2}\): Using the and operator

    Consider the example above. Darcy tries to enter when the capacity is 3000. For what values of hrs_to_close will Darcy to be able to enter?

    1. hrs_to close > 1.0
    2. no such value
    Answer

    b. 3000 > 3000 is False . False and True is False . False and False is False . So no value of hrs_to_close will allow Darcy to enter.

    Concepts in Practice \(\PageIndex{3}\): Using the and operator

    Given is_admin = False and is_online = True, what is the value of is_admin and is_online?

    1. True
    2. False
    Answer

    b. False and True is False .

    Concepts in Practice \(\PageIndex{4}\): Using the and operator

    Given x = 8 and y = 21, what is the final value of z?

    if (x < 10) and (y > 20):
      z = 5
    else:
      z = 0
    
    1. 0
    2. 5
    Answer

    b. (8 < 10) and (21 > 20) evaluates to True and True, which is True . So the body of the if is executed, and z is assigned with 5 .

    Logical operator: or

    Sometimes a decision only requires one condition to be true. Ex: If a student is in the band or choir, they will perform in the spring concert. The or operator takes two condition operands and returns True if either condition is true.

    p q p or q
    True
    True
    True
    True
    False
    True
    False
    True
    True
    False
    False
    False
    Table 4.2: Truth table: p or q.
    Checkpoint: Example: Streaming prompt
    Concepts in Practice \(\PageIndex{5}\): Using the or operator

    Given days = 21 and is_damaged is False, is the refund processed?

    if (days < 30) or is_damaged:
      # Process refund
    
    1. yes
    2. no
    Answer

    a. (21 < 30) or False evaluates to True or False, which is True .

    Concepts in Practice \(\PageIndex{6}\): Using the or operator

    For what values of age is there no discount?

    if (age < 12) or (age > 65):
      # Apply student/senior discount
    
    1. age >= 12
    2. age <= 65
    3. (age >= 12) and (age <= 65)
    Answer

    c. The discount does not apply for ages between 12 and 65 inclusive.

    Concepts in Practice \(\PageIndex{7}\): Using the or operator

    Given a = 9 and b = 10, does the test pass?

    if (a%2 == 0 and b%2 == 1) or (a%2 == 1 and b%2 == 0):
      # Test passed
    else:
      # Test failed
    
    1. yes
    2. no
    Answer

    a. (9%2==0 and 10%2 ==1) evaluates to (False and False), which is False . (9%2 == 1 and 10%2 == 0) evaluates to (True and True), which is True . False or True is True . The if statement checks whether the numbers form an even-odd or odd-even pair.

    Logical operator: not

    If the computer is not on, press the power button. The not operator takes one condition operand and returns True when the operand is false and returns False when the operand is true.

    not is a useful operator that can make a condition more readable and can be used to toggle a Boolean's value. Ex: is_on = not is_on.

    Table 4.3 Truth table: not p.
    p not p
    True
    False
    False
    True
    Checkpoint: Example: Diving warning
    Concepts in Practice \(\PageIndex{8}\): Using the not operator

    Given x = 13, what is the value of not(x < 10)?

    1. True
    2. False
    Answer

    a. not(13 < 10) evaluates to not(False), which is True .

    Concepts in Practice \(\PageIndex{9}\): Using the not operator

    Given x = 18, is x in the correct range?

    if not(x > 15 and x < 20):
      # x in correct range
    
    1. yes
    2. no
    Answer

    b. not(18 > 15 and 18 < 20) evaluates to not(True and True), then not(True), and finally, False .

    Concepts in Practice \(\PageIndex{10}\): Using the not operator

    Given is_turn = False and timer = 65, what is the final value of is_turn?

    if timer > 60:
      is_turn = not is_turn
    
    1. True
    2. False
    Answer

    a. 65 > 60 is True , so is_turn is assigned with not(False), which is True .

    Try It: Speed limits

    Write a program that reads in a car's speed as an integer and checks if the car's speed is within the freeway limits. A car's speed must be at least 45 mph but no greater than 70 mph on the freeway.

    If the speed is within the limits, print "Good driving". Else, print "Follow the speed limits".

    Interactive Code
     
     
    Input

    65

    Answer

    speed = int(input())

    if (speed >= 45) and (speed <= 70):
    print("Good driving")
    else:
    print("Follow the speed limits")


    This page titled 4.3: Boolean Operations 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.